2020-10-14 21:28:25 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# @Auther skiqqy
|
2020-10-14 21:47:47 +00:00
|
|
|
# This script is best run as root inside the root of this repo.
|
2020-10-14 21:28:25 +00:00
|
|
|
# This script allows me to enable/disable certain sections of my website.
|
|
|
|
|
2020-10-15 08:12:49 +00:00
|
|
|
# Available units
|
|
|
|
units=( git blog wiki irc proj all )
|
|
|
|
|
2020-10-15 08:31:59 +00:00
|
|
|
# Sow the help menu
|
2020-10-14 21:28:25 +00:00
|
|
|
help_menu () {
|
|
|
|
echo "Welcome!"
|
|
|
|
echo -e "e\t->\tEnable a unit."
|
|
|
|
echo -e "d\t->\tDisable a unit."
|
2020-10-14 21:54:48 +00:00
|
|
|
echo -e "u\t->\tSpecify the path to the units directory"
|
2020-10-14 21:28:25 +00:00
|
|
|
echo -e "h\t->\tShows this message."
|
|
|
|
}
|
|
|
|
|
2020-10-15 08:31:59 +00:00
|
|
|
# Display an error message and exit.
|
|
|
|
# error [message] [exit code]
|
2020-10-14 21:28:25 +00:00
|
|
|
error () {
|
|
|
|
echo "[ERROR] $1"
|
|
|
|
exit $2
|
|
|
|
}
|
|
|
|
|
2020-10-15 08:31:59 +00:00
|
|
|
# links available site to enabled.
|
|
|
|
# link [unit]
|
2020-10-14 21:47:47 +00:00
|
|
|
link () {
|
2020-10-15 08:12:49 +00:00
|
|
|
[ ! -f /etc/nginx/sites-available/$1 ] && error "The unit <$1> file cannot be linked." 1
|
2020-10-14 21:47:47 +00:00
|
|
|
ln -s /etc/nginx/sites-available/$1 /etc/nginx/sites-enabled/$1
|
|
|
|
}
|
|
|
|
|
2020-10-15 08:31:59 +00:00
|
|
|
# Enable or disable all units
|
|
|
|
# all [ a | d ]
|
2020-10-15 08:12:49 +00:00
|
|
|
all () {
|
|
|
|
|
|
|
|
# Test and check valid args
|
|
|
|
[ -z $1 ] && error "[ALL] Not given a flag."
|
|
|
|
! [[ $1 == 'e' || $1 == 'd' ]] && error "[ALL] Not given a valid flag."
|
|
|
|
|
|
|
|
REMOVE=()
|
|
|
|
ADD=()
|
|
|
|
if [ $1 == 'e' ]
|
|
|
|
then
|
2020-10-15 08:31:59 +00:00
|
|
|
for u in "${units[@]}"
|
|
|
|
do
|
|
|
|
if [ $u != "all" ]
|
|
|
|
then
|
|
|
|
ADD+=("$u")
|
|
|
|
REMOVE+=("d$u")
|
|
|
|
fi
|
|
|
|
done
|
2020-10-15 08:12:49 +00:00
|
|
|
else
|
2020-10-15 08:31:59 +00:00
|
|
|
for u in "${units[@]}"
|
|
|
|
do
|
|
|
|
if [ $u != "all" ]
|
|
|
|
then
|
|
|
|
REMOVE+=("$u")
|
|
|
|
ADD+=("d$u")
|
|
|
|
fi
|
|
|
|
done
|
2020-10-15 08:12:49 +00:00
|
|
|
fi
|
2020-10-15 08:31:59 +00:00
|
|
|
|
|
|
|
echo "Adding units ( ${ADD[@]} )"
|
|
|
|
echo "Removing units ( ${REMOVE[@]} )"
|
|
|
|
echo TODO: Add and remove units using ADD,REMOVE
|
2020-10-15 08:12:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 22:00:43 +00:00
|
|
|
# Handle arguments and do basic error checking
|
2020-10-14 21:54:48 +00:00
|
|
|
while getopts "he:d:u:" opt
|
2020-10-14 21:28:25 +00:00
|
|
|
do
|
|
|
|
case $opt in
|
|
|
|
h)
|
|
|
|
help_menu
|
2020-10-14 22:00:43 +00:00
|
|
|
exit 0
|
2020-10-14 21:28:25 +00:00
|
|
|
;;
|
|
|
|
e | d)
|
2020-10-14 21:47:47 +00:00
|
|
|
[ ! -z $unit ] && error "Unit <$unit> already set." 1
|
2020-10-14 21:28:25 +00:00
|
|
|
[[ $opt == "e" ]] && oper=e || oper=d
|
|
|
|
unit="$OPTARG"
|
|
|
|
;;
|
2020-10-14 21:54:48 +00:00
|
|
|
u)
|
2020-10-15 08:12:49 +00:00
|
|
|
[ -d "$OPTARG" ] && unitsd="$OPTARG" || error "Not <$OPTARG> a valid directory." 1
|
2020-10-14 21:54:48 +00:00
|
|
|
;;
|
2020-10-14 21:28:25 +00:00
|
|
|
*)
|
|
|
|
exit 2
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2020-10-14 22:00:43 +00:00
|
|
|
# if not specified, then by default we enable everything
|
|
|
|
[ -z $unit ] && unit="all" && oper="e"
|
|
|
|
|
2020-10-15 08:12:49 +00:00
|
|
|
# Unit directory
|
2020-10-14 22:11:44 +00:00
|
|
|
unitsd=${unitsd:="./units"}
|
2020-10-15 07:53:19 +00:00
|
|
|
unitsd=$(echo $unitsd | sed 's:/*$::') # Remove trailing slash
|
2020-10-14 21:47:47 +00:00
|
|
|
|
|
|
|
# Check that a valid unit was given.
|
|
|
|
[[ ! " ${units[@]} " =~ " ${unit} " ]] && error "The unit <$unit> does not exist." 1 \
|
2020-10-15 07:44:10 +00:00
|
|
|
|| echo "Unit <$unit> is supported!"
|
|
|
|
|
|
|
|
# Check if we are disabeling, that we have a replacement unit
|
2020-10-15 08:12:49 +00:00
|
|
|
[ $oper == "d" ] && [[ ! -f "$unitsd/d$unit" ]] && error "Cannot find replacement unit for <$unit>." 1
|
2020-10-15 07:45:26 +00:00
|
|
|
[ $oper == "e" ] && [ $unit != "all" ] && [[ ! -f "$unitsd/$unit" ]] && error "Cannot find unit file for <$unit>" 1
|
2020-10-14 22:11:44 +00:00
|
|
|
|
|
|
|
# Everything is valid, start setting up.
|
2020-10-15 07:44:10 +00:00
|
|
|
echo TODO: Setup links, and delete links if needed
|
2020-10-15 08:12:49 +00:00
|
|
|
|
2020-10-15 08:31:59 +00:00
|
|
|
# Perform tasks
|
|
|
|
if [ $unit == "all"]
|
|
|
|
then
|
|
|
|
all $oper
|
|
|
|
elif [ $oper == 'e' ]
|
2020-10-15 08:12:49 +00:00
|
|
|
then
|
|
|
|
echo Enabling $unit
|
|
|
|
elif [ $oper == 'd' ]
|
|
|
|
then
|
|
|
|
echo Disabling $unit
|
|
|
|
else
|
|
|
|
error "No operation set." 1
|
|
|
|
fi
|