skiqqy.xyz/nginxd

67 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
# @Auther skiqqy
# This script is best run as root inside the root of this repo.
# This script allows me to enable/disable certain sections of my website.
help_menu () {
echo "Welcome!"
echo -e "e\t->\tEnable a unit."
echo -e "d\t->\tDisable a unit."
echo -e "u\t->\tSpecify the path to the units directory"
echo -e "h\t->\tShows this message."
}
error () {
# error [message] [exit code]
echo "[ERROR] $1"
exit $2
}
link () {
# links available site to enabled.
# link [unit]
[ ! -f /etc/nginx/sites-available/$1 ] && error "The unit <$1> file cannot be linked" 1
ln -s /etc/nginx/sites-available/$1 /etc/nginx/sites-enabled/$1
}
# Handle arguments and do basic error checking
while getopts "he:d:u:" opt
do
case $opt in
h)
help_menu
exit 0
;;
e | d)
[ ! -z $unit ] && error "Unit <$unit> already set." 1
[[ $opt == "e" ]] && oper=e || oper=d
unit="$OPTARG"
;;
u)
[ -d "$OPTARG" ] && unitsd="$OPTARG" || error "Not <$OPTARG> a valid directory" 1
;;
*)
exit 2
;;
esac
done
# if not specified, then by default we enable everything
[ -z $unit ] && unit="all" && oper="e"
# Available units
units=( git blog wiki irc proj all )
unitsd=${unitsd:="./units"}
# Check that a valid unit was given.
[[ ! " ${units[@]} " =~ " ${unit} " ]] && error "The unit <$unit> does not exist." 1 \
|| echo "Unit <$unit> is supported!"
# Check if we are disabeling, that we have a replacement unit
[ $oper == "d" ] && [[ ! -f "$unitsd/d$unit" ]] && error "Cannot find replacement unit for <$unit>" 1
[ $oper == "e" ] && [[ ! -f "$unitsd/$unit" ]] && error "Cannot find unit file for <$unit>" 1
# Everything is valid, start setting up.
#cp $unitsd/* /etc/nginx/sites-available/
echo TODO: Setup links, and delete links if needed