ihsec.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. # source: https://github.com/daedreth/ihsec
  4. CONFIGS_DIR=$HOME'/.ihsec/'
  5. # Uncomment to use this script from the same folder as emacs configs
  6. #CONFIGS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  7. echo " Configuration directory: "$CONFIGS_DIR
  8. CONFIGDIRS="$CONFIGS_DIR/*/"
  9. EMACSDIR=$HOME'/.emacs.d'
  10. function displayHelp
  11. {
  12. echo -e " Usage:\n\t'ihsec' - To view this help menu.\n\t'ihsec list' - To view a list of available configurations."
  13. echo -e "\t'ihsec set <config>' - To set a configuration.\n\t'ihsec del <config>' - To delete a config."
  14. echo -e "\t'ihsec install <url_to_git_repo> <name_for_the_config>' - To install a configuration via git."; exit 1
  15. }
  16. function displayError
  17. {
  18. echo -e " Something went wrong!\n Ensure ~/.emacs.d is a symlink or does not exist!"; exit $?
  19. }
  20. function displayArgError
  21. {
  22. echo -e " Wrong number of arguments entered.\n Use 'ihsec help' or 'ihsec' to learn about the usage."; exit 1
  23. }
  24. if [ "$#" -eq 0 ]; then displayHelp; fi
  25. case "$1" in
  26. list)
  27. if [ ! "$#" -eq 1 ]; then displayArgError; fi
  28. CURR_CONFIG=$(basename $(readlink -f $HOME/.emacs.d/))
  29. echo -e " Available configurations:"
  30. for DIR in $CONFIGDIRS; do
  31. if [[ -d $DIR ]]; then
  32. if [[ $DIR == *"$CURR_CONFIG"* ]]; then echo -en "->"; else echo -en " "; fi
  33. echo -en " \u2022" $(basename $DIR)"\n"
  34. fi
  35. done
  36. ;;
  37. set)
  38. if [ ! "$#" -eq 2 ]; then displayArgError; fi
  39. if [ -d "$CONFIGS_DIR/$2" ]; then
  40. unlink $EMACSDIR &> /dev/null
  41. ln -s "$CONFIGS_DIR/$2" $EMACSDIR
  42. if [[ $? != 0 ]]; then
  43. displayError
  44. else
  45. echo "Configuration $2 set!"
  46. fi
  47. else
  48. echo "Invalid configuration name, ensure it exists!"; exit 1
  49. fi
  50. ;;
  51. del)
  52. if [ ! "$#" -eq 2 ]; then displayArgError; fi
  53. if [ -d "$CONFIGS_DIR/$2" ]; then
  54. read -rsn1 -p "Are you sure? This can not be undone! [y/n]: " yn
  55. case $yn in
  56. [Yy]* )
  57. rm -rf "$CONFIGS_DIR/$2"
  58. if [[ $? != 0 ]]; then
  59. displayError
  60. else
  61. echo -e "\nConfiguration $2 removed!"
  62. fi
  63. ;;
  64. [Nn]* )
  65. echo -e "\nExiting..."
  66. exit 1
  67. ;;
  68. * ) echo -e "\nPlease answer 'y' or 'n'." ;;
  69. esac
  70. else
  71. echo " Invalid configuration name, ensure it exists!"
  72. exit 1
  73. fi
  74. ;;
  75. install)
  76. if [ ! "$#" -eq 3 ]; then displayArgError; fi
  77. echo "Installing $3 from $2..."
  78. git clone $2 "$CONFIGS_DIR/$3" &> /dev/null
  79. if [[ $? != 0 ]]; then
  80. echo "Something went wrong, ensure your connection is stable and the link is valid."
  81. else
  82. echo "Installed $3 successfully!"
  83. fi
  84. ;;
  85. help)
  86. if [ ! "$#" -eq 1 ]; then displayArgError; fi; displayHelp
  87. ;;
  88. *)
  89. echo -e " Unknown command entered.\n Use 'ihsec help' or 'ihsec' to learn about the usage."
  90. esac