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