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.6 KiB

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