diff --git a/install.sh b/install.sh index 17250eb0..e6518042 100755 --- a/install.sh +++ b/install.sh @@ -1,65 +1,18 @@ #!/usr/bin/env bash -BASH_IT="$(cd "$(dirname "$0")" && pwd)" +# bash-it installer -case $OSTYPE in - darwin*) - CONFIG_FILE=.bash_profile - ;; - *) - CONFIG_FILE=.bashrc - ;; -esac - -BACKUP_FILE=$CONFIG_FILE.bak - -if [ -e "$HOME/$BACKUP_FILE" ]; then - echo -e "\033[0;33mBackup file already exists. Make sure to backup your .bashrc before running this installation.\033[0m" >&2 - while true - do - read -e -n 1 -r -p "Would you like to overwrite the existing backup? This will delete your existing backup file ($HOME/$BACKUP_FILE) [y/N] " RESP - case $RESP in - [yY]) - break - ;; - [nN]|"") - echo -e "\033[91mInstallation aborted. Please come back soon!\033[m" - exit 1 - ;; - *) - echo -e "\033[91mPlease choose y or n.\033[m" - ;; - esac - done -fi - -while true -do - read -e -n 1 -r -p "Would you like to keep your $CONFIG_FILE and append bash-it templates at the end? [y/N] " choice - case $choice in - [yY]) - test -w "$HOME/$CONFIG_FILE" && - cp -aL "$HOME/$CONFIG_FILE" "$HOME/$CONFIG_FILE.bak" && - echo -e "\033[0;32mYour original $CONFIG_FILE has been backed up to $CONFIG_FILE.bak\033[0m" - - (sed "s|{{BASH_IT}}|$BASH_IT|" "$BASH_IT/template/bash_profile.template.bash" | tail -n +2) >> "$HOME/$CONFIG_FILE" - echo -e "\033[0;32mBash-it template has been added to your $CONFIG_FILE\033[0m" - break - ;; - [nN]|"") - test -w "$HOME/$CONFIG_FILE" && - cp -aL "$HOME/$CONFIG_FILE" "$HOME/$CONFIG_FILE.bak" && - echo -e "\033[0;32mYour original $CONFIG_FILE has been backed up to $CONFIG_FILE.bak\033[0m" - sed "s|{{BASH_IT}}|$BASH_IT|" "$BASH_IT/template/bash_profile.template.bash" > "$HOME/$CONFIG_FILE" - break - ;; - *) - echo -e "\033[91mPlease choose y or n.\033[m" - ;; - esac -done - -echo -e "\033[0;32mCopied the template $CONFIG_FILE into ~/$CONFIG_FILE, edit this file to customize bash-it\033[0m" +# Show how to use this installer +function show_usage() { + echo -e "\n$0 : Install bash-it" + echo -e "Usage:\n$0 [arguments] \n" + echo "Arguments:" + echo "--help (-h): Display this help message" + echo "--silent (-s): Install default settings without prompting for input"; + echo "--interactive (-i): Interactively choose plugins" + exit 0; +} +# enable a thing function load_one() { file_type=$1 file_to_enable=$2 @@ -73,6 +26,7 @@ function load_one() { fi } +# Interactively enable several things function load_some() { file_type=$1 [ -d "$BASH_IT/$file_type/enabled" ] || mkdir "$BASH_IT/$file_type/enabled" @@ -98,7 +52,102 @@ function load_some() { done } -if [[ "$1" == "--interactive" ]] +# Back up existing profile and create new one for bash-it +function backup_new() { + test -w "$HOME/$CONFIG_FILE" && + cp -aL "$HOME/$CONFIG_FILE" "$HOME/$CONFIG_FILE.bak" && + echo -e "\033[0;32mYour original $CONFIG_FILE has been backed up to $CONFIG_FILE.bak\033[0m" + sed "s|{{BASH_IT}}|$BASH_IT|" "$BASH_IT/template/bash_profile.template.bash" > "$HOME/$CONFIG_FILE" + echo -e "\033[0;32mCopied the template $CONFIG_FILE into ~/$CONFIG_FILE, edit this file to customize bash-it\033[0m" +} + +for param in "$@"; do + shift + case "$param" in + "--help") set -- "$@" "-h" ;; + "--silent") set -- "$@" "-s" ;; + "--interactive") set -- "$@" "-i" ;; + *) set -- "$@" "$param" + esac +done + +OPTIND=1 +while getopts "hsi" opt +do + case "$opt" in + "h") show_usage; exit 0 ;; + "s") silent=true ;; + "i") interactive=true ;; + "?") show_usage >&2; exit 1 ;; + esac +done +shift $(expr $OPTIND - 1) + +if [[ $silent ]] && [[ $interactive ]]; then + echo -e "\033[91mOptions --silent and --interactive are mutually exclusive. Please choose one or the other.\033[m" + exit 1; +fi + +BASH_IT="$(cd "$(dirname "$0")" && pwd)" + +case $OSTYPE in + darwin*) + CONFIG_FILE=.bash_profile + ;; + *) + CONFIG_FILE=.bashrc + ;; +esac + +BACKUP_FILE=$CONFIG_FILE.bak +echo "Installing bash-it" +if [ -e "$HOME/$BACKUP_FILE" ]; then + echo -e "\033[0;33mBackup file already exists. Make sure to backup your .bashrc before running this installation.\033[0m" >&2 + while ! [ $silent ]; do + read -e -n 1 -r -p "Would you like to overwrite the existing backup? This will delete your existing backup file ($HOME/$BACKUP_FILE) [y/N] " RESP + case $RESP in + [yY]) + break + ;; + [nN]|"") + echo -e "\033[91mInstallation aborted. Please come back soon!\033[m" + exit 1 + ;; + *) + echo -e "\033[91mPlease choose y or n.\033[m" + ;; + esac + done +fi + +while ! [ $silent ]; do + read -e -n 1 -r -p "Would you like to keep your $CONFIG_FILE and append bash-it templates at the end? [y/N] " choice + case $choice in + [yY]) + test -w "$HOME/$CONFIG_FILE" && + cp -aL "$HOME/$CONFIG_FILE" "$HOME/$CONFIG_FILE.bak" && + echo -e "\033[0;32mYour original $CONFIG_FILE has been backed up to $CONFIG_FILE.bak\033[0m" + + (sed "s|{{BASH_IT}}|$BASH_IT|" "$BASH_IT/template/bash_profile.template.bash" | tail -n +2) >> "$HOME/$CONFIG_FILE" + echo -e "\033[0;32mBash-it template has been added to your $CONFIG_FILE\033[0m" + break + ;; + [nN]|"") + backup_new + break + ;; + *) + echo -e "\033[91mPlease choose y or n.\033[m" + ;; + esac +done + +if [ $silent ]; then + # backup/new by default + backup_new +fi + +if [[ $interactive ]] && ! [[ $silent ]] ; then for type in "aliases" "plugins" "completion" do