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

11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. #!/bin/sh
  2. set -e
  3. # setup.sh --- 10sr setup script
  4. # 2014, 10sr. Unlicensed <http://unlicense.org>
  5. __setups="shrc_common tmux scripts darwin dirs selfupdate windirs dotfiles env"
  6. __homelocal="$HOME/.local"
  7. __homevar="$HOME/.var"
  8. __dotfiles_dir_default="$HOME/10sr_dotfiles"
  9. _current_date=`date`
  10. # TODO: how to give args to command?
  11. #################################################################
  12. _dotfiles_url_base=https://raw.githubusercontent.com/10sr/dotfiles/master/
  13. if test -z "$DOTFILES_DIR"
  14. then
  15. DOTFILES_DIR="$__dotfiles_dir_default"
  16. fi
  17. ismsys=
  18. iscygwin=
  19. iswindows=
  20. isdarwin=
  21. isfreebsd=
  22. isbsd=
  23. islinux=
  24. ###########################
  25. # utils
  26. ##########################33
  27. # Utilities
  28. __tput_bold=`tput bold || true`
  29. __tput_default=`tput sgr0 || true`
  30. __tput_yellow=`tput setaf 3 || true`
  31. _msg(){
  32. echo ">> ${__tput_bold}$*${__tput_default}"
  33. }
  34. _warn(){
  35. echo ">>> ${__tput_yellow}${__tput_bold}$*${__tput_default}" 1>&2
  36. }
  37. _die(){
  38. _warn "$@"
  39. exit 1
  40. }
  41. _download(){
  42. # download <url> <file>
  43. if type wget >/dev/null 2>&1
  44. then
  45. wget $__my_wget_options "$1" -O "$2"
  46. elif type curl >/dev/null 2>&1
  47. then
  48. curl --url "$1" --output "$2"
  49. else
  50. _warn "No download program found."
  51. _die "Install wget or curl."
  52. fi
  53. }
  54. # Detect systems
  55. detect_systems(){
  56. ismsys=false
  57. iscygwin=false
  58. iswindows=false
  59. isdarwin=false
  60. isfreebsd=false
  61. isbsd=false
  62. islinux=false
  63. # $OSTYPE is another choice. which is better?
  64. # NOTE: sh on FreeBSD does not define OSTYPE
  65. case `uname` in
  66. (MINGW*) ismsys=true ;;
  67. (CYGWIN*) iscygwin=true ;;
  68. (Darwin*) isdarwin=true ;;
  69. (FreeBSD*) isfreebsd=true ;;
  70. (Linux*) islinux=true ;;
  71. esac
  72. ($ismsys || $iscygwin) && iswindows=true
  73. # is this true?
  74. ($isdarwin || $isfreebsd) && isbsd=true
  75. _msg "Detecting the system...done"
  76. }
  77. ################################
  78. # setups
  79. ###############################
  80. # setup selfupdate
  81. setup_selfupdate(){
  82. _msg "Download latest setup.sh from 10sr repository."
  83. if test -z "$1"
  84. then
  85. _warn "Filename for setup script is not given"
  86. echo "usage: setup.sh selfupdate <filename>"
  87. _die "Abort"
  88. fi
  89. mkdir -vp "`dirname $1`"
  90. _download $_dotfiles_url_base/setup.sh "$1"
  91. chmod +x "$1"
  92. }
  93. ##################################
  94. # setup dotfiles
  95. setup_dotfiles(){
  96. _msg "Prepare latest dotfiles."
  97. mkdir -p "$DOTFILES_DIR"
  98. if test -d "$DOTFILES_DIR"/.git
  99. then
  100. # if git repository found, always skip
  101. _warn "Git repository $DOTFILES_DIR already exists"
  102. _warn "Skipping"
  103. elif test "$1" = "--git"
  104. then
  105. # git clone
  106. _msg "Option \"--git\" has been given. Using git"
  107. _msg "Checking github.com connectivity"
  108. ssh git@github.com 2>/dev/null && true
  109. if test $? -eq 1
  110. then
  111. _git_clone_url=git@github.com:10sr/dotfiles.git
  112. _msg "Authentication succeeded"
  113. else
  114. _git_clone_url=https://github.com/10sr/dotfiles.git
  115. _msg "Authentication failed"
  116. fi
  117. _msg "Git cloning $_git_clone_url"
  118. git clone $_git_clone_url "$DOTFILES_DIR"
  119. else
  120. for f in $@
  121. do
  122. _msg "Prepareing $f"
  123. mkdir -p "`dirname $DOTFILES_DIR/$f`"
  124. _download $_dotfiles_url_base/$f "$DOTFILES_DIR"/$f
  125. done
  126. fi
  127. }
  128. #############################
  129. # setup shrc_common
  130. # Generate ~/.shrc.common, which contains system infos and is sourced from
  131. # setup.sh (this file) and dotfiles/shrc .
  132. # this variable must consistent with shrc
  133. __shrc_common="$HOME/.shrc.common"
  134. setup_shrc_common(){
  135. _msg "Generate $__shrc_common"
  136. test -f "$__shrc_common" && rm -- "$__shrc_common"
  137. cat <<__EOC__ >"$__shrc_common"
  138. #!/bin/sh
  139. # $__shrc_common
  140. # Automatically generated by $0 at $_current_date
  141. ismsys=$ismsys
  142. iscygwin=$iscygwin
  143. iswindows=$iswindows
  144. isdarwin=$isdarwin
  145. isfreebsd=$isfreebsd
  146. isbsd=$isbsd
  147. islinux=$islinux
  148. __homelocal="$__homelocal"
  149. __homevar="$__homevar"
  150. __EOC__
  151. }
  152. #############################
  153. # setup tmux
  154. setup_tmux(){
  155. tmux_conf_local="$HOME/.tmux.conf.local"
  156. _msg "Generate $tmux_conf_local"
  157. case "`hostname`" in
  158. arch-aspireone)
  159. tmux_bg_color=yellow
  160. tmux_fg_color=black
  161. ;;
  162. arch-mba)
  163. tmux_bg_color=cyan
  164. tmux_fg_color=black
  165. ;;
  166. newkiwi)
  167. tmux_bg_color=magenta
  168. tmux_fg_color=white
  169. ;;
  170. debian-vb-win7-opti)
  171. tmux_bg_color=red
  172. tmux_fg_color=white
  173. ;;
  174. *)
  175. tmux_bg_color=green
  176. tmux_fg_color=black
  177. ;;
  178. esac
  179. cat <<__EOC__ >"$tmux_conf_local"
  180. # $tmux_conf_local
  181. # Automatically generated by $0 at $_current_date
  182. set -g status-right "${USER}@$(hostname) | #(tmux -V) "
  183. set -g status-bg $tmux_bg_color
  184. set -g status-fg $tmux_fg_color
  185. set -g mode-bg $tmux_bg_color
  186. set -g mode-fg $tmux_fg_color
  187. set -g pane-active-border-fg $tmux_bg_color
  188. __EOC__
  189. _tmux_conf="$HOME"/.tmux.conf
  190. _msg "Prepare $_tmux_conf"
  191. if test -f "$_tmux_conf"
  192. then
  193. _warn "Tmux config file found. Skipping"
  194. else
  195. echo "source \"$DOTFILES_DIR/tmux.conf\"" >>"$_tmux_conf"
  196. fi
  197. setup_dotfiles tmux.conf
  198. }
  199. ###############################
  200. # setup emacs
  201. setup_emacs(){
  202. _msg "Setup emacs init.el"
  203. _emacs_dir="$HOME"/.emacs.d
  204. mkdir -vp "$_emacs_dir"
  205. _emacs_init_el="$_emacs_dir"/init.el
  206. if test -f "$_emacs_init_el"
  207. then
  208. _warn "Emacs init.el found. Skipping"
  209. else
  210. cat <<__EOC__ >>"$_emacs_init_el"
  211. (and (file-readable-p "$DOTFILES_DIR/emacs.el")
  212. (load-file "$DOTFILES_DIR/emacs.el"))
  213. __EOC__
  214. fi
  215. setup_dotfiles emacs.el
  216. }
  217. ##################################
  218. # setup vim
  219. setup_vim(){
  220. _msg "Setup vimrc"
  221. _vimrc="$HOME"/.vimrc
  222. if test -f "$_vimrc"
  223. then
  224. _warn "Vim rcfile found. Skipping"
  225. else
  226. cat <<__EOC__ >>"$_vimrc"
  227. if filereadable(expand('$DOTFILES_DIR/vimrc'))
  228. source $DOTFILES_DIR/vimrc
  229. endif
  230. __EOC__
  231. fi
  232. setup_dotfiles vimrc
  233. }
  234. ##############################
  235. # setup scripts
  236. _fetch_script(){
  237. # _fetch_script <url> <binname>
  238. url="$1"
  239. name="$2"
  240. dst="$HOME/.local/bin/$name"
  241. test -f "$dst" && return 0
  242. command -v "$name" >/dev/null && return 0
  243. if _download "$url" "$dst"
  244. then
  245. chmod u+x "$dst"
  246. else
  247. test -f "$dst" && rm -- "$dst"
  248. fi
  249. }
  250. setup_scripts(){
  251. _msg "Download some utility scripts"
  252. _fetch_script \
  253. https://gist.github.com/10sr/6852317/raw/colortable16.sh colortable16.sh
  254. _fetch_script \
  255. https://gist.github.com/10sr/6852331/raw/256colors2.pl 256colors2.pl
  256. _fetch_script \
  257. https://github.com/icy/pacapt/raw/ng/pacapt pacapt
  258. _fetch_script \
  259. http://beyondgrep.com/ack-2.12-single-file ack-2.12
  260. }
  261. ################################
  262. # setup darwin
  263. __darwin_set_defaults(){
  264. $isdarwin || return 0
  265. # http://appdrill.net/60641/mac-boot-mute.html
  266. #sudo nvram SystemAudioVolume=%80
  267. # add quit entry in menu
  268. defaults write com.apple.finder QuitMenuItem -bool YES
  269. # show full path on titlebar
  270. defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES
  271. # do not show desktop icons
  272. defaults write com.apple.finder CreateDesktop -boolean false
  273. killall Finder
  274. # disable dashboard
  275. #defaults write com.apple.dashboard mcx-disabled -bool YES
  276. }
  277. __darwin_start_daemon(){
  278. $isdarwin || return 0
  279. test "`launchctl getenv LC_ALL`" = C || sudo launchctl setenv LC_ALL C
  280. if ! (launchctl list | grep com.apple.locate) >/dev/null
  281. then
  282. sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
  283. fi
  284. }
  285. setup_darwin(){
  286. __darwin_set_defaults
  287. __darwin_start_daemon
  288. }
  289. ##########################
  290. # setup windirs
  291. setup_windirs(){
  292. $iswindows || return 0
  293. _msg "Setup some directories for windows environment"
  294. if $iscygwin
  295. then
  296. #__winhome="/cygdrive/c/Users/`whoami`"
  297. __winhome=`cygpath -H`/`whoami`
  298. fi
  299. if test -n "$__winhome" -a -d "$__winhome" -a '!' -e "$HOME/.winhome"
  300. then
  301. _msg Make symlink to "$__winhome" with name "$HOME/.winhome"
  302. ln -s "$__winhome" "$HOME/.winhome"
  303. fi
  304. }
  305. #########################
  306. # setup dirs
  307. setup_dirs(){
  308. _msg Make some direcoties
  309. mkdir -vp "$__homelocal"
  310. mkdir -vp "$__homelocal/bin"
  311. mkdir -vp "$__homevar"
  312. }
  313. ####################################
  314. # setup env
  315. # setup new environment with default options
  316. setup_env(){
  317. setup_shrc_common
  318. setup_dirs
  319. setup_tmux
  320. setup_scripts
  321. setup_dotfiles --git
  322. }
  323. #########################
  324. # help and main
  325. help(){
  326. echo "Usage: ./setup.sh <cmd> ..."
  327. echo " or: ./setup.sh help"
  328. echo "Available cmds are: $__setups"
  329. }
  330. main(){
  331. detect_systems
  332. if test -z "$1"
  333. then
  334. help
  335. return 1
  336. fi
  337. _cmd=$1
  338. shift
  339. if test "$_cmd" = help
  340. then
  341. help
  342. return 0
  343. fi
  344. _msg Running setup_$_cmd
  345. setup_$_cmd "$@"
  346. _msg Running setup_$_cmd done
  347. }
  348. main "$@"