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.
 
 
 
 
 
 

310 lines
6.9 KiB

  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 bopen and bclose, which can be useful for topic branching
  112. $_gc alias.bopen "checkout -b"
  113. $_gc alias.bclose \
  114. "!sh -cx 'git stash && git checkout master && git merge --no-ff -'"
  115. $_gc alias.todo "grep -E -i 'todo:|note:|fixme:'"
  116. #$_gc alias.wc "!git ls-files -z | xargs -0 wc"
  117. # $_gc push.default "simple"
  118. if $iswindows; then
  119. $_gc core.fileMode false
  120. fi
  121. }
  122. #############################
  123. # gen_tmux_conf_local
  124. setup_tmux(){
  125. tmux_conf_local="$HOME/.tmux.conf.local"
  126. case "`hostname`" in
  127. arch-aspireone)
  128. tmux_bg_color=yellow
  129. tmux_fg_color=black
  130. ;;
  131. darwin-mba.local)
  132. tmux_bg_color=cyan
  133. tmux_fg_color=black
  134. ;;
  135. newkiwi)
  136. tmux_bg_color=magenta
  137. tmux_fg_color=white
  138. ;;
  139. freebsd-vb-win7-opti)
  140. tmux_bg_color=red
  141. tmux_fg_color=white
  142. ;;
  143. *)
  144. tmux_bg_color=green
  145. tmux_fg_color=black
  146. ;;
  147. esac
  148. cat <<__EOC__ >"$tmux_conf_local"
  149. # $tmux_conf_local
  150. # Automatically generated by $0
  151. set -g status-right "${USER}@$(hostname) | #(tmux -V) "
  152. set -g status-bg $tmux_bg_color
  153. set -g status-fg $tmux_fg_color
  154. set -g mode-bg $tmux_bg_color
  155. set -g mode-fg $tmux_fg_color
  156. set -g pane-active-border-fg $tmux_bg_color
  157. __EOC__
  158. }
  159. ##############################
  160. # install_scripts
  161. _fetch_script(){
  162. # _fetch_script <url> <binname>
  163. url="$1"
  164. name="$2"
  165. dst="$HOME/.local/bin/$name"
  166. command -v "$name" >/dev/null && return
  167. if _download "$url" "$dst"
  168. then
  169. chmod u+x "$dst"
  170. else
  171. test -f "$dst" && rm -- "$dst"
  172. fi
  173. }
  174. setup_scripts(){
  175. _fetch_script \
  176. https://gist.github.com/10sr/6852317/raw/colortable16.sh colortable16.sh
  177. _fetch_script \
  178. https://gist.github.com/10sr/6852331/raw/256colors2.pl 256colors2.pl
  179. }
  180. ################################
  181. # darwin
  182. __darwin_set_defaults(){
  183. $isdarwin || return 1
  184. # http://appdrill.net/60641/mac-boot-mute.html
  185. #sudo nvram SystemAudioVolume=%80
  186. # add quit entry in menu
  187. defaults write com.apple.finder QuitMenuItem -bool YES
  188. # show full path on titlebar
  189. defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES
  190. # do not show desktop icons
  191. defaults write com.apple.finder CreateDesktop -boolean false
  192. killall Finder
  193. # disable dashboard
  194. #defaults write com.apple.dashboard mcx-disabled -bool YES
  195. }
  196. __darwin_start_daemon(){
  197. $isdarwin || return 1
  198. test "`launchctl getenv LC_ALL`" = C || sudo launchctl setenv LC_ALL C
  199. if ! (launchctl list | grep com.apple.locate) >/dev/null
  200. then
  201. sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
  202. fi
  203. }
  204. setup_darwin(){
  205. __darwin_set_defaults
  206. __darwin_start_daemon
  207. }
  208. #########################
  209. # mkdirs
  210. setup_dirs(){
  211. mkdir -p "$__homelocal"
  212. mkdir -p "$__homelocal/bin"
  213. mkdir -p "$__homevar"
  214. }
  215. #########################
  216. # main
  217. main(){
  218. gen_common
  219. . "$__shrc_common"
  220. if test -z "$1"
  221. then
  222. echo "usage: ./setup.sh <setups> ..."
  223. echo "setups: all $__setups"
  224. exit 1
  225. fi
  226. while test -n "$1"
  227. do
  228. if test "$1" = all
  229. then
  230. for c in $__setups
  231. do
  232. set -x
  233. setup_$c
  234. set +x
  235. done
  236. fi
  237. for c in $__setups
  238. do
  239. if test "$1" = "$c"
  240. then
  241. set -x
  242. setup_$c
  243. set +x
  244. fi
  245. done
  246. shift
  247. done
  248. }
  249. main "$@"