Major overhaul of install script

This commit is contained in:
Jon-William Lewis
2016-06-20 08:37:02 -05:00
parent 618d74330e
commit 755c248f93

View File

@@ -16,52 +16,164 @@
# with this program; if not, write to the Free Software Foundation, Inc., # with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
if [ "$1" == "--uninstall" ]; then # Color codes for messages
rm /bin/sns readonly RED_COLOR='\033[1;31m'
rm /usr/share/bash-completion/completions/sns 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 exit
fi fi
if [ ! -d ./build ]; then S="build/sns.sh"
mkdir build
fi
S=build/sns.sh function build {
bash header.sh
bash header.sh > "$S" echo -e "\n# Section: Functions"
echo -e "\n# Section: Functions" >> "$S" cat ./src/includes/init_store.sns.sh
cat ./src/includes/init_store.sns.sh >> "$S" cat ./src/includes/verify_store.sns.sh
cat ./src/includes/verify_store.sns.sh >> "$S" cat ./src/includes/help.sns.sh
cat ./src/includes/help.sns.sh >> "$S" cat ./src/includes/p_header.sh
cat ./src/includes/p_header.sh >> "$S" cat ./src/includes/libencryption.sns.sh
cat ./src/includes/libencryption.sns.sh >> "$S" cat ./src/includes/create.sns.sh
cat ./src/includes/create.sns.sh >> "$S" cat ./src/includes/delete.sns.sh
cat ./src/includes/delete.sns.sh >> "$S" cat ./src/includes/edit.sns.sh
cat ./src/includes/edit.sns.sh >> "$S" cat ./src/includes/print.sns.sh
cat ./src/includes/print.sns.sh >> "$S" cat ./src/includes/list.sns.sh
cat ./src/includes/list.sns.sh >> "$S" printf "%s\n" "# End Section: Functions"
printf "%s\n" "# End Section: Functions" >> "$S" cat ./src/main/stage1.sns.sh
cat ./src/main/stage1.sns.sh >> "$S" cat ./src/main/stage2.sns.sh
cat ./src/main/stage2.sns.sh >> "$S" cat ./src/main/stage3.sns.sh
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 cp -f "$S" "$PREFIX/bin/sns"
sudo cp "$S" "/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 else
if grep -i "simple note system" "/bin/sns" >/dev/null; then printf "$YELLOW_COLOR*$RESET_COLOR - %s\n"\
sudo rm /bin/sns "Simple Note System was installed to $PREFIX/bin/sns successfully."
sudo cp "$S" "/bin/sns" printf "$YELLOW_COLOR*$RESET_COLOR - %s\n"\
"Bash completion was installed to $PREFIX/etc/bash-completion.d/sns successfully."
fi fi
fi if grep "source $PREFIX/etc/bash-completion.d/sns" < "$BASH_SETTINGS" > /dev/null; then
# Install Bash completion SNS_BASH_COMPLETION=1;
if [ ! -r "/usr/share/bash-completion/completions/sns" ]; then
sudo cp "src/bash-completion/sns" "/usr/share/bash-completion/completions/sns"
else else
if grep -i "simple note system" "/usr/share/bash-completion/completions/sns" >/dev/null; then SNS_BASH_COMPLETION=0;
sudo rm /bin/sns
sudo cp "$S" "/bin/sns"
fi 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 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 exit