You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

11 年之前
10 年之前
10 年之前
10 年之前
11 年之前
11 年之前
11 年之前
11 年之前
10 年之前
11 年之前
10 年之前
10 年之前
10 年之前
11 年之前
10 年之前
11 年之前
11 年之前
10 年之前
10 年之前
11 年之前
11 年之前
11 年之前
11 年之前
11 年之前
11 年之前
11 年之前
11 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. #!/bin/sh
  2. set -e
  3. # setup.sh --- 10sr setup script
  4. # 2014, 10sr. Unlicensed <http://unlicense.org>
  5. __setups="shrc_common gitconf tmux scripts darwin dirs selfupdate windirs dotfiles env"
  6. __homelocal="$HOME/.local"
  7. __homevar="$HOME/.var"
  8. __dotfiles_dir_default="$HOME/10sr_dotfiles"
  9. _current_date=`date`
  10. # TODO: how to give args to command?
  11. #################################################################
  12. _dotfiles_url_base=https://raw.githubusercontent.com/10sr/dotfiles/master/
  13. if test -z "$DOTFILES_DIR"
  14. then
  15. DOTFILES_DIR="$__dotfiles_dir_default"
  16. fi
  17. ismsys=
  18. iscygwin=
  19. iswindows=
  20. isdarwin=
  21. isfreebsd=
  22. isbsd=
  23. islinux=
  24. ###########################
  25. # utils
  26. ##########################33
  27. # Utilities
  28. __tput_bold=`tput bold || true`
  29. __tput_default=`tput sgr0 || true`
  30. __tput_yellow=`tput setaf 3 || true`
  31. _msg(){
  32. echo ">> ${__tput_bold}$*${__tput_default}"
  33. }
  34. _warn(){
  35. echo ">>> ${__tput_yellow}${__tput_bold}$*${__tput_default}" 1>&2
  36. }
  37. _die(){
  38. _warn "$@"
  39. exit 1
  40. }
  41. _download(){
  42. # download <url> <file>
  43. if type wget >/dev/null 2>&1
  44. then
  45. wget $__my_wget_options "$1" -O "$2"
  46. elif type curl >/dev/null 2>&1
  47. then
  48. curl --url "$1" --output "$2"
  49. else
  50. _warn "No download program found."
  51. _die "Install wget or curl."
  52. fi
  53. }
  54. # Detect systems
  55. detect_systems(){
  56. ismsys=false
  57. iscygwin=false
  58. iswindows=false
  59. isdarwin=false
  60. isfreebsd=false
  61. isbsd=false
  62. islinux=false
  63. # $OSTYPE is another choice. which is better?
  64. # NOTE: sh on FreeBSD does not define OSTYPE
  65. case `uname` in
  66. (MINGW*) ismsys=true ;;
  67. (CYGWIN*) iscygwin=true ;;
  68. (Darwin*) isdarwin=true ;;
  69. (FreeBSD*) isfreebsd=true ;;
  70. (Linux*) islinux=true ;;
  71. esac
  72. ($ismsys || $iscygwin) && iswindows=true
  73. # is this true?
  74. ($isdarwin || $isfreebsd) && isbsd=true
  75. _msg "Detecting the system...done"
  76. }
  77. ################################
  78. # setups
  79. ###############################
  80. # setup selfupdate
  81. setup_selfupdate(){
  82. _msg "Download latest setup.sh from 10sr repository."
  83. mkdir -p "$DOTFILES_nDIR"
  84. _download $_dotfiles_url_base/setup.sh "$DOTFILES_DIR/"setup.sh
  85. chmod +x "$DOTFILES_DIR"/setup.sh
  86. }
  87. ##################################
  88. # setup dotfiles
  89. setup_dotfiles(){
  90. _msg "Prepare latest dotfiles."
  91. mkdir -p "$DOTFILES_DIR"
  92. if test "$1" = "--git"
  93. then
  94. # git clone
  95. _msg "Option \"--git\" has been given. Using git"
  96. if test -d "$DOTFILES_DIR"/.git
  97. then
  98. _warn "Git repository $DOTFILES_DIR already exists"
  99. _warn "Skipping"
  100. else
  101. _msg "Checking github.com connectivity"
  102. ssh git@github.com 2>/dev/null && true
  103. if test $? -eq 1
  104. then
  105. _git_clone_url=git@github.com:10sr/dotfiles.git
  106. _msg "Authentication succeeded"
  107. else
  108. _git_clone_url=https://github.com/10sr/dotfiles.git
  109. _msg "Authentication failed"
  110. fi
  111. _msg "Git cloning $_git_clone_url"
  112. git clone $_git_clone_url "$DOTFILES_DIR"
  113. fi
  114. else
  115. for f in $@
  116. do
  117. _msg "Prepareing $f"
  118. mkdir -p "`dirname $f`"
  119. _download $_dotfiles_url_base/$f "$DOTFILES_DIR"/$f
  120. done
  121. fi
  122. }
  123. #############################
  124. # setup shrc_common
  125. # Generate ~/.shrc.common, which contains system infos and is sourced from
  126. # setup.sh (this file) and dotfiles/shrc .
  127. # this variable must consistent with shrc
  128. __shrc_common="$HOME/.shrc.common"
  129. setup_shrc_common(){
  130. _msg "Generate $__shrc_common"
  131. test -f "$__shrc_common" && rm -- "$__shrc_common"
  132. cat <<__EOC__ >"$__shrc_common"
  133. #!/bin/sh
  134. # $__shrc_common
  135. # Automatically generated by $0 at $_current_date
  136. ismsys=$ismsys
  137. iscygwin=$iscygwin
  138. iswindows=$iswindows
  139. isdarwin=$isdarwin
  140. isfreebsd=$isfreebsd
  141. isbsd=$isbsd
  142. islinux=$islinux
  143. __homelocal="$__homelocal"
  144. __homevar="$__homevar"
  145. __EOC__
  146. }
  147. ################################
  148. # setup gitconf
  149. setup_gitconf(){
  150. _msg "Configure git environment"
  151. if ! command -v git >/dev/null
  152. then
  153. _msg "Git program not found"
  154. return 0
  155. fi
  156. _gc="git config --global"
  157. $_gc user.name '10sr'
  158. $_gc user.email '8slashes+git@gmail.com'
  159. $_gc core.autocrlf false
  160. $_gc core.excludesfile '~/.gitignore'
  161. $_gc color.ui auto
  162. $_gc status.relativePaths false
  163. $_gc status.showUntrackedFiles normal
  164. $_gc log.date iso
  165. command -v xz >/dev/null && \
  166. $_gc tar.txz.command "xz -c"
  167. $_gc push.default current
  168. $_gc alias.graph "log --graph --date-order -C -M --pretty=tformat:\"%C(green)%h%C(reset) %C(white)%ad%C(reset) %C(red)%an%C(reset)%C(yellow)%d%C(reset) %C(white bold)%s%C(reset)\" --all --date=iso -n 499"
  169. $_gc alias.st "status -s -b"
  170. $_gc alias.b "branch"
  171. $_gc alias.sb "show-branch"
  172. $_gc alias.ci "commit --verbose"
  173. $_gc alias.co "checkout"
  174. $_gc alias.cim "commit --verbose -m"
  175. $_gc alias.di "diff --color"
  176. $_gc alias.me "merge --no-ff --stat --verbose"
  177. $_gc alias.gr "grep -n"
  178. $_gc alias.ls "ls-files"
  179. # $_gc alias.ls "ls-files -v --full-name"
  180. # $_gc alias.ls "status -u -s ."
  181. $_gc alias.sl "!sl"
  182. # $_gc alias.my-ls "ls-files | xargs ls"
  183. # $_gc alias.ll "!git ls-files | xargs ls -l -CFG --color=auto --time-style=long-iso"
  184. $_gc alias.addi "add -i"
  185. $_gc alias.clean-p "!test -z \"\$(git status -s -uno)\""
  186. # alias open-branch and close-branch, which will be useful for topic branch
  187. # workflow
  188. _git_open_branch="checkout -b"
  189. _git_close_branch="!sh -cx 'git stash && \
  190. git checkout master && git merge --no-ff --stat --verbose -'"
  191. $_gc alias.open-branch "$_git_open_branch"
  192. $_gc alias.close-branch "$_git_close_branch"
  193. $_gc alias.o "$_git_open_branch"
  194. $_gc alias.c "$_git_close_branch"
  195. $_gc alias.todo "grep -E -i 'todo:|note:|fixme:'"
  196. #$_gc alias.wc "!git ls-files -z | xargs -0 wc"
  197. # $_gc push.default "simple"
  198. if $iswindows; then
  199. $_gc core.fileMode false
  200. fi
  201. }
  202. #############################
  203. # setup tmux
  204. setup_tmux(){
  205. tmux_conf_local="$HOME/.tmux.conf.local"
  206. _msg "Generate $tmux_conf_local"
  207. case "`hostname`" in
  208. arch-aspireone)
  209. tmux_bg_color=yellow
  210. tmux_fg_color=black
  211. ;;
  212. arch-mba)
  213. tmux_bg_color=cyan
  214. tmux_fg_color=black
  215. ;;
  216. newkiwi)
  217. tmux_bg_color=magenta
  218. tmux_fg_color=white
  219. ;;
  220. freebsd-vb-win7-opti)
  221. tmux_bg_color=red
  222. tmux_fg_color=white
  223. ;;
  224. *)
  225. tmux_bg_color=green
  226. tmux_fg_color=black
  227. ;;
  228. esac
  229. cat <<__EOC__ >"$tmux_conf_local"
  230. # $tmux_conf_local
  231. # Automatically generated by $0 at $_current_date
  232. set -g status-right "${USER}@$(hostname) | #(tmux -V) "
  233. set -g status-bg $tmux_bg_color
  234. set -g status-fg $tmux_fg_color
  235. set -g mode-bg $tmux_bg_color
  236. set -g mode-fg $tmux_fg_color
  237. set -g pane-active-border-fg $tmux_bg_color
  238. __EOC__
  239. }
  240. ##############################
  241. # setup scripts
  242. _fetch_script(){
  243. # _fetch_script <url> <binname>
  244. url="$1"
  245. name="$2"
  246. dst="$HOME/.local/bin/$name"
  247. test -f "$dst" && return 0
  248. command -v "$name" >/dev/null && return 0
  249. if _download "$url" "$dst"
  250. then
  251. chmod u+x "$dst"
  252. else
  253. test -f "$dst" && rm -- "$dst"
  254. fi
  255. }
  256. setup_scripts(){
  257. _msg "Download some utility scripts"
  258. _fetch_script \
  259. https://gist.github.com/10sr/6852317/raw/colortable16.sh colortable16.sh
  260. _fetch_script \
  261. https://gist.github.com/10sr/6852331/raw/256colors2.pl 256colors2.pl
  262. _fetch_script \
  263. https://github.com/icy/pacapt/raw/ng/pacapt pacapt
  264. }
  265. ################################
  266. # setup darwin
  267. __darwin_set_defaults(){
  268. $isdarwin || return 0
  269. # http://appdrill.net/60641/mac-boot-mute.html
  270. #sudo nvram SystemAudioVolume=%80
  271. # add quit entry in menu
  272. defaults write com.apple.finder QuitMenuItem -bool YES
  273. # show full path on titlebar
  274. defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES
  275. # do not show desktop icons
  276. defaults write com.apple.finder CreateDesktop -boolean false
  277. killall Finder
  278. # disable dashboard
  279. #defaults write com.apple.dashboard mcx-disabled -bool YES
  280. }
  281. __darwin_start_daemon(){
  282. $isdarwin || return 0
  283. test "`launchctl getenv LC_ALL`" = C || sudo launchctl setenv LC_ALL C
  284. if ! (launchctl list | grep com.apple.locate) >/dev/null
  285. then
  286. sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
  287. fi
  288. }
  289. setup_darwin(){
  290. __darwin_set_defaults
  291. __darwin_start_daemon
  292. }
  293. ##########################
  294. # setup windirs
  295. setup_windirs(){
  296. $iswindows || return 0
  297. _msg "Setup some directories for windows environment"
  298. if $iscygwin
  299. then
  300. #__winhome="/cygdrive/c/Users/`whoami`"
  301. __winhome=`cygpath -H`/`whoami`
  302. fi
  303. if test -n "$__winhome" -a -d "$__winhome" -a '!' -e "$HOME/.winhome"
  304. then
  305. _msg Make symlink to "$__winhome" with name "$HOME/.winhome"
  306. ln -s "$__winhome" "$HOME/.winhome"
  307. fi
  308. }
  309. #########################
  310. # setup dirs
  311. setup_dirs(){
  312. _msg Make some direcoties
  313. mkdir -vp "$__homelocal"
  314. mkdir -vp "$__homelocal/bin"
  315. mkdir -vp "$__homevar"
  316. }
  317. ####################################
  318. # setup env
  319. # setup new environment with default options
  320. setup_env(){
  321. setup_shrc_common
  322. setup_dirs
  323. setup_gitconf
  324. setup_tmux
  325. setup_scripts
  326. setup_dotfiles --git
  327. }
  328. #########################
  329. # main
  330. main(){
  331. detect_systems
  332. if test -z "$1"
  333. then
  334. echo "Usage: ./setup.sh <cmd> ..."
  335. echo "Available cmds are: $__setups"
  336. exit 1
  337. fi
  338. _cmd=$1
  339. shift
  340. _msg Running setup_$_cmd
  341. setup_$_cmd "$@"
  342. _msg Setup done
  343. }
  344. main "$@"