#!/bin/bash # Simple Note System - Install Script # Copyright (C) 2016, Jon Lewis # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # Color codes for messages readonly RED_COLOR='\033[1;31m' readonly YELLOW_COLOR='\033[1;33m' readonly BLUE_COLOR='\033[1;34m' readonly RESET_COLOR='\033[0m' # Set variables for header PROD_STR="Simple Note System" PROD_SUB="Install Script" # Set default build directory if none specified if [ ! -d "$BUILD_ROOT" ]; then BUILD_ROOT="build"; fi # Set default install prefix according to platform if [ -z "$INSTALL_DIR" ]; then case "$(uname)" in "Darwin" ) INSTALL_ROOT=/usr/local/opt ;; *) INSTALL_ROOT=/opt ;; esac INSTALL_DIR="$INSTALL_ROOT/sns" fi function sns_out { # Compile SNS from source files and print the resulting script to stdout cat ./src/header.src.sh printf "%s\n" "# Section: Functions" awk 'NR>=17' ./src/libEncryption.src.sh awk 'NR>=17' ./src/libStore.src.sh awk 'NR>=17' ./src/libSNS.src.sh printf "%s\n" "# End Section: Functions" awk 'NR>=17' ./src/sns.src.sh } function clean { if [ -d "$BUILD_ROOT" ]; then rm -rf "$BUILD_ROOT"; fi } function build_sns { clean if [ ! -d ."$BUILD_ROOT" ]; then mkdir $BUILD_ROOT; fi sns_out > "$BUILD_ROOT/sns" chmod +x "$BUILD_ROOT/sns" cp -f ./src/bash-completion.src.sh ./build/bash-completion } function check_install_dir_perms { STATS=( $(stat -c "%a %u %g" "$INSTALL_ROOT")) O=$(echo "${STATS[1]}" | cut -c 1) G=$(echo "${STATS[1]}" | cut -c 2) E=$(echo "${STATS[1]}" | cut -c 3) USER=$(id -u) GROUP=$(id -g) if [ "${STATS[2]}" == "$USER" ] && [ "$O" == "7" ]; then WRITE="1"; elif [ "${STATS[3]}" == "$GROUP" ] && [ "$G" == "7" ]; then WRITE="1"; elif [ "$E" == "7" ]; then WRITE="1"; else WRITE="0"; fi if [ "$(id -u)" == "0" ]; then WRITE=1; fi } function install_sns { # Make sure we can write to "$INSTALL_DIR" check_install_dir_perms if [ "$WRITE" == "0" ]; then printf "$RED_COLOR!$RESET_COLOR - %s %s\n"\ "Superuser permissions required to install to" "$INSTALL_DIR." printf "$YELLOW_COLOR!$RESET_COLOR - %s\n"\ "Will use sudo as necessary." USE_SUDO="sudo" else USE_SUDO="" fi # Prepare target environment "$USE_SUDO" mkdir -p "$INSTALL_DIR" "$USE_SUDO" mkdir -p "$INSTALL_DIR/bin" "$USE_SUDO" mkdir -p "$INSTALL_DIR/etc/bash-completion.d" # Install files from "$BUILD_ROOT" to "$INSTALL_DIR" "$USE_SUDO" install -m 0755 "$BUILD_ROOT/sns" "$INSTALL_DIR/bin/sns" "$USE_SUDO" install -m 0644 "$BUILD_ROOT/bash-completion" "$INSTALL_DIR/etc/bash-completion.d/sns" } function verify { if [ ! -r "$INSTALL_DIR/bin/sns" ]; then printf "$RED_COLOR!$RESET_COLOR - %s\n"\ "could not write to $INSTALL_DIR/bin/sns" exit 15 else printf "$YELLOW_COLOR*$RESET_COLOR - %s $BLUE_COLOR%s$RESET_COLOR\n"\ "Simple Note System was installed to" "$INSTALL_DIR/bin/sns." SNS_INSTALLED=1 fi if [ ! -r "$INSTALL_DIR/etc/bash-completion.d/sns" ]; then printf "$RED_COLOR!$RESET_COLOR - %s\n"\ "could not write to $INSTALL_DIR/etc/bash-completion.d/sns" exit 15 else printf "$YELLOW_COLOR*$RESET_COLOR - %s $BLUE_COLOR%s$RESET_COLOR\n"\ "Bash completion was installed to" "$INSTALL_DIR/etc/bash-completion.d/sns." fi DEFAULT_SNS="$(which sns 2>/dev/null)" if [ -n "$DEFAULT_SNS" ] && [ "$DEFAULT_SNS" != "$INSTALL_DIR/bin/sns" ]; then printf "$RED_COLOR!$RESET_COLOR - %s %s %s\n"\ "Another installation of sns"\ "($DEFAULT_SNS)"\ "was detected." fi if [ -z "$DEFAULT_SNS" ]; then printf "\n$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=$INSTALL_DIR/bin:\$PATH"\ " to your ~/.bashrc or ~/.bash_profile" fi if grep "source $INSTALL_DIR/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 "\n$YELLOW_COLOR*$RESET_COLOR - %s\n\t$YELLOW_COLOR%s\n$RESET_COLOR %s\n\n"\ "To enable bash completion, add"\ "source $INSTALL_DIR/etc/bash-completion.d/sns"\ " to your ~/.bashrc or ~/.bash_profile" else printf "$YELLOW_COLOR*$RESET_COLOR - %s\n"\ "Bash completion already enabled." fi if [ "$SNS_INSTALLED" == "1" ]; then printf "$YELLOW_COLOR*$RESET_COLOR - %s $BLUE_COLOR%s$RESET_COLOR %s\n"\ "Installation succeeded. You should run" "sns -i" "now." fi } function print_help { printf " %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\n%s" "Run Variables:" printf "\n %s %s" "BUILD_DIR= " "Directory to build SNS in" printf "\n %s %s" "INSTALL_DIR=" "Directory to install SNS to" printf "\n\n" } # Sanity check if [ -z "$INSTALL_DIR" ]; then printf "$RED_COLOR!$RESET_COLOR - %s\n"\ "Failed to guess optimal install directory please specify with INSTALL_DIR=\n" exit 10 fi if [ -r "$HOME"/.bashrc ]; then BASH_SETTINGS="$HOME/.bashrc" elif [ -r "$HOME"/.bash_profile ]; then BASH_SETTINGS="$HOME/.bash_profile" else printf "$RED_COLOR!$RESET_COLOR - %s\n"\ "Failed to determine bash settings file (e.g. ~/.bashrc)" exit 13 fi # Print header printf "\n %s\n" "$PROD_STR" printf " %s\n" "$PROD_SUB" printf "%s\n\n" "--------------------" # Argument Parsing for ARG in "$@"; do case "$ARG" in -b|--build-only) build_sns printf "\n" exit ;; -c|--clean) clean exit ;; -u|--uninstall) check_install_dir_perms if [ "$WRITE" == "1" ]; then rm -rf "$INSTALL_DIR" else sudo rm -rf "$INSTALL_DIR" fi exit ;; -i|--install) build_sns install_sns verify printf "\n" exit ;; -h|--help) print_help exit ;; esac done print_help exit