Cleanup
This commit is contained in:
220
install.sh
220
install.sh
@@ -19,19 +19,33 @@
|
||||
# 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'
|
||||
|
||||
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
|
||||
|
||||
# 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
|
||||
|
||||
S="build/sns.sh"
|
||||
|
||||
function build {
|
||||
bash header.sh
|
||||
echo -e "\n# Section: Functions"
|
||||
function sns_out {
|
||||
# Compile SNS from source files and print the resulting script to stdout
|
||||
cat ./src/includes/header.sh
|
||||
printf "%s\n" "# Section: Functions"
|
||||
cat ./src/includes/init_store.sns.sh
|
||||
cat ./src/includes/verify_store.sns.sh
|
||||
cat ./src/includes/help.sns.sh
|
||||
@@ -47,129 +61,173 @@ function build {
|
||||
cat ./src/main/stage2.sns.sh
|
||||
cat ./src/main/stage3.sns.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"
|
||||
cp -f src/bash-completion/sns ./build/bash-completion
|
||||
}
|
||||
|
||||
function install {
|
||||
mkdir -p "$PREFIX/bin"
|
||||
mkdir -p "$PREFIX/etc/bash-completion.d"
|
||||
chmod +x "$S"
|
||||
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)
|
||||
|
||||
cp -f "$S" "$PREFIX/bin/sns"
|
||||
cp -f ./build/bash-completion "$PREFIX/etc/bash-completion.d/sns"
|
||||
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 "$PREFIX/bin/sns" ]; then
|
||||
if [ ! -r "$INSTALL_DIR/bin/sns" ]; then
|
||||
printf "$RED_COLOR!$RESET_COLOR - %s\n"\
|
||||
"could not write to $PREFIX/bin/sns"
|
||||
"could not write to $INSTALL_DIR/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."
|
||||
|
||||
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 grep "source $PREFIX/etc/bash-completion.d/sns" < "$BASH_SETTINGS" > /dev/null; then
|
||||
|
||||
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 "$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"
|
||||
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 "\n%s" "usage: ./install [bchiu]"
|
||||
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"
|
||||
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"
|
||||
}
|
||||
|
||||
# Platform detection
|
||||
if [ -z "$PREFIX" ]; then
|
||||
case "$(uname)" in
|
||||
"Darwin" )
|
||||
PREFIX=/usr/local/opt/sns
|
||||
;;
|
||||
*)
|
||||
PREFIX=/opt/sns
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
|
||||
# Sanity check
|
||||
if [ -z "$PREFIX" ]; then
|
||||
if [ -z "$INSTALL_DIR" ]; then
|
||||
printf "$RED_COLOR!$RESET_COLOR - %s\n"\
|
||||
"Failed to determine an install prefix please specify with PREFIX=\n"
|
||||
"Failed to guess optimal install directory please specify with INSTALL_DIR=\n"
|
||||
exit 10
|
||||
fi
|
||||
|
||||
if [ -r "$HOME"/.bash_profile ]; then
|
||||
BASH_SETTINGS="$HOME/.bash_profile"
|
||||
elif [ -r "$HOME"/.bashrc ]; then
|
||||
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
|
||||
|
||||
#Argument Parsing
|
||||
# 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
|
||||
-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
|
||||
build_sns
|
||||
printf "\n"
|
||||
exit
|
||||
;;
|
||||
-c|--clean)
|
||||
if [ -d ./build ]; then rm -rf build; fi
|
||||
clean
|
||||
exit
|
||||
;;
|
||||
-u|--uninstall)
|
||||
rm -rf $PREFIX
|
||||
check_install_dir_perms
|
||||
if [ "$WRITE" == "1" ]; then
|
||||
rm -rf "$INSTALL_DIR"
|
||||
else
|
||||
sudo rm -rf "$INSTALL_DIR"
|
||||
fi
|
||||
exit
|
||||
;;
|
||||
-i|--install)
|
||||
if [ ! -d ./build ]; then mkdir ./build; fi
|
||||
build > "$S"
|
||||
cp src/bash-completion/sns ./build/bash-completion
|
||||
install
|
||||
build_sns
|
||||
install_sns
|
||||
verify
|
||||
printf "\n"
|
||||
exit
|
||||
;;
|
||||
-h|--help)
|
||||
print_help
|
||||
exit
|
||||
;;
|
||||
esac
|
||||
|
||||
Reference in New Issue
Block a user