Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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