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.

158 lines
5.3 KiB

9 years ago
9 years ago
  1. #!/usr/bin/env bash
  2. #
  3. # A script to convert the local system to use my dotfiles my local dotfiles over
  4. # the provided ones already on the system. This script will backup any of the
  5. # existing dot files, before linking to the ones in this repository.
  6. #
  7. # This script must be able to operate cleanly on an already bootstraped environment
  8. # so as to update the system cleanly to new versions of the dotfiles repository.
  9. #
  10. BASEDIR=$(dirname $0)
  11. # Source the tools script
  12. . ${BASEDIR}/tools.sh
  13. #set -x
  14. set -e
  15. # Need to get the source for the script
  16. SOURCE="${BASH_SOURCE[0]}"
  17. while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  18. TARGET="$(readlink "$SOURCE")"
  19. if [[ $TARGET == /* ]]; then
  20. echo "SOURCE '$SOURCE' is an absolute symlink to '$TARGET'"
  21. SOURCE="$TARGET"
  22. else
  23. DIR="$( dirname "$SOURCE" )"
  24. echo "SOURCE '$SOURCE' is a relative symlink to '$TARGET' (relative to '$DIR')"
  25. SOURCE="$DIR/$TARGET" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
  26. fi
  27. done
  28. echo "SOURCE is '$SOURCE'"
  29. RDIR="$( dirname "$SOURCE" )"
  30. DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  31. if [ "$DIR" != "$RDIR" ]; then
  32. echo "DIR '$RDIR' resolves to '$DIR'"
  33. fi
  34. echo "DIR is '$DIR'"
  35. # Step into the directory where thes script is
  36. cd $DIR
  37. # Store the path to the root directory and the reference home directory
  38. GIT_DIR="$(echo `git rev-parse --show-toplevel`)"
  39. cd $GIT_DIR
  40. # Home directory in the git directory
  41. DOTFILES_DIR="$GIT_DIR"
  42. DOTFILES_HOME="$DOTFILES_DIR/home"
  43. # Configure .vimrc
  44. link "$HOME" ".vimrc" "$DOTFILES_HOME/.vimrc"
  45. # Download pathogen if it doesn't exist already
  46. VIM_AUTOLOAD=$HOME/.vim/autoload
  47. mkdir -p $VIM_AUTOLOAD
  48. if [ ! -f "$VIM_AUTOLOAD/pathogen.vim" ]; then
  49. curl -LSso $VIM_AUTOLOAD/pathogen.vim https://tpo.pe/pathogen.vim
  50. fi
  51. # Configure Pathogen Plugins
  52. VIM_BUNDLE=$HOME/.vim/bundle
  53. # Make the bundle directory and it's parents if they don't exist
  54. mkdir -p $VIM_BUNDLE
  55. # A list of vim plugins that are submodules
  56. plugins=($(ls -d $DOTFILES_HOME/.vim/bundle/* | tr -s ' '))
  57. #echo "${plugins[@]}"
  58. # Loop through all of the plugins and install them as links in the bundle directory
  59. for plugin in ${plugins[@]}; do
  60. parts=($(echo "$plugin" | tr '/' ' '))
  61. plugin_name=${parts[-1]}
  62. #echo "$VIM_BUNDLE $plugin_name $plugin"
  63. copy_dir $VIM_BUNDLE $plugin_name $plugin false
  64. done
  65. # Configure emacs
  66. EMACS_CONFIG_HOME=$HOME/.emacs.d
  67. mkdir -p $EMACS_CONFIG_HOME
  68. emacs_configs=($(ls -d $DOTFILES_HOME/.emacs.d/*))
  69. for emacs_config in ${emacs_configs[@]}; do
  70. parts=($(echo "$emacs_config" | tr '/' ' '))
  71. emacs_config_name=${parts[-1]}
  72. if [ -d $emacs_config ]; then
  73. link_dir $EMACS_CONFIG_HOME $emacs_config_name $emacs_config
  74. else
  75. link $EMACS_CONFIG_HOME $emacs_config_name $emacs_config
  76. fi
  77. done
  78. # Configure .gitconfig
  79. link "$HOME" ".gitconfig" "$DOTFILES_HOME/.gitconfig"
  80. # Configure .zshrc
  81. link "$HOME" ".zshrc" "$DOTFILES_HOME/.zshrc"
  82. # zsh plugins
  83. clone_or_update_git_repo $HOME/.oh-my-zsh/custom/plugins/zsh-completions https://github.com/zsh-users/zsh-completions
  84. # Tmux plugins
  85. clone_or_update_git_repo $HOME/.tmux/plugins/tpm https://github.com/tmux-plugins/tpm.git
  86. clone_or_update_git_repo $HOME/.tmux/plugins/tmux-sensible https://github.com/tmux-plugins/tmux-sensible.git
  87. clone_or_update_git_repo $HOME/.tmux/plugins/tmux-resurrect https://github.com/tmux-plugins/tmux-resurrect.git
  88. # Configure .tmux.conf
  89. link "$HOME" ".tmux.conf" "$DOTFILES_HOME/.tmux.conf"
  90. # Copy example ssh config
  91. copy "$HOME" ".ssh/config" "$DOTFILES_HOME/.ssh/config" false false
  92. # Copy yaourtrc config
  93. link "$HOME" ".yaourtrc" "$DOTFILES_HOME/.yaourtrc"
  94. # Copy the systemd user files from .config
  95. SYSTEMD_CONFIG_HOME=$HOME/.config/systemd/user
  96. mkdir -p $SYSTEMD_CONFIG_HOME
  97. systemd_configs=($(ls -d $DOTFILES_HOME/.config/systemd/user/*.service))
  98. for systemd_config in ${systemd_configs[@]}; do
  99. parts=($(echo "$systemd_config" | tr '/' ' '))
  100. systemd_config_name=${parts[-1]}
  101. copy $SYSTEMD_CONFIG_HOME $systemd_config_name $systemd_config true true
  102. done
  103. # Install/update rbenv and plugins
  104. clone_or_update_git_repo $HOME/.rbenv https://github.com/sstephenson/rbenv.git
  105. clone_or_update_git_repo $HOME/.rbenv/plugins/ruby-build https://github.com/sstephenson/ruby-build.git
  106. # <-- EDIT/ADD ABOVE THIS
  107. # This should be the Last section
  108. cd $GIT_DIR
  109. # Create an auto updater for the dotfiles
  110. SOURCE_MD5="$(md5sum scripts/.update_dotfiles.sh | tr -s ' ' | cut -d ' ' -f 1)"
  111. WRITE_NEW_UPDATER=false
  112. # If it doesn't exist, we need to create it
  113. if [ ! -f "$HOME/.update_dotfiles.sh" ]; then
  114. WRITE_NEW_UPDATER=true
  115. else
  116. # Now we need to check if the md5's don't match
  117. CURRENT_MD5="$(cat $HOME/.update_dotfiles.sh | tail -n 1 | tr -s ' ' | cut -d ' ' -f 2)"
  118. # No hash, we need to update the file
  119. if [ -z "$CURRENT_MD5" ]; then
  120. WRITE_NEW_UPDATER=true
  121. else
  122. # need to compare the hashes, if they don't match we need to update
  123. if [ "$CURRENT_MD5" != "$SOURCE_MD5" ]; then
  124. WRITE_NEW_UPDATER=true
  125. fi
  126. fi
  127. fi
  128. if $WRITE_NEW_UPDATER; then
  129. SAFE_PATH="$(escape_path $GIT_DIR)"
  130. cat "scripts/.update_dotfiles.sh" | sed "s/@GIT_DIR@/$SAFE_PATH/" > $HOME/.update_dotfiles.sh
  131. echo "# $SOURCE_MD5" >> $HOME/.update_dotfiles.sh
  132. chmod +x $HOME/.update_dotfiles.sh
  133. fi