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.
 
 
 
 
 
 

415 lines
9.2 KiB

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