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.

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