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.

setup.sh 7.1 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #!/bin/sh
  2. # setup.sh --- 10sr setup script
  3. __setups="gitconf tmux scripts darwin dirs selfupdate"
  4. __homelocal="$HOME/.local"
  5. __homevar="$HOME/.var"
  6. ###########################
  7. # utils
  8. _download(){
  9. # download <url> <file>
  10. if type wget >/dev/null 2>&1
  11. then
  12. wget $__my_wget_options "$1" -O "$2"
  13. elif type curl >/dev/null 2>&1
  14. then
  15. curl --url "$1" --output "$2"
  16. fi
  17. }
  18. #############################
  19. # gen_common
  20. # Generate ~/.shrc.common, which contains system infos and is sourced from
  21. # setup.sh (this file) and dotfiles/shrc .
  22. # This functions is always called.
  23. # this variable must consistent with shrc
  24. __shrc_common="$HOME/.shrc.common"
  25. gen_common(){
  26. test -f "$__shrc_common" && rm -- "$__shrc_common"
  27. __ismsys=false
  28. __iscygwin=false
  29. __iswindows=false
  30. __isdarwin=false
  31. __isfreebsd=false
  32. __isbsd=false
  33. __islinux=false
  34. # $OSTYPE is another choice. which is better?
  35. # sh on FreeBSD does not define OSTYPE
  36. case `uname` in
  37. (MINGW*) __ismsys=true ;;
  38. (CYGWIN*) __iscygwin=true ;;
  39. (Darwin*) __isdarwin=true ;;
  40. (FreeBSD*) __isfreebsd=true ;;
  41. (Linux*) __islinux=true ;;
  42. esac
  43. if $__ismsys || $__iscygwin
  44. then
  45. __iswindows=true
  46. fi
  47. # is this true?
  48. if $__isdarwin || $__freebsd
  49. then
  50. __isbsd=true
  51. fi
  52. cat <<__EOC__ >"$__shrc_common"
  53. #!/bin/sh
  54. # $__shrc_common
  55. # Automatically generated by $0
  56. ismsys=$__ismsys
  57. iscygwin=$__iscygwin
  58. iswindows=$__iswindows
  59. isdarwin=$__isdarwin
  60. isfreebsd=$__isfreebsd
  61. isbsd=$__isbsd
  62. islinux=$__islinux
  63. __homelocal="$__homelocal"
  64. __homevar="$__homevar"
  65. __EOC__
  66. }
  67. ###############################
  68. # selfupdate
  69. __setup_url="https://raw.github.com/10sr/dotfiles/master/setup.sh"
  70. setup_selfupdate(){
  71. if test -z "$SETUP_OUTPUT"
  72. then
  73. echo SETUP_OUTPUT is not set.
  74. echo Ignore selfupdate.
  75. return
  76. fi
  77. _download $__setup_url "$SETUP_OUTPUT"
  78. }
  79. ################################
  80. # git_configs
  81. setup_gitconf(){
  82. if ! command -v git >/dev/null
  83. then
  84. echo "git not found"
  85. return 1
  86. fi
  87. _gc="git config --global"
  88. $_gc user.name '10sr'
  89. $_gc user.email '8slashes+git@gmail.com'
  90. $_gc core.autocrlf false
  91. $_gc core.excludesfile '~/.gitignore'
  92. $_gc color.ui auto
  93. $_gc status.relativePaths false
  94. $_gc status.showUntrackedFiles normal
  95. $_gc log.date iso
  96. command -v xz >/dev/null && \
  97. $_gc tar.txz.command "xz -c"
  98. $_gc push.default current
  99. $_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"
  100. $_gc alias.st "status -s -b"
  101. $_gc alias.b "branch"
  102. $_gc alias.sb "show-branch"
  103. $_gc alias.ci "commit --verbose"
  104. $_gc alias.co "checkout"
  105. $_gc alias.cim "commit --verbose -m"
  106. $_gc alias.di "diff --color"
  107. $_gc alias.me "merge --no-ff --stat -v"
  108. $_gc alias.gr "grep -n"
  109. $_gc alias.ls "ls-files"
  110. # $_gc alias.ls "ls-files -v --full-name"
  111. # $_gc alias.ls "status -u -s ."
  112. $_gc alias.sl "!sl"
  113. # $_gc alias.my-ls "ls-files | xargs ls"
  114. # $_gc alias.ll "!git ls-files | xargs ls -l -CFG --color=auto --time-style=long-iso"
  115. $_gc alias.addi "add -i"
  116. $_gc alias.clean-p "!test -z \"\$(git status -s -uno)\""
  117. # alias open-branch and close-branch, which will be useful for topic branch
  118. # workflow
  119. _git_open_branch="checkout -b"
  120. _git_close_branch="!sh -cx 'git stash && \
  121. git checkout master && git merge -'"
  122. $_gc alias.open-branch "$_git_open_branch"
  123. $_gc alias.close-branch "$_git_close_branch"
  124. $_gc alias.o "$_git_open_branch"
  125. $_gc alias.c "$_git_close_branch"
  126. $_gc alias.todo "grep -E -i 'todo:|note:|fixme:'"
  127. #$_gc alias.wc "!git ls-files -z | xargs -0 wc"
  128. # $_gc push.default "simple"
  129. if $iswindows; then
  130. $_gc core.fileMode false
  131. fi
  132. }
  133. #############################
  134. # gen_tmux_conf_local
  135. setup_tmux(){
  136. tmux_conf_local="$HOME/.tmux.conf.local"
  137. case "`hostname`" in
  138. arch-aspireone)
  139. tmux_bg_color=yellow
  140. tmux_fg_color=black
  141. ;;
  142. arch-mba)
  143. tmux_bg_color=cyan
  144. tmux_fg_color=black
  145. ;;
  146. newkiwi)
  147. tmux_bg_color=magenta
  148. tmux_fg_color=white
  149. ;;
  150. freebsd-vb-win7-opti)
  151. tmux_bg_color=red
  152. tmux_fg_color=white
  153. ;;
  154. *)
  155. tmux_bg_color=green
  156. tmux_fg_color=black
  157. ;;
  158. esac
  159. cat <<__EOC__ >"$tmux_conf_local"
  160. # $tmux_conf_local
  161. # Automatically generated by $0
  162. set -g status-right "${USER}@$(hostname) | #(tmux -V) "
  163. set -g status-bg $tmux_bg_color
  164. set -g status-fg $tmux_fg_color
  165. set -g mode-bg $tmux_bg_color
  166. set -g mode-fg $tmux_fg_color
  167. set -g pane-active-border-fg $tmux_bg_color
  168. __EOC__
  169. }
  170. ##############################
  171. # install_scripts
  172. _fetch_script(){
  173. # _fetch_script <url> <binname>
  174. url="$1"
  175. name="$2"
  176. dst="$HOME/.local/bin/$name"
  177. command -v "$name" >/dev/null && return
  178. if _download "$url" "$dst"
  179. then
  180. chmod u+x "$dst"
  181. else
  182. test -f "$dst" && rm -- "$dst"
  183. fi
  184. }
  185. setup_scripts(){
  186. _fetch_script \
  187. https://gist.github.com/10sr/6852317/raw/colortable16.sh colortable16.sh
  188. _fetch_script \
  189. https://gist.github.com/10sr/6852331/raw/256colors2.pl 256colors2.pl
  190. }
  191. ################################
  192. # darwin
  193. __darwin_set_defaults(){
  194. $isdarwin || return 1
  195. # http://appdrill.net/60641/mac-boot-mute.html
  196. #sudo nvram SystemAudioVolume=%80
  197. # add quit entry in menu
  198. defaults write com.apple.finder QuitMenuItem -bool YES
  199. # show full path on titlebar
  200. defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES
  201. # do not show desktop icons
  202. defaults write com.apple.finder CreateDesktop -boolean false
  203. killall Finder
  204. # disable dashboard
  205. #defaults write com.apple.dashboard mcx-disabled -bool YES
  206. }
  207. __darwin_start_daemon(){
  208. $isdarwin || return 1
  209. test "`launchctl getenv LC_ALL`" = C || sudo launchctl setenv LC_ALL C
  210. if ! (launchctl list | grep com.apple.locate) >/dev/null
  211. then
  212. sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
  213. fi
  214. }
  215. setup_darwin(){
  216. __darwin_set_defaults
  217. __darwin_start_daemon
  218. }
  219. #########################
  220. # mkdirs
  221. setup_dirs(){
  222. mkdir -p "$__homelocal"
  223. mkdir -p "$__homelocal/bin"
  224. mkdir -p "$__homevar"
  225. }
  226. #########################
  227. # main
  228. main(){
  229. gen_common
  230. . "$__shrc_common"
  231. if test -z "$1"
  232. then
  233. echo "usage: ./setup.sh <setups> ..."
  234. echo "setups: all $__setups"
  235. exit 1
  236. fi
  237. while test -n "$1"
  238. do
  239. if test "$1" = all
  240. then
  241. for c in $__setups
  242. do
  243. set -x
  244. setup_$c
  245. set +x
  246. done
  247. fi
  248. for c in $__setups
  249. do
  250. if test "$1" = "$c"
  251. then
  252. set -x
  253. setup_$c
  254. set +x
  255. fi
  256. done
  257. shift
  258. done
  259. }
  260. main "$@"