180 lines
5.5 KiB
Bash
Executable File
180 lines
5.5 KiB
Bash
Executable File
#!/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 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
|
|
|
|
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
|
|
}
|
|
|
|
function install {
|
|
mkdir -p "$PREFIX/bin"
|
|
mkdir -p "$PREFIX/etc/bash-completion.d"
|
|
chmod +x "$S"
|
|
|
|
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
|
|
|
|
# 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
|
|
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
|