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