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.

154 lines
5.2 KiB

9 years ago
9 years ago
  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. BASEDIR=$(dirname $0)
  11. # Source the tools script
  12. . ${BASEDIR}/tools.sh
  13. #set -x
  14. set -e
  15. # Need to get the source for the script
  16. SOURCE="${BASH_SOURCE[0]}"
  17. while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  18. TARGET="$(readlink "$SOURCE")"
  19. if [[ $TARGET == /* ]]; then
  20. echo "SOURCE '$SOURCE' is an absolute symlink to '$TARGET'"
  21. SOURCE="$TARGET"
  22. else
  23. DIR="$( dirname "$SOURCE" )"
  24. echo "SOURCE '$SOURCE' is a relative symlink to '$TARGET' (relative to '$DIR')"
  25. SOURCE="$DIR/$TARGET" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
  26. fi
  27. done
  28. echo "SOURCE is '$SOURCE'"
  29. RDIR="$( dirname "$SOURCE" )"
  30. DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  31. if [ "$DIR" != "$RDIR" ]; then
  32. echo "DIR '$RDIR' resolves to '$DIR'"
  33. fi
  34. echo "DIR is '$DIR'"
  35. # Step into the directory where thes script is
  36. cd $DIR
  37. # Store the path to the root directory and the reference home directory
  38. GIT_DIR="$(echo `git rev-parse --show-toplevel`)"
  39. cd $GIT_DIR
  40. # Home directory in the git directory
  41. DOTFILES_DIR="$GIT_DIR"
  42. DOTFILES_HOME="$DOTFILES_DIR/home"
  43. # Configure .vimrc
  44. link "$HOME" ".vimrc" "$DOTFILES_HOME/.vimrc"
  45. # Download pathogen if it doesn't exist already
  46. VIM_AUTOLOAD=$HOME/.vim/autoload
  47. mkdir -p $VIM_AUTOLOAD
  48. if [ ! -f "$VIM_AUTOLOAD/pathogen.vim" ]; then
  49. curl -LSso $VIM_AUTOLOAD/pathogen.vim https://tpo.pe/pathogen.vim
  50. fi
  51. # Configure Pathogen Plugins
  52. VIM_BUNDLE=$HOME/.vim/bundle
  53. # Make the bundle directory and it's parents if they don't exist
  54. mkdir -p $VIM_BUNDLE
  55. # A list of vim plugins that are submodules
  56. plugins=($(ls -d $DOTFILES_HOME/.vim/bundle/* | tr -s ' '))
  57. #echo "${plugins[@]}"
  58. # Loop through all of the plugins and install them as links in the bundle directory
  59. for plugin in ${plugins[@]}; do
  60. parts=($(echo "$plugin" | tr '/' ' '))
  61. plugin_name=${parts[-1]}
  62. #echo "$VIM_BUNDLE $plugin_name $plugin"
  63. copy_dir $VIM_BUNDLE $plugin_name $plugin false
  64. done
  65. # Configure emacs
  66. EMACS_CONFIG_HOME=$HOME/.emacs.d
  67. mkdir -p $EMACS_CONFIG_HOME
  68. emacs_configs=($(ls -d $DOTFILES_HOME/.emacs.d/*))
  69. for emacs_config in ${emacs_configs[@]}; do
  70. parts=($(echo "$emacs_config" | tr '/' ' '))
  71. emacs_config_name=${parts[-1]}
  72. if [ -d $emacs_config ]; then
  73. link_dir $EMACS_CONFIG_HOME $emacs_config_name $emacs_config
  74. else
  75. link $EMACS_CONFIG_HOME $emacs_config_name $emacs_config
  76. fi
  77. done
  78. # Configure .gitconfig
  79. link "$HOME" ".gitconfig" "$DOTFILES_HOME/.gitconfig"
  80. # Configure .zshrc
  81. link "$HOME" ".zshrc" "$DOTFILES_HOME/.zshrc"
  82. clone_or_update_git_repo $HOME/.tmux/plugins/tpm https://github.com/tmux-plugins/tpm.git
  83. clone_or_update_git_repo $HOME/.tmux/plugins/tmux-sensible https://github.com/tmux-plugins/tmux-sensible.git
  84. clone_or_update_git_repo $HOME/.tmux/plugins/tmux-resurrect https://github.com/tmux-plugins/tmux-resurrect.git
  85. # Configure .tmux.conf
  86. link "$HOME" ".tmux.conf" "$DOTFILES_HOME/.tmux.conf"
  87. # Copy example ssh config
  88. copy "$HOME" ".ssh/config" "$DOTFILES_HOME/.ssh/config" false false
  89. # Copy yaourtrc config
  90. link "$HOME" ".yaourtrc" "$DOTFILES_HOME/.yaourtrc"
  91. # Copy the systemd user files from .config
  92. SYSTEMD_CONFIG_HOME=$HOME/.config/systemd/user
  93. mkdir -p $SYSTEMD_CONFIG_HOME
  94. systemd_configs=($(ls -d $DOTFILES_HOME/.config/systemd/user/*.service))
  95. for systemd_config in ${systemd_configs[@]}; do
  96. parts=($(echo "$systemd_config" | tr '/' ' '))
  97. systemd_config_name=${parts[-1]}
  98. copy $SYSTEMD_CONFIG_HOME $systemd_config_name $systemd_config true true
  99. done
  100. # Install/update rbenv and plugins
  101. clone_or_update_git_repo $HOME/.rbenv https://github.com/sstephenson/rbenv.git
  102. clone_or_update_git_repo $HOME/.rbenv/plugins/ruby-build https://github.com/sstephenson/ruby-build.git
  103. # <-- EDIT/ADD ABOVE THIS
  104. # This should be the Last section
  105. cd $GIT_DIR
  106. # Create an auto updater for the dotfiles
  107. SOURCE_MD5="$(md5sum scripts/.update_dotfiles.sh | tr -s ' ' | cut -d ' ' -f 1)"
  108. WRITE_NEW_UPDATER=false
  109. # If it doesn't exist, we need to create it
  110. if [ ! -f "$HOME/.update_dotfiles.sh" ]; then
  111. WRITE_NEW_UPDATER=true
  112. else
  113. # Now we need to check if the md5's don't match
  114. CURRENT_MD5="$(cat $HOME/.update_dotfiles.sh | tail -n 1 | tr -s ' ' | cut -d ' ' -f 2)"
  115. # No hash, we need to update the file
  116. if [ -z "$CURRENT_MD5" ]; then
  117. WRITE_NEW_UPDATER=true
  118. else
  119. # need to compare the hashes, if they don't match we need to update
  120. if [ "$CURRENT_MD5" != "$SOURCE_MD5" ]; then
  121. WRITE_NEW_UPDATER=true
  122. fi
  123. fi
  124. fi
  125. if $WRITE_NEW_UPDATER; then
  126. SAFE_PATH="$(escape_path $GIT_DIR)"
  127. cat "scripts/.update_dotfiles.sh" | sed "s/@GIT_DIR@/$SAFE_PATH/" > $HOME/.update_dotfiles.sh
  128. echo "# $SOURCE_MD5" >> $HOME/.update_dotfiles.sh
  129. chmod +x $HOME/.update_dotfiles.sh
  130. fi