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.
 
 
 
 
 
 

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