setup.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #!/bin/sh
  2. # Setup Script - Modeline and Notes {{{
  3. # vim: set sw=2 ts=2 sts=2 et tw=78 foldmarker={{{,}}} foldlevel=0 foldmethod=marker spell:
  4. #
  5. # Automated Setup Script - cinaeco/dotfiles
  6. #
  7. # This script creates appropriate symlinks in the home folder and pulls
  8. # extra software and plugins where available.
  9. #
  10. # Run from where it's located within dotfiles directory i.e. `./setup.sh`
  11. #
  12. # }}}
  13. # Script Helper Functions {{{
  14. #
  15. # These are mostly taken from spf13-vim. They are primarily here to setup
  16. # vundle beforehand, to avoid the chicken-and-egg issue.
  17. #
  18. debug_mode='0'
  19. # Backup Folder for old configuration files {{{
  20. #
  21. # The setup script is non-destructive, and can be run repeatedly - all old
  22. # config files or symlinks from previous runs can be recovered from
  23. # timestamped temp folders.
  24. #
  25. BACKUP_DIR="/tmp/$(date)"
  26. mkdir "$BACKUP_DIR"
  27. # }}}
  28. # Symlink Attempt
  29. lnif() {
  30. if [ -e "$1" ]; then
  31. ln -sf "$1" "$2"
  32. fi
  33. ret="$?"
  34. debug
  35. }
  36. # Backup files to backup folder
  37. backup() {
  38. [ -f "$1" ] && mv "$1" "$BACKUP_DIR"
  39. ret="$?"
  40. debug
  41. }
  42. # Backup directories to backup folder
  43. backup_dir() {
  44. [ -d "$1" ] && mv "$1" "$BACKUP_DIR"
  45. ret="$?"
  46. debug
  47. }
  48. msg() {
  49. printf '%b\n' "$1" >&2
  50. }
  51. success() {
  52. if [ "$ret" -eq '0' ]; then
  53. msg "\e[32m[+]\e[0m ${1}${2}"
  54. fi
  55. }
  56. debug() {
  57. if [ "$debug_mode" -eq '1' ] && [ "$ret" -gt '1' ]; then
  58. msg "An error occured in function \"${FUNCNAME[$i+1]}\" on line ${BASH_LINENO[$i+1]}"
  59. fi
  60. }
  61. upgrade_repo() {
  62. msg "trying to update $1"
  63. if [ "$1" = "$app_name" ]; then
  64. cd "$HOME/.$app_name-3" &&
  65. git pull origin "$git_branch"
  66. fi
  67. if [ "$1" = "vundle" ]; then
  68. cd "$HOME/.vim/bundle/vundle" &&
  69. git pull origin master
  70. fi
  71. ret="$?"
  72. success "$2"
  73. debug
  74. }
  75. create_vimlinks() {
  76. dotfilesvim="$HOME/dotfiles/vim"
  77. if [ ! -d "$HOME/.tmp-vim" ]; then
  78. mkdir -p "$HOME/.tmp-vim"
  79. fi
  80. vimfolder="$HOME/.tmp-vim"
  81. if [ ! -d "$vimfolder/.vim/bundle" ]; then
  82. mkdir -p "$vimfolder/.vim/bundle"
  83. fi
  84. lnif "$dotfilesvim/vimrc" "$HOME/.vimrc"
  85. lnif "$dotfilesvim/vimrc.bundles" "$HOME/.vimrc.bundles"
  86. lnif "$vimfolder/.vim" "$HOME/.vim"
  87. ret="$?"
  88. success "$1"
  89. debug
  90. }
  91. clone_vundle() {
  92. if [ ! -e "$HOME/.vim/bundle/vundle" ]; then
  93. git clone https://github.com/gmarik/vundle.git "$HOME/.vim/bundle/vundle"
  94. else
  95. upgrade_repo "vundle" "Successfully updated vundle"
  96. fi
  97. ret="$?"
  98. success "$1"
  99. debug
  100. }
  101. setup_vundle() {
  102. system_shell="$SHELL"
  103. export SHELL='/bin/sh'
  104. vim -u "$HOME/.vimrc.bundles" +PluginInstall! +PluginClean! +qall
  105. export SHELL="$system_shell"
  106. success "$1"
  107. debug
  108. }
  109. # }}}
  110. # Make known_hosts file if none {{{
  111. #
  112. # Otherwise, zsh throws an error for our .zshrc
  113. #
  114. if test -f ~/.ssh/known_hosts; then
  115. ret="$?"
  116. success "File 'known_hosts' found"
  117. else
  118. mkdir -p ~/.ssh
  119. touch ~/.ssh/known_hosts
  120. ret="$?"
  121. success "File 'known_hosts' created (blank)"
  122. fi
  123. # }}}
  124. # Get the ack search tool {{{
  125. #
  126. # Requires perl 5.8.8 or higher. Curl may need to have proxy settings.
  127. #
  128. if test -f ~/bin/ack; then
  129. ret="$?"
  130. success "Ack found"
  131. else
  132. mkdir -p ~/bin
  133. curl http://beyondgrep.com/ack-2.04-single-file > ~/bin/ack && chmod 0755 ~/bin/ack
  134. ret="$?"
  135. success "Ack installed from beyondgrep.com"
  136. fi
  137. # }}}
  138. # Git: colorise output, edit with vim {{{
  139. #
  140. # Makes status', diffs and logs much nicer to use.
  141. #
  142. git config --global color.ui true
  143. git config --global core.editor "vim"
  144. success "Git colour and editor setup"
  145. # }}}
  146. # Initialise and clone any submodules {{{
  147. git submodule sync
  148. git submodule update --init
  149. success "Submodules done"
  150. # }}}
  151. # Zsh {{{
  152. backup ~/.zshrc
  153. backup ~/.zshenv
  154. lnif ~/dotfiles/zsh/zshrc ~/.zshrc
  155. lnif ~/dotfiles/zsh/zshenv ~/.zshenv
  156. success "Zsh config linked"
  157. # }}}
  158. # Vimperator {{{
  159. backup_dir ~/.vimperator
  160. backup ~/.vimperatorrc
  161. lnif ~/dotfiles/vimperator ~/.vimperator
  162. lnif ~/dotfiles/vimperator/vimperatorrc ~/.vimperatorrc
  163. success "Vimperator config linked"
  164. # }}}
  165. # Screen {{{
  166. backup ~/.screenrc
  167. lnif ~/dotfiles/screen/screenrc ~/.screenrc
  168. success "Screen config linked"
  169. # }}}
  170. # Tmux {{{
  171. backup ~/.tmux.conf
  172. lnif ~/dotfiles/tmux/tmux.conf ~/.tmux.conf
  173. success "Tmux config linked"
  174. # }}}
  175. # Powerline {{{
  176. # `.config` is a shared config location, so take more care
  177. backup_dir ~/.config/powerline
  178. [ ! -d ~/.config ] && mkdir ~/.config
  179. lnif ~/dotfiles/powerline/config ~/.config/powerline
  180. success "Powerline config linked"
  181. # }}}
  182. # Irssi {{{
  183. backup_dir ~/.irssi
  184. lnif ~/dotfiles/irssi ~/.irssi
  185. success "Irssi config linked"
  186. # }}}
  187. # Ack {{{
  188. backup ~/.ackrc
  189. lnif ~/dotfiles/ack/ackrc ~/.ackrc
  190. success "Ack config linked"
  191. # }}}
  192. # Emacs {{{
  193. backup ~/.emacs
  194. lnif ~/dotfiles/emacs/emacs ~/.emacs
  195. success "Emacs config linked"
  196. # }}}
  197. # ZFS {{{
  198. backup ~/bin/destroy-zfs-auto-snaps
  199. lnif ~/dotfiles/zfs/destroy-zfs-auto-snaps ~/bin
  200. success "ZFS utilities installed"
  201. # }}}
  202. # Vim {{{
  203. backup_dir ~/.vim
  204. backup ~/.vimrc
  205. backup ~/.vimrc.bundles
  206. create_vimlinks "Setting up vim symlinks"
  207. clone_vundle "Successfully cloned vundle"
  208. setup_vundle "Now updating/installing plugins using Vundle"
  209. # }}}
  210. # Success {{{
  211. ret="$?"
  212. success "Setup complete!"
  213. # }}}