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 8.0 KiB

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