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.

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