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.
 
 
 
 
 
 

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