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.

184 lines
5.5 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. git clone "$repo" "$target"
  65. else
  66. # Update the repo otherwise
  67. cur_dir=$PWD
  68. cd "$target"
  69. git checkout .
  70. git pull
  71. cd $cur_dir
  72. fi
  73. }
  74. set -x
  75. set -e
  76. # Need to get the source for the script
  77. SOURCE="${BASH_SOURCE[0]}"
  78. while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  79. TARGET="$(readlink "$SOURCE")"
  80. if [[ $TARGET == /* ]]; then
  81. echo "SOURCE '$SOURCE' is an absolute symlink to '$TARGET'"
  82. SOURCE="$TARGET"
  83. else
  84. DIR="$( dirname "$SOURCE" )"
  85. echo "SOURCE '$SOURCE' is a relative symlink to '$TARGET' (relative to '$DIR')"
  86. SOURCE="$DIR/$TARGET" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
  87. fi
  88. done
  89. echo "SOURCE is '$SOURCE'"
  90. RDIR="$( dirname "$SOURCE" )"
  91. DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  92. if [ "$DIR" != "$RDIR" ]; then
  93. echo "DIR '$RDIR' resolves to '$DIR'"
  94. fi
  95. echo "DIR is '$DIR'"
  96. # Step into the directory where thes script is
  97. cd $DIR
  98. # Store the path to the root directory and the reference home directory
  99. GIT_DIR="$(echo `git rev-parse --show-toplevel`)"
  100. cd $GIT_DIR
  101. # Home directory in the git directory
  102. DOTFILES_DIR="$GIT_DIR"
  103. DOTFILES_HOME="$DOTFILES_DIR/home"
  104. # Configure .vimrc
  105. link "$HOME" ".vimrc" "$DOTFILES_HOME/.vimrc"
  106. # Download pathogen if it doesn't exist already
  107. VIM_AUTOLOAD=$HOME/.vim/autoload
  108. mkdir -p $VIM_AUTOLOAD
  109. if [ ! -f "$VIM_AUTOLOAD/pathogen.vim" ]; then
  110. curl -LSso $VIM_AUTOLOAD/pathogen.vim https://tpo.pe/pathogen.vim
  111. fi
  112. # Configure Pathogen Plugins
  113. VIM_BUNDLE=$HOME/.vim/bundle
  114. # Make the bundle directory and it's parents if they don't exist
  115. mkdir -p $VIM_BUNDLE
  116. # A list of vim plugins that are submodules
  117. plugins=($(ls -d $DOTFILES_HOME/.vim/bundle/* | tr -s ' '))
  118. #echo "${plugins[@]}"
  119. # Loop through all of the plugins and install them as links in the bundle directory
  120. for plugin in ${plugins[@]}; do
  121. parts=($(echo "$plugin" | tr '/' ' '))
  122. plugin_name=${parts[-1]}
  123. #echo "$VIM_BUNDLE $plugin_name $plugin"
  124. copy_dir $VIM_BUNDLE $plugin_name $plugin false
  125. done
  126. # Configure .gitconfig
  127. link "$HOME" ".gitconfig" "$DOTFILES_HOME/.gitconfig"
  128. # Configure .zshrc
  129. link "$HOME" ".zshrc" "$DOTFILES_HOME/.zshrc"
  130. clone_git_repo $HOME/.tmux/plugins/tpm https://github.com/tmux-plugins/tpm.git
  131. clone_git_repo $HOME/.tmux/plugins/tmux-sensible https://github.com/tmux-plugins/tmux-sensible.git
  132. clone_git_repo $HOME/.tmux/plugins/tmux-resurrect https://github.com/tmux-plugins/tmux-resurrect.git
  133. # Configure .tmux.conf
  134. link "$HOME" ".tmux.conf" "$DOTFILES_HOME/.tmux.conf"
  135. cd $GIT_DIR
  136. # Create an auto updater for the dotfiles
  137. SOURCE_MD5="$(md5sum scripts/.update_dotfiles.sh | tr -s ' ' | cut -d ' ' -f 1)"
  138. WRITE_NEW_UPDATER=false
  139. # If it doesn't exist, we need to create it
  140. if [ ! -f "$HOME/.update_dotfiles.sh" ]; then
  141. WRITE_NEW_UPDATER=true
  142. else
  143. # Now we need to check if the md5's don't match
  144. CURRENT_MD5="$(cat $HOME/.update_dotfiles.sh | tail -n 1 | tr -s ' ' | cut -d ' ' -f 2)"
  145. # No hash, we need to update the file
  146. if [ -z "$CURRENT_MD5" ]; then
  147. WRITE_NEW_UPDATER=true
  148. else
  149. # need to compare the hashes, if they don't match we need to update
  150. if [ "$CURRENT_MD5" != "$SOURCE_MD5" ]; then
  151. WRITE_NEW_UPDATER=true
  152. fi
  153. fi
  154. fi
  155. if $WRITE_NEW_UPDATER; then
  156. SAFE_PATH="$(escape_path $GIT_DIR)"
  157. cat "scripts/.update_dotfiles.sh" | sed "s/@GIT_DIR@/$SAFE_PATH/" > $HOME/.update_dotfiles.sh
  158. echo "# $SOURCE_MD5" >> $HOME/.update_dotfiles.sh
  159. chmod +x $HOME/.update_dotfiles.sh
  160. fi