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

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 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. mkdir -p "$DOTFILES_nDIR"
  84. _download $_dotfiles_url_base/setup.sh "$DOTFILES_DIR/"setup.sh
  85. chmod +x "$DOTFILES_DIR"/setup.sh
  86. }
  87. ##################################
  88. # setup dotfiles
  89. setup_dotfiles(){
  90. _msg "Prepare latest dotfiles."
  91. mkdir -p "$DOTFILES_DIR"
  92. if test "$1" = "--git"
  93. then
  94. # git clone
  95. _msg "Option \"--git\" has been given. Using git"
  96. if test -d "$DOTFILES_DIR"/.git
  97. then
  98. _warn "Git repository $DOTFILES_DIR already exists"
  99. _warn "Skipping"
  100. else
  101. _msg "Checking github.com connectivity"
  102. ssh git@github.com 2>/dev/null && true
  103. if test $? -eq 1
  104. then
  105. _git_clone_url=git@github.com:10sr/dotfiles.git
  106. _msg "Authentication succeeded"
  107. else
  108. _git_clone_url=https://github.com/10sr/dotfiles.git
  109. _msg "Authentication failed"
  110. fi
  111. _msg "Git cloning $_git_clone_url"
  112. git clone $_git_clone_url "$DOTFILES_DIR"
  113. fi
  114. else
  115. for f in $@
  116. do
  117. _msg "Prepareing $f"
  118. mkdir -p "`dirname $f`"
  119. _download $_dotfiles_url_base/$f "$DOTFILES_DIR"/$f
  120. done
  121. fi
  122. }
  123. #############################
  124. # setup shrc_common
  125. # Generate ~/.shrc.common, which contains system infos and is sourced from
  126. # setup.sh (this file) and dotfiles/shrc .
  127. # this variable must consistent with shrc
  128. __shrc_common="$HOME/.shrc.common"
  129. setup_shrc_common(){
  130. _msg "Generate $__shrc_common"
  131. test -f "$__shrc_common" && rm -- "$__shrc_common"
  132. cat <<__EOC__ >"$__shrc_common"
  133. #!/bin/sh
  134. # $__shrc_common
  135. # Automatically generated by $0 at $_current_date
  136. ismsys=$ismsys
  137. iscygwin=$iscygwin
  138. iswindows=$iswindows
  139. isdarwin=$isdarwin
  140. isfreebsd=$isfreebsd
  141. isbsd=$isbsd
  142. islinux=$islinux
  143. __homelocal="$__homelocal"
  144. __homevar="$__homevar"
  145. __EOC__
  146. }
  147. ################################
  148. # setup gitconf
  149. setup_gitconf(){
  150. _msg "Configure git environment"
  151. if ! command -v git >/dev/null
  152. then
  153. _msg "Git program not found"
  154. return 0
  155. fi
  156. _gc="git config --global"
  157. $_gc user.name '10sr'
  158. $_gc user.email '8slashes+git@gmail.com'
  159. $_gc core.autocrlf false
  160. $_gc core.excludesfile '~/.gitignore'
  161. $_gc color.ui auto
  162. $_gc status.relativePaths false
  163. $_gc status.showUntrackedFiles normal
  164. $_gc log.date iso
  165. command -v xz >/dev/null && \
  166. $_gc tar.txz.command "xz -c"
  167. $_gc push.default current
  168. $_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"
  169. $_gc alias.st "status -s -b"
  170. $_gc alias.b "branch"
  171. $_gc alias.sb "show-branch"
  172. $_gc alias.ci "commit --verbose"
  173. $_gc alias.co "checkout"
  174. $_gc alias.cim "commit --verbose -m"
  175. $_gc alias.di "diff --color"
  176. $_gc alias.me "merge --no-ff --stat --verbose"
  177. $_gc alias.gr "grep -n"
  178. $_gc alias.ls "ls-files"
  179. # $_gc alias.ls "ls-files -v --full-name"
  180. # $_gc alias.ls "status -u -s ."
  181. $_gc alias.sl "!sl"
  182. # $_gc alias.my-ls "ls-files | xargs ls"
  183. # $_gc alias.ll "!git ls-files | xargs ls -l -CFG --color=auto --time-style=long-iso"
  184. $_gc alias.addi "add -i"
  185. $_gc alias.clean-p "!test -z \"\$(git status -s -uno)\""
  186. # alias open-branch and close-branch, which will be useful for topic branch
  187. # workflow
  188. _git_open_branch="checkout -b"
  189. _git_close_branch="!sh -cx 'git stash && \
  190. git checkout master && git merge --no-ff --stat --verbose -'"
  191. $_gc alias.open-branch "$_git_open_branch"
  192. $_gc alias.close-branch "$_git_close_branch"
  193. $_gc alias.o "$_git_open_branch"
  194. $_gc alias.c "$_git_close_branch"
  195. $_gc alias.todo "grep -E -i 'todo:|note:|fixme:'"
  196. #$_gc alias.wc "!git ls-files -z | xargs -0 wc"
  197. # $_gc push.default "simple"
  198. if $iswindows; then
  199. $_gc core.fileMode false
  200. fi
  201. }
  202. #############################
  203. # setup tmux
  204. setup_tmux(){
  205. tmux_conf_local="$HOME/.tmux.conf.local"
  206. _msg "Generate $tmux_conf_local"
  207. case "`hostname`" in
  208. arch-aspireone)
  209. tmux_bg_color=yellow
  210. tmux_fg_color=black
  211. ;;
  212. arch-mba)
  213. tmux_bg_color=cyan
  214. tmux_fg_color=black
  215. ;;
  216. newkiwi)
  217. tmux_bg_color=magenta
  218. tmux_fg_color=white
  219. ;;
  220. freebsd-vb-win7-opti)
  221. tmux_bg_color=red
  222. tmux_fg_color=white
  223. ;;
  224. *)
  225. tmux_bg_color=green
  226. tmux_fg_color=black
  227. ;;
  228. esac
  229. cat <<__EOC__ >"$tmux_conf_local"
  230. # $tmux_conf_local
  231. # Automatically generated by $0 at $_current_date
  232. set -g status-right "${USER}@$(hostname) | #(tmux -V) "
  233. set -g status-bg $tmux_bg_color
  234. set -g status-fg $tmux_fg_color
  235. set -g mode-bg $tmux_bg_color
  236. set -g mode-fg $tmux_fg_color
  237. set -g pane-active-border-fg $tmux_bg_color
  238. __EOC__
  239. _tmux_conf="$HOME"/.tmux.conf
  240. _msg "Prepare $_tmux_conf"
  241. if test -f "$_tmux_conf"
  242. then
  243. _warn "Tmux config file found. Skipping"
  244. else
  245. echo "source \"$DOTFILES_DIR/tmux.conf\"" >>"$_tmux_conf"
  246. fi
  247. }
  248. ##############################
  249. # setup scripts
  250. _fetch_script(){
  251. # _fetch_script <url> <binname>
  252. url="$1"
  253. name="$2"
  254. dst="$HOME/.local/bin/$name"
  255. test -f "$dst" && return 0
  256. command -v "$name" >/dev/null && return 0
  257. if _download "$url" "$dst"
  258. then
  259. chmod u+x "$dst"
  260. else
  261. test -f "$dst" && rm -- "$dst"
  262. fi
  263. }
  264. setup_scripts(){
  265. _msg "Download some utility scripts"
  266. _fetch_script \
  267. https://gist.github.com/10sr/6852317/raw/colortable16.sh colortable16.sh
  268. _fetch_script \
  269. https://gist.github.com/10sr/6852331/raw/256colors2.pl 256colors2.pl
  270. _fetch_script \
  271. https://github.com/icy/pacapt/raw/ng/pacapt pacapt
  272. }
  273. ################################
  274. # setup darwin
  275. __darwin_set_defaults(){
  276. $isdarwin || return 0
  277. # http://appdrill.net/60641/mac-boot-mute.html
  278. #sudo nvram SystemAudioVolume=%80
  279. # add quit entry in menu
  280. defaults write com.apple.finder QuitMenuItem -bool YES
  281. # show full path on titlebar
  282. defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES
  283. # do not show desktop icons
  284. defaults write com.apple.finder CreateDesktop -boolean false
  285. killall Finder
  286. # disable dashboard
  287. #defaults write com.apple.dashboard mcx-disabled -bool YES
  288. }
  289. __darwin_start_daemon(){
  290. $isdarwin || return 0
  291. test "`launchctl getenv LC_ALL`" = C || sudo launchctl setenv LC_ALL C
  292. if ! (launchctl list | grep com.apple.locate) >/dev/null
  293. then
  294. sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
  295. fi
  296. }
  297. setup_darwin(){
  298. __darwin_set_defaults
  299. __darwin_start_daemon
  300. }
  301. ##########################
  302. # setup windirs
  303. setup_windirs(){
  304. $iswindows || return 0
  305. _msg "Setup some directories for windows environment"
  306. if $iscygwin
  307. then
  308. #__winhome="/cygdrive/c/Users/`whoami`"
  309. __winhome=`cygpath -H`/`whoami`
  310. fi
  311. if test -n "$__winhome" -a -d "$__winhome" -a '!' -e "$HOME/.winhome"
  312. then
  313. _msg Make symlink to "$__winhome" with name "$HOME/.winhome"
  314. ln -s "$__winhome" "$HOME/.winhome"
  315. fi
  316. }
  317. #########################
  318. # setup dirs
  319. setup_dirs(){
  320. _msg Make some direcoties
  321. mkdir -vp "$__homelocal"
  322. mkdir -vp "$__homelocal/bin"
  323. mkdir -vp "$__homevar"
  324. }
  325. ####################################
  326. # setup env
  327. # setup new environment with default options
  328. setup_env(){
  329. setup_shrc_common
  330. setup_dirs
  331. setup_gitconf
  332. setup_tmux
  333. setup_scripts
  334. setup_dotfiles --git
  335. }
  336. #########################
  337. # main
  338. main(){
  339. detect_systems
  340. if test -z "$1"
  341. then
  342. echo "Usage: ./setup.sh <cmd> ..."
  343. echo "Available cmds are: $__setups"
  344. exit 1
  345. fi
  346. _cmd=$1
  347. shift
  348. _msg Running setup_$_cmd
  349. setup_$_cmd "$@"
  350. _msg Running setup_$_cmd done
  351. }
  352. main "$@"