Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

339 rader
7.5 KiB

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