#!/bin/sh
#
# Compatibility shim called 'unrealircdctl' based on UnrealIRCd's
# https://github.com/unrealircd/unrealircd/blob/unreal50/unrealircd.in
# to provide proper support for systemd units and to avoid file name
# conflicts between same-named shell script and ELF executable binary,
# which upstream unfortunately both called 'unrealircd'.
#

PID="/run/unrealircd/unrealircd.pid"
if [ "$1" = "start" ] ; then
	echo "Starting UnrealIRCd"
	if [ ! -f /etc/unrealircd/unrealircd.conf ]; then
		echo ""
		echo "The configuration file does not exist (/etc/unrealircd/unrealircd.conf)."
		echo "Create one using the example configuration file, see the documentation:"
		echo "https://www.unrealircd.org/docs/Installing_from_source#Creating_a_configuration_file"
		exit 1
	fi
	systemctl start unrealircd.service
	if [ $? -ne 0 ] ; then
		echo "====================================================="
		echo "UnrealIRCd failed to start. Check above for possible errors."
		echo "If you don't understand the problem, then have a look at our:"
		echo "* FAQ (Frequently Asked Questions): https://www.unrealircd.org/docs/FAQ"
		echo "* Documentation: https://www.unrealircd.org/docs/"
		echo "====================================================="
		exit 1
	fi
	# Now check if we need to create a crash report.
	runuser -l unrealircd -s /bin/sh -c "/usr/bin/unrealircd -R"
elif [ "$1" = "stop" ] ; then
	echo "Stopping UnrealIRCd"
	systemctl -q is-active unrealircd.service
	if [ $? -ne 0 ] ; then
		echo "ERROR: UnrealIRCd is not running"
		exit 1
	fi
	systemctl stop unrealircd.service
	if [ $? -ne 0 ]; then
		echo "ERROR: UnrealIRCd is not running"
		exit 1
	fi
elif [ "$1" = "rehash" ] ; then
	echo "Rehashing UnrealIRCd"
	systemctl -q is-active unrealircd.service
	if [ $? -ne 0 ] ; then
		echo "ERROR: UnrealIRCd is not running"
		exit 1
	fi
	kill -1 `cat $PID 2> /dev/null`
	if [ $? -ne 0 ]; then
		echo "ERROR: UnrealIRCd is not running"
		exit 1
	fi
elif [ "$1" = "restart" ] ; then
	echo "Restarting UnrealIRCd"
	systemctl -q is-active unrealircd.service
	if [ $? -ne 0 ] ; then
		echo "WARNING: UnrealIRCd was not running"
	else
		systemctl restart unrealircd.service
	fi
elif [ "$1" = "croncheck" ] ; then
	systemctl -q is-active unrealircd.service
	if [ $? -ne 0 ] ; then
		echo "UnrealIRCd is not running. Starting now..."
		systemctl start unrealircd.service
	fi
elif [ "$1" = "configtest" ] ; then
	runuser -l unrealircd -s /bin/sh -c "/usr/bin/unrealircd -c"
elif [ "$1" = "module" ] ; then
	echo "ERROR: UnrealIRCd was installed as RPM package, thus the module manager is unavailable!"
	exit 1
elif [ "$1" = "reloadtls" ] ; then
	echo "Reloading SSL/TLS certificates"
	systemctl -q is-active unrealircd.service
	if [ $? -ne 0 ] ; then
		echo "ERROR: UnrealIRCd is not running"
		exit 1
	fi
	kill -USR1 `cat $PID 2> /dev/null`
	if [ $? -ne 0 ]; then
		echo "ERROR: UnrealIRCd is not running"
		exit 1
	fi
elif [ "$1" = "mkpasswd" ] ; then
	runuser -l unrealircd -s /bin/sh -c "/usr/bin/unrealircd -P $2 $3"
elif [ "$1" = "version" ] ; then
	runuser -l unrealircd -s /bin/sh -c "/usr/bin/unrealircd -v"
elif [ "$1" = "gencloak" ] ; then
	runuser -l unrealircd -s /bin/sh -c "/usr/bin/unrealircd -k"
elif [ "$1" = "upgrade-conf" ] ; then
	runuser -l unrealircd -s /bin/sh -c "/usr/bin/unrealircd -U"
elif [ "$1" = "backtrace" ] ; then
	echo "ERROR: UnrealIRCd was installed as RPM package, thus the backtrace functionality is unavailable!"
	exit 1
elif [ "$1" = "spki" -o "$1" = "spkifp" ] ; then
	CERT="/etc/unrealircd/tls/server.cert.pem"
	if [ "$2" != "" ]; then
		CERT="$2"
	fi
	if [ ! -f "$CERT" ]; then
		echo "Could not open certificate: $CERT"
		exit 1
	fi
	openssl x509 -noout -in "$CERT" -pubkey | openssl asn1parse -noout -inform pem -out /var/lib/unrealircd/tmp/tmp.public.key
	HASH="`openssl dgst -sha256 -binary /var/lib/unrealircd/tmp/tmp.public.key | openssl enc -base64`"
	rm -f /var/lib/unrealircd/tmp/tmp.public.key
	if [ "$HASH" = "" ]; then
		echo "Sorry, something went wrong when generating the SPKI fingerprint."
		echo "Is the 'openssl' tool properly installed?"
		exit 1
	fi
	echo "The SPKI fingerprint for certificate $CERT is:"
	echo "$HASH"
	echo ""
	echo "You normally add this password on the other side of the link as:"
	echo "password \"$HASH\" { spkifp; };"
	echo ""
elif [ "$1" = "hot-patch" -o "$1" = "cold-patch" -o "$1" = "upgrade" ] ; then
	METHOD="patch"
	if [ "$1" = "upgrade" ] ; then
		METHOD="upgrade"
	fi
	echo "ERROR: UnrealIRCd was installed as RPM package, thus internal $METHOD functionality is unavailable!"
	DNF="yum"
	if [ -x /usr/bin/dnf ] ; then
		DNF="dnf"
	fi
	echo "NOTICE: Please consider running '$DNF update unrealircd' instead to update UnrealIRCd properly!"
elif [ "$1" = "genlinkblock" ] ; then
	runuser -l unrealircd -s /bin/sh -c "/usr/bin/unrealircd -L"
else
	if [ "$1" = "" ]; then
		echo "This script expects a parameter. Use:"
	else
		echo "Unrecognized parameter '$1'. Use:"
	fi
	echo "unrealircdctl configtest    Test the configuration file"
	echo "unrealircdctl start         Start the IRC Server"
	echo "unrealircdctl stop          Stop (kill) the IRC Server"
	echo "unrealircdctl rehash        Reload the configuration file"
	echo "unrealircdctl reloadtls     Reload the SSL/TLS certificates"
	echo "unrealircdctl restart       Restart the IRC Server (stop+start)"
	echo "unrealircdctl upgrade       Upgrade UnrealIRCd to the latest version"
	echo "unrealircdctl upgrade-conf  Upgrade the configuration file from UnrealIRCd"
	echo "                            3.2.x/4.x to 5.x format"
	echo "unrealircdctl mkpasswd      Hash a password"
	echo "unrealircdctl version       Display the UnrealIRCd version"
	echo "unrealircdctl croncheck     For use in crontab: this checks if the server"
	echo "                            is running. If not, the server is started."
	echo "unrealircdctl genlinkblock  Generate link { } block for the other side."
	echo "unrealircdctl gencloak      Display 3 random cloak keys"
	echo "unrealircdctl spkifp        Display SPKI Fingerprint"
fi
