install.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/bash
  2. # Go trough all the files available in this folder.
  3. commonFiles=();
  4. for sourceFile in .*; do
  5. # Exclude some files.
  6. if [[ "$sourceFile" == "install_unattended.sh" || "$sourceFile" == "install.sh" ]] || [[ "$sourceFile" == "README.md" ]] || [[ "$sourceFile" == "." ]] || [[ "$sourceFile" == ".." ]] || [[ "$sourceFile" == ".git" ]]; then
  7. continue;
  8. fi;
  9. # Set the target path for the source file.
  10. # We'll port them to the ~/ (home) directory.
  11. targetFile="$HOME/$sourceFile";
  12. # The file already exists.
  13. if [ -e "$targetFile" ]; then
  14. # The source and target point to the same file, continue.
  15. [ "$sourceFile" -ef "$targetFile" ] && continue;
  16. # The content of the file is the same, continue.
  17. diff -rq "$sourceFile" "$targetFile" > /dev/null 2>&1 && continue;
  18. # The source file is another file than our dotfile.
  19. # Store it in the commonFiles array so we can back it up.
  20. commonFiles+=("$sourceFile");
  21. fi;
  22. done;
  23. # Show the files that will be overwritten.
  24. conflictCount=${#commonFiles[@]};
  25. if [ $conflictCount -gt 0 ]; then
  26. # Create a suffix with a timestamp so the user can see which
  27. # files and when they've been backupped.
  28. backupSuffix=".jsVimconfig-$(date +'%Y%m%d-%H%M%S')";
  29. # Print some information about the replacement.
  30. tput setaf 3;
  31. printf "Warning: there are some files that will be overwritten.";
  32. tput sgr0;
  33. echo " Your files will be given the suffix $backupSuffix and will be" \
  34. "replaced by symlinks to the Dotfiles:";
  35. # Print the conflict files.
  36. for sourceFile in "${commonFiles[@]}"; do
  37. targetFile="$HOME/$sourceFile";
  38. echo "Yours: $targetFile";
  39. echo "Dotfiles: $sourceFile";
  40. done;
  41. # Make sure the user knows that we'll be replacing the old files.
  42. read -p "Would you like to overwrite the current files? (yes|no): ";
  43. case "$(tr '[:upper:]' '[:lower:]' <<< "$REPLY")" in
  44. 'yes'|'y')
  45. # The user is sure, continue.
  46. ;;
  47. 'no'|'n')
  48. # The user isn't sure, abort.
  49. echo 'Aborting.';
  50. exit 1;
  51. ;;
  52. *)
  53. # The user didn't type yes/no.
  54. echo 'Invalid answer. Assumed "no". Installation aborted.';
  55. exit 1;
  56. esac;
  57. fi;
  58. # Rename the conflicting files as backup.
  59. for sourceFile in "${commonFiles[@]}"; do
  60. targetFile="$HOME/$sourceFile";
  61. mv -v "$targetFile" "$targetFile$backupSuffix";
  62. done;
  63. # Symlink the files in the home dir by those in the dotfiles repo.
  64. for sourceFile in .*; do
  65. # Exclude some files.
  66. if [[ "$sourceFile" == "dotfiles-002.png" || "$sourceFile" == "dotfiles-001.png" || "$sourceFile" == "install.sh" ]] || [[ "$sourceFile" == "README.md" ]] || [[ "$sourceFile" == "." ]] || [[ "$sourceFile" == ".." ]] || [[ "$sourceFile" == ".git" ]]; then
  67. continue;
  68. fi;
  69. targetFile="$HOME/$sourceFile";
  70. # Delete the old file and replace it by the symlink.
  71. rm -rf "$targetFile" &&
  72. ln -vs "$PWD/$sourceFile" "$targetFile";
  73. done;
  74. # This will add a tag to our prompt, so we know which environment we're in.
  75. touch ~/.bash_environment;
  76. read -p "Please, enter your environment: ";
  77. echo -e "export __prompt_environment='[$REPLY] '" | tee ~/.bash_environment > /dev/null;
  78. echo "Done.";