25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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