From 755c248f93ac7be0754999aa21a5bff03cad1a77 Mon Sep 17 00:00:00 2001 From: Jon-William Lewis Date: Mon, 20 Jun 2016 08:37:02 -0500 Subject: [PATCH] Major overhaul of install script --- install.sh | 190 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 151 insertions(+), 39 deletions(-) diff --git a/install.sh b/install.sh index c2b8b59..65e8ef8 100755 --- a/install.sh +++ b/install.sh @@ -16,52 +16,164 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -if [ "$1" == "--uninstall" ]; then - rm /bin/sns - rm /usr/share/bash-completion/completions/sns - exit +# Color codes for messages +readonly RED_COLOR='\033[1;31m' +readonly YELLOW_COLOR='\033[1;33m' +readonly RESET_COLOR='\033[0m' + +if [ "$(id -u)" != "0" ]; then + printf "$RED_COLOR!$RESET_COLOR - %s\n"\ + "Please run as root or specify a PREFIX= you have write access to." + exit fi -if [ ! -d ./build ]; then - mkdir build -fi +S="build/sns.sh" -S=build/sns.sh +function build { + bash header.sh + echo -e "\n# Section: Functions" + cat ./src/includes/init_store.sns.sh + cat ./src/includes/verify_store.sns.sh + cat ./src/includes/help.sns.sh + cat ./src/includes/p_header.sh + cat ./src/includes/libencryption.sns.sh + cat ./src/includes/create.sns.sh + cat ./src/includes/delete.sns.sh + cat ./src/includes/edit.sns.sh + cat ./src/includes/print.sns.sh + cat ./src/includes/list.sns.sh + printf "%s\n" "# End Section: Functions" + cat ./src/main/stage1.sns.sh + cat ./src/main/stage2.sns.sh + cat ./src/main/stage3.sns.sh +} -bash header.sh > "$S" -echo -e "\n# Section: Functions" >> "$S" -cat ./src/includes/init_store.sns.sh >> "$S" -cat ./src/includes/verify_store.sns.sh >> "$S" -cat ./src/includes/help.sns.sh >> "$S" -cat ./src/includes/p_header.sh >> "$S" -cat ./src/includes/libencryption.sns.sh >> "$S" -cat ./src/includes/create.sns.sh >> "$S" -cat ./src/includes/delete.sns.sh >> "$S" -cat ./src/includes/edit.sns.sh >> "$S" -cat ./src/includes/print.sns.sh >> "$S" -cat ./src/includes/list.sns.sh >> "$S" -printf "%s\n" "# End Section: Functions" >> "$S" -cat ./src/main/stage1.sns.sh >> "$S" -cat ./src/main/stage2.sns.sh >> "$S" -cat ./src/main/stage3.sns.sh >> "$S" +function install { + mkdir -p "$PREFIX/bin" + mkdir -p "$PREFIX/etc/bash-completion.d" + chmod +x "$S" -chmod +x "$S" -# Install SNS -if [ ! -r "/bin/sns" ]; then - sudo cp "$S" "/bin/sns" -else - if grep -i "simple note system" "/bin/sns" >/dev/null; then - sudo rm /bin/sns - sudo cp "$S" "/bin/sns" + cp -f "$S" "$PREFIX/bin/sns" + cp -f ./build/bash-completion "$PREFIX/etc/bash-completion.d/sns" + +} + +function verify { + if [ ! -r "$PREFIX/bin/sns" ]; then + printf "$RED_COLOR!$RESET_COLOR - %s\n"\ + "could not write to $PREFIX/bin/sns" + exit 15 + elif [ ! -r "$PREFIX"/etc/bash-completion.d/sns ]; then + printf "$RED_COLOR!$RESET_COLOR - %s\n"\ + "could not write to $PREFIX/etc/bash-completion.d/sns" + exit 15 fi + + if [ -z "$(which sns)" ]; then + printf "$YELLOW_COLOR*$RESET_COLOR - %s\n\t$YELLOW_COLOR%s\n$RESET_COLOR %s\n\n"\ + "sns was not found in PATH. Please add"\ + "export PATH=$PREFIX/bin:\$PATH"\ + "to your ~/.bashrc or ~/.bash_profile" + elif [ "$(which sns)" != "$PREFIX/bin/sns" ]; then + printf "$RED_COLOR!$RESET_COLOR - %s\n\t%s\n %s\n\n"\ + "Another installation of sns "\ + "($(which sns))"\ + " is conflicting with this one." + else + printf "$YELLOW_COLOR*$RESET_COLOR - %s\n"\ + "Simple Note System was installed to $PREFIX/bin/sns successfully." + printf "$YELLOW_COLOR*$RESET_COLOR - %s\n"\ + "Bash completion was installed to $PREFIX/etc/bash-completion.d/sns successfully." + + fi + if grep "source $PREFIX/etc/bash-completion.d/sns" < "$BASH_SETTINGS" > /dev/null; then + SNS_BASH_COMPLETION=1; + else + SNS_BASH_COMPLETION=0; + fi + + if [ "$SNS_BASH_COMPLETION" != "1" ]; then + printf "$YELLOW_COLOR*$RESET_COLOR - %s\n\t$YELLOW_COLOR%s\n$RESET_COLOR %s\n\n"\ + "to enable bash completion, add"\ + "source $PREFIX/etc/bash-completion.d/sns"\ + "to your ~/.bashrc or ~/.bash_profile" + else + printf "$YELLOW_COLOR*$RESET_COLOR - %s\n"\ + "Bash completion already enabled." + fi + +} + +function print_help { + printf "\n%s" "usage: ./install [bchiu]" + printf "\n%s" " -b | --build-only : build, but do not install sns" + printf "\n%s" " -c | --clean : clean build directory" + printf "\n%s" " -h | --help : Print this information" + printf "\n%s" " -i | --install : build and install sns" + printf "\n%s" " -u | --uninstall : remove sns from the system (will not remove notes)" + printf "\n" +} + +# Platform detection +if [ -z "$PREFIX" ]; then + case "$(uname)" in + "Darwin" ) + PREFIX=/usr/local/opt/sns + ;; + *) + PREFIX=/opt/sns + ;; + esac fi -# Install Bash completion -if [ ! -r "/usr/share/bash-completion/completions/sns" ]; then - sudo cp "src/bash-completion/sns" "/usr/share/bash-completion/completions/sns" + +# Sanity check +if [ -z "$PREFIX" ]; then + printf "$RED_COLOR!$RESET_COLOR - %s\n"\ + "Failed to determine an install prefix please specify with PREFIX=\n" + exit 10 +fi + +if [ -r "$HOME"/.bash_profile ]; then + BASH_SETTINGS="$HOME/.bash_profile" +elif [ -r "$HOME"/.bashrc ]; then + BASH_SETTINGS="$HOME/.bashrc" else - if grep -i "simple note system" "/usr/share/bash-completion/completions/sns" >/dev/null; then - sudo rm /bin/sns - sudo cp "$S" "/bin/sns" - fi + printf "$RED_COLOR!$RESET_COLOR - %s\n"\ + "Failed to determine bash settings file (e.g. ~/.bashrc)" + exit 13 fi + +#Argument Parsing +for ARG in "$@"; do + case "$ARG" in + -h|--help) + print_help + exit + ;; + -b|--build-only) + if [ ! -d ./build ]; then mkdir ./build; fi + build > "$S"; chmod +x "$S" + cp src/bash-completion/sns ./build/bash-completion + exit + ;; + -c|--clean) + if [ -d ./build ]; then rm -rf build; fi + exit + ;; + -u|--uninstall) + rm -rf $PREFIX + exit + ;; + -i|--install) + if [ ! -d ./build ]; then mkdir ./build; fi + build > "$S" + cp src/bash-completion/sns ./build/bash-completion + install + verify + exit + ;; + esac +done + +print_help exit