Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

setup.sh 11 KiB

há 11 anos
há 11 anos
há 11 anos
há 11 anos
há 11 anos
há 11 anos
há 11 anos
há 11 anos
há 11 anos
há 10 anos
há 11 anos
há 11 anos
há 11 anos
há 11 anos
há 11 anos
há 11 anos
há 11 anos
há 11 anos
há 11 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 -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 gitconf
  154. setup_gitconf(){
  155. _msg "Configure git environment"
  156. if ! command -v git >/dev/null
  157. then
  158. _msg "Git program not found"
  159. return 0
  160. fi
  161. _gc="git config --global"
  162. $_gc user.name '10sr'
  163. $_gc user.email '8slashes+git@gmail.com'
  164. $_gc core.autocrlf false
  165. $_gc core.excludesfile '~/.gitignore'
  166. $_gc color.ui auto
  167. $_gc status.relativePaths false
  168. $_gc status.showUntrackedFiles normal
  169. $_gc log.date iso
  170. $_gc push.default current
  171. command -v xz >/dev/null && \
  172. $_gc tar.txz.command "xz -c"
  173. $_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)\" --date=short -n 499"
  174. $_gc alias.st "status -s -b"
  175. $_gc alias.b "branch"
  176. $_gc alias.sb "show-branch"
  177. $_gc alias.ci "commit --verbose"
  178. $_gc alias.co "checkout"
  179. $_gc alias.cim "commit --verbose -m"
  180. $_gc alias.di "diff --color"
  181. $_gc alias.me "merge --no-ff --stat --verbose"
  182. $_gc alias.ffme "merge --ff-only --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 "diff --quiet"
  192. $_gc alias.echo-ref "for-each-ref --format='%(refname:short)'"
  193. # alias open-branch and close-branch, which will be useful for topic branch
  194. # workflow
  195. _git_open_branch="checkout -b"
  196. _git_close_branch="!sh -cx 'git stash && \
  197. git checkout master && git merge --no-ff --stat --verbose -'"
  198. $_gc alias.open-branch "$_git_open_branch"
  199. $_gc alias.close-branch "$_git_close_branch"
  200. $_gc alias.o "$_git_open_branch"
  201. $_gc alias.c "$_git_close_branch"
  202. $_gc alias.todo "grep -nH -E -i 'todo:|note:|fixme:'"
  203. $_gc alias.snap '! gitdir="`git rev-parse --git-dir`" && : >>"$gitdir"/logs/refs/snapshot && cmt=`git stash create` && test -n "$cmt" && git update-ref refs/snapshot $cmt && echo Snapshot created: $cmt'
  204. #$_gc alias.wc "!git ls-files -z | xargs -0 wc"
  205. # $_gc push.default "simple"
  206. if $iswindows; then
  207. $_gc core.fileMode false
  208. fi
  209. }
  210. #############################
  211. # setup tmux
  212. setup_tmux(){
  213. tmux_conf_local="$HOME/.tmux.conf.local"
  214. _msg "Generate $tmux_conf_local"
  215. case "`hostname`" in
  216. arch-aspireone)
  217. tmux_bg_color=yellow
  218. tmux_fg_color=black
  219. ;;
  220. arch-mba)
  221. tmux_bg_color=cyan
  222. tmux_fg_color=black
  223. ;;
  224. newkiwi)
  225. tmux_bg_color=magenta
  226. tmux_fg_color=white
  227. ;;
  228. debian-vb-win7-opti)
  229. tmux_bg_color=red
  230. tmux_fg_color=white
  231. ;;
  232. *)
  233. tmux_bg_color=green
  234. tmux_fg_color=black
  235. ;;
  236. esac
  237. cat <<__EOC__ >"$tmux_conf_local"
  238. # $tmux_conf_local
  239. # Automatically generated by $0 at $_current_date
  240. set -g status-right "${USER}@$(hostname) | #(tmux -V) "
  241. set -g status-bg $tmux_bg_color
  242. set -g status-fg $tmux_fg_color
  243. set -g mode-bg $tmux_bg_color
  244. set -g mode-fg $tmux_fg_color
  245. set -g pane-active-border-fg $tmux_bg_color
  246. __EOC__
  247. _tmux_conf="$HOME"/.tmux.conf
  248. _msg "Prepare $_tmux_conf"
  249. if test -f "$_tmux_conf"
  250. then
  251. _warn "Tmux config file found. Skipping"
  252. else
  253. echo "source \"$DOTFILES_DIR/tmux.conf\"" >>"$_tmux_conf"
  254. fi
  255. setup_dotfiles tmux.conf
  256. }
  257. ###############################
  258. # setup emacs
  259. setup_emacs(){
  260. _msg "Setup emacs init.el"
  261. _emacs_dir="$HOME"/.emacs.d
  262. mkdir -vp "$_emacs_dir"
  263. _emacs_init_el="$_emacs_dir"/init.el
  264. if test -f "$_emacs_init_el"
  265. then
  266. _warn "Emacs init.el found. Skipping"
  267. else
  268. cat <<__EOC__ >>"$_emacs_init_el"
  269. (and (file-readable-p "$DOTFILES_DIR/emacs.el")
  270. (load-file "$DOTFILES_DIR/emacs.el"))
  271. __EOC__
  272. fi
  273. setup_dotfiles emacs.el
  274. }
  275. ##################################
  276. # setup vim
  277. setup_vim(){
  278. _msg "Setup vimrc"
  279. _vimrc="$HOME"/.vimrc
  280. if test -f "$_vimrc"
  281. then
  282. _warn "Vim rcfile found. Skipping"
  283. else
  284. cat <<__EOC__ >>"$_vimrc"
  285. if filereadable(expand('$DOTFILES_DIR/vimrc'))
  286. source $DOTFILES_DIR/vimrc
  287. endif
  288. __EOC__
  289. fi
  290. setup_dotfiles vimrc
  291. }
  292. ##############################
  293. # setup scripts
  294. _fetch_script(){
  295. # _fetch_script <url> <binname>
  296. url="$1"
  297. name="$2"
  298. dst="$HOME/.local/bin/$name"
  299. test -f "$dst" && return 0
  300. command -v "$name" >/dev/null && return 0
  301. if _download "$url" "$dst"
  302. then
  303. chmod u+x "$dst"
  304. else
  305. test -f "$dst" && rm -- "$dst"
  306. fi
  307. }
  308. setup_scripts(){
  309. _msg "Download some utility scripts"
  310. _fetch_script \
  311. https://gist.github.com/10sr/6852317/raw/colortable16.sh colortable16.sh
  312. _fetch_script \
  313. https://gist.github.com/10sr/6852331/raw/256colors2.pl 256colors2.pl
  314. _fetch_script \
  315. https://github.com/icy/pacapt/raw/ng/pacapt pacapt
  316. _fetch_script \
  317. http://beyondgrep.com/ack-2.12-single-file ack-2.12
  318. }
  319. ################################
  320. # setup darwin
  321. __darwin_set_defaults(){
  322. $isdarwin || return 0
  323. # http://appdrill.net/60641/mac-boot-mute.html
  324. #sudo nvram SystemAudioVolume=%80
  325. # add quit entry in menu
  326. defaults write com.apple.finder QuitMenuItem -bool YES
  327. # show full path on titlebar
  328. defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES
  329. # do not show desktop icons
  330. defaults write com.apple.finder CreateDesktop -boolean false
  331. killall Finder
  332. # disable dashboard
  333. #defaults write com.apple.dashboard mcx-disabled -bool YES
  334. }
  335. __darwin_start_daemon(){
  336. $isdarwin || return 0
  337. test "`launchctl getenv LC_ALL`" = C || sudo launchctl setenv LC_ALL C
  338. if ! (launchctl list | grep com.apple.locate) >/dev/null
  339. then
  340. sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
  341. fi
  342. }
  343. setup_darwin(){
  344. __darwin_set_defaults
  345. __darwin_start_daemon
  346. }
  347. ##########################
  348. # setup windirs
  349. setup_windirs(){
  350. $iswindows || return 0
  351. _msg "Setup some directories for windows environment"
  352. if $iscygwin
  353. then
  354. #__winhome="/cygdrive/c/Users/`whoami`"
  355. __winhome=`cygpath -H`/`whoami`
  356. fi
  357. if test -n "$__winhome" -a -d "$__winhome" -a '!' -e "$HOME/.winhome"
  358. then
  359. _msg Make symlink to "$__winhome" with name "$HOME/.winhome"
  360. ln -s "$__winhome" "$HOME/.winhome"
  361. fi
  362. }
  363. #########################
  364. # setup dirs
  365. setup_dirs(){
  366. _msg Make some direcoties
  367. mkdir -vp "$__homelocal"
  368. mkdir -vp "$__homelocal/bin"
  369. mkdir -vp "$__homevar"
  370. }
  371. ####################################
  372. # setup env
  373. # setup new environment with default options
  374. setup_env(){
  375. setup_shrc_common
  376. setup_dirs
  377. setup_gitconf
  378. setup_tmux
  379. setup_scripts
  380. setup_dotfiles --git
  381. }
  382. #########################
  383. # help and main
  384. help(){
  385. echo "Usage: ./setup.sh <cmd> ..."
  386. echo " or: ./setup.sh help"
  387. echo "Available cmds are: $__setups"
  388. }
  389. main(){
  390. detect_systems
  391. if test -z "$1"
  392. then
  393. help
  394. return 1
  395. fi
  396. _cmd=$1
  397. shift
  398. if test "$_cmd" = help
  399. then
  400. help
  401. return 0
  402. fi
  403. _msg Running setup_$_cmd
  404. setup_$_cmd "$@"
  405. _msg Running setup_$_cmd done
  406. }
  407. main "$@"