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.

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