#!/bin/bash

# # # # # # # # # # # # # # # # # #
# ipcop darkstat addon installer 
# corz at corz dot org - Dec 2008
#

OPTION="$1"
FORCE="no"
if [ "$2" = "f" ]; then
	FORCE="yes"
fi


# install..
cop_install() {

	echo -e "\033[1;32m installing darkstat..\033[0m"

	if [ -e /usr/local/sbin/darkstat ]; then
		echo -e "\n\033[1;31;7;5m error:\033[0m \033[1;31m darkstat is already installed!\033[0m"

		if [ $FORCE = "no" ]; then 
			echo -e "\n\033[1m to uninstall, do.. \033[1;35m$0 -u\033[0m\n"
			exit 1
		else
			echo -e " \n\033[31;5;43m\033[1mPERFORMING FORCED INSTALL..\033[0m"
		fi
	fi
	
	# main executable..
	cp -p darkstat /usr/local/sbin/  > /dev/null 2>&1

	# just in case..
	chmod +x /usr/local/sbin/darkstat

	# rc.darkstat may already exist..
	if [ -e /etc/rc.d/rc.darkstat ]; then
		mv -f /etc/rc.d/rc.darkstat /etc/rc.d/rc.darkstat-old
		chmod 644 /etc/rc.d/rc.darkstat-old
	fi
	cp rc.darkstat /etc/rc.d/rc.darkstat
	chmod +x /etc/rc.d/rc.darkstat

	# have darkstat start at bootup..
	cp -p /etc/rc.d/rc.local /var/log/rc.local.darkstat_backup
	echo "" >> /etc/rc.d/rc.local
	echo "# the following line added by corz ipcop darkstat install.." >> /etc/rc.d/rc.local
	echo "/etc/rc.d/rc.darkstat start" >> /etc/rc.d/rc.local
	echo "" >> /etc/rc.d/rc.local
	
	# fingers crossed..
	/etc/rc.d/rc.darkstat start

	if [ $? = 0 ]; then
		echo -e "\033[1;32m darkstat installed successfully.\033[0m"
	fi
}


# uninstall..
cop_uninstall() {

	echo -e "\n\033[1;32m uninstalling darkstat..\033[0m"

	if [ ! -e /usr/local/sbin/darkstat ]; then
		echo
		echo -e "\033[1;31m darkstat is not installed!\033[0m "
		if [ $FORCE = "no" ]; then
			echo -e "\n\033[1m to install, do.. \033[1;35m$0 -i\033[0m\033[1m first\033[0m\n"
			exit 1
		else
			echo -e " \n\033[31;5;43m\033[1mFORCED UNINSTALL..\033[0m\n"
		fi
	fi

	# bring down the server..
	/etc/rc.d/rc.darkstat stop

	# delete auxilliary files, etc..
	if [ -e /etc/rc.d/rc.local ]; then
		mv /etc/rc.d/rc.local /var/log/rc.local-darkstat_uninstall > /dev/null 2>&1
	fi

	if [ -e /var/log/rc.local.darkstat_backup ]; then
		mv /var/log/rc.local.darkstat_backup  /etc/rc.d/rc.local > /dev/null 2>&1
	else
		# possibly they have force uninstalled (backup gone) put this back, then..
		cp /var/log/rc.local-darkstat_uninstall /etc/rc.d/rc.local > /dev/null 2>&1
	fi

	rm /etc/rc.d/rc.darkstat > /dev/null 2>&1
	rm /usr/local/sbin/darkstat > /dev/null 2>&1

	echo -e "\n\033[1;32m darkstat uninstall completed.\033[0m"
	echo -e "\n\033[1m your original \033[1;35m/etc/rc.d/rc.local\033[0m\033[1m file has been restored"
	echo -e "\n if you have made any changes to it since darkstat was installed"
	echo -e " you can copy those changes back from \033[1;35m/var/log/rc.local.darkstat_uninstall\033[0m"
	echo -e " after which time, you may safely delete that file."
	echo -e " your darkstat database/log directory remains unchanged.\n"
	exit 0;
}


# errors..
errors() {
	echo -e "\n\033[1;33m error: $1 \033[0m"
	options
	exit 1;
}

# helpful text
options() {
	echo -e "\n\033[1;33m choose one of the following options..\033[0m"
	echo -e "\n\033[1m  ./install -i      to install\033[0m"
	echo -e "\033[1m  ./install -i f    to force install\033[0m"
	echo -e "\033[1m  ./install -u      to uninstall\033[0m"
	echo -e "\033[1m  ./install -u f    to force uninstall\033[0m"
	echo -e "\n\033[1;33m you shouldn't ever need the 'force' options\033[0m"
}



# "clear" isn't usually installed on IPCoP boxes!
echo -e "\033[2J"

# get the switch..
#
case $OPTION in

	 -h|h|help|-help)
		echo -e "\n\033[1;33m install help..\033[0m"
		options
		;;

	 -i|i|install)
		cop_install
		;;

	-u|u|uninstall)
		cop_uninstall
		;;
	*)
		errors "\033[01;31m unrecognised option!\033[0m"
		;;
esac


# just in case (reset shell colours)
tput sgr0

# flush caches to disk..
sync

# see ya!
exit 0
