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.

186 lines
5.6 KiB

  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. # Helper Functions
  11. # Make a backup of the existing file if it exists
  12. # Then link to our supplied dotfile
  13. link() {
  14. # Only make a backup if an existing file is there and is not a link
  15. if [ -f "$1/$2" ] && [ ! -L "$1/$2" ]; then
  16. cp "$1/$2" "$1/$2.old"
  17. echo "Backed up original $2 to $1/$2.old"
  18. fi
  19. ln -sf "$3" "$1/$2"
  20. }
  21. link_dir() {
  22. # Only make a backup if an existing directory is there and is not a link
  23. if [ -d "$1/$2" ] && [ ! -L "$1/$2" ]; then
  24. mv "$1/$2" "$1/$2.old"
  25. echo "Backed up original $2 to $1/$2.old"
  26. fi
  27. # Don't overwrite links
  28. if [ ! -L "$1/$2" ]; then
  29. echo "ln -sf $3 $1/$2"
  30. ln -sf "$3" "$1/$2"
  31. fi
  32. }
  33. copy_dir() {
  34. # Only make a backup if an existing directory is there and is not a link
  35. if [ -d "$1/$2" ] || [ -L "$1/$2" ]; then
  36. # Allow passing true as third param to do backups
  37. backup=true
  38. if [ ! -z "$3" ]; then
  39. backup=$4
  40. fi
  41. if $backup; then
  42. mv "$1/$2" "$1/$2.old"
  43. echo "Backed up original $2 to $1/$2.old"
  44. else
  45. rm -r "$1/$2"
  46. echo "removed original $2"
  47. fi
  48. fi
  49. # Should probably only do this is the source is newer
  50. # Remove the old and copy the new
  51. cp -r "$3" "$1/$2"
  52. }
  53. # Escape the path so we can use it in sed
  54. escape_path() {
  55. local safe="$(echo $1 | sed 's/\//\\\//g')"
  56. echo $safe
  57. }
  58. # Clone a git repository into a location
  59. clone_git_repo() {
  60. local target=$1
  61. local repo=$2
  62. if [ ! -d "$target" ]; then
  63. mkdir -p "$target"
  64. echo "Cloning [$repo] into [$target]"
  65. git clone "$repo" "$target"
  66. else
  67. # Update the repo otherwise
  68. echo "Updating [$repo]"
  69. cur_dir=$PWD
  70. cd "$target"
  71. git checkout .
  72. git pull
  73. cd $cur_dir
  74. fi
  75. }
  76. set -x
  77. set -e
  78. # Need to get the source for the script
  79. SOURCE="${BASH_SOURCE[0]}"
  80. while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  81. TARGET="$(readlink "$SOURCE")"
  82. if [[ $TARGET == /* ]]; then
  83. echo "SOURCE '$SOURCE' is an absolute symlink to '$TARGET'"
  84. SOURCE="$TARGET"
  85. else
  86. DIR="$( dirname "$SOURCE" )"
  87. echo "SOURCE '$SOURCE' is a relative symlink to '$TARGET' (relative to '$DIR')"
  88. SOURCE="$DIR/$TARGET" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
  89. fi
  90. done
  91. echo "SOURCE is '$SOURCE'"
  92. RDIR="$( dirname "$SOURCE" )"
  93. DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  94. if [ "$DIR" != "$RDIR" ]; then
  95. echo "DIR '$RDIR' resolves to '$DIR'"
  96. fi
  97. echo "DIR is '$DIR'"
  98. # Step into the directory where thes script is
  99. cd $DIR
  100. # Store the path to the root directory and the reference home directory
  101. GIT_DIR="$(echo `git rev-parse --show-toplevel`)"
  102. cd $GIT_DIR
  103. # Home directory in the git directory
  104. DOTFILES_DIR="$GIT_DIR"
  105. DOTFILES_HOME="$DOTFILES_DIR/home"
  106. # Configure .vimrc
  107. link "$HOME" ".vimrc" "$DOTFILES_HOME/.vimrc"
  108. # Download pathogen if it doesn't exist already
  109. VIM_AUTOLOAD=$HOME/.vim/autoload
  110. mkdir -p $VIM_AUTOLOAD
  111. if [ ! -f "$VIM_AUTOLOAD/pathogen.vim" ]; then
  112. curl -LSso $VIM_AUTOLOAD/pathogen.vim https://tpo.pe/pathogen.vim
  113. fi
  114. # Configure Pathogen Plugins
  115. VIM_BUNDLE=$HOME/.vim/bundle
  116. # Make the bundle directory and it's parents if they don't exist
  117. mkdir -p $VIM_BUNDLE
  118. # A list of vim plugins that are submodules
  119. plugins=($(ls -d $DOTFILES_HOME/.vim/bundle/* | tr -s ' '))
  120. #echo "${plugins[@]}"
  121. # Loop through all of the plugins and install them as links in the bundle directory
  122. for plugin in ${plugins[@]}; do
  123. parts=($(echo "$plugin" | tr '/' ' '))
  124. plugin_name=${parts[-1]}
  125. #echo "$VIM_BUNDLE $plugin_name $plugin"
  126. copy_dir $VIM_BUNDLE $plugin_name $plugin false
  127. done
  128. # Configure .gitconfig
  129. link "$HOME" ".gitconfig" "$DOTFILES_HOME/.gitconfig"
  130. # Configure .zshrc
  131. link "$HOME" ".zshrc" "$DOTFILES_HOME/.zshrc"
  132. clone_git_repo $HOME/.tmux/plugins/tpm https://github.com/tmux-plugins/tpm.git
  133. clone_git_repo $HOME/.tmux/plugins/tmux-sensible https://github.com/tmux-plugins/tmux-sensible.git
  134. clone_git_repo $HOME/.tmux/plugins/tmux-resurrect https://github.com/tmux-plugins/tmux-resurrect.git
  135. # Configure .tmux.conf
  136. link "$HOME" ".tmux.conf" "$DOTFILES_HOME/.tmux.conf"
  137. cd $GIT_DIR
  138. # Create an auto updater for the dotfiles
  139. SOURCE_MD5="$(md5sum scripts/.update_dotfiles.sh | tr -s ' ' | cut -d ' ' -f 1)"
  140. WRITE_NEW_UPDATER=false
  141. # If it doesn't exist, we need to create it
  142. if [ ! -f "$HOME/.update_dotfiles.sh" ]; then
  143. WRITE_NEW_UPDATER=true
  144. else
  145. # Now we need to check if the md5's don't match
  146. CURRENT_MD5="$(cat $HOME/.update_dotfiles.sh | tail -n 1 | tr -s ' ' | cut -d ' ' -f 2)"
  147. # No hash, we need to update the file
  148. if [ -z "$CURRENT_MD5" ]; then
  149. WRITE_NEW_UPDATER=true
  150. else
  151. # need to compare the hashes, if they don't match we need to update
  152. if [ "$CURRENT_MD5" != "$SOURCE_MD5" ]; then
  153. WRITE_NEW_UPDATER=true
  154. fi
  155. fi
  156. fi
  157. if $WRITE_NEW_UPDATER; then
  158. SAFE_PATH="$(escape_path $GIT_DIR)"
  159. cat "scripts/.update_dotfiles.sh" | sed "s/@GIT_DIR@/$SAFE_PATH/" > $HOME/.update_dotfiles.sh
  160. echo "# $SOURCE_MD5" >> $HOME/.update_dotfiles.sh
  161. chmod +x $HOME/.update_dotfiles.sh
  162. fi