#!/bin/bash

# # # # # # # # # # # # # # # # # #
# ipcop fakeidentd addon installer 
# (or at corz.org - April 06
#

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


# install..
cop_install() {

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

	if [ -e /usr/sbin/identd ]; then
		echo
		echo -e " \033[1;31;7;5merror:\033[0m \033[1;31man ident server is already installed!\033[0m"

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

	# just in case..
	chmod +x /usr/sbin/identd

	# it may already exist..
	if [ ! -e /etc/fakeuser ]; then
		cp -p fakeuser /etc/fakeuser
	fi

	cp -p rc.fakeidentd /etc/rc.d/rc.fakeidentd
	cp -p /etc/rc.d/rc.local /var/log/rc.local.backup

	# have fakeidentd start at bootup..
	echo "" >> /etc/rc.d/rc.local
	echo "# the following line added by fakeidentd install.." >> /etc/rc.d/rc.local
	echo "/etc/rc.d/rc.fakeidentd start" >> /etc/rc.d/rc.local
	echo "" >> /etc/rc.d/rc.local

	# fingers crossed..
	echo
	echo -en "\033[1;33m firing up fakeidentd server.."
	/usr/sbin/identd  `cat /etc/fakeuser`

	count=1
	while [ ! -e /var/run/identd.pid ]; do	
		sleep 1
		echo -en "\033[1;33m.\033[0m";
		let count=$[$count + 1]
			if [ $count = 5 ]; then
				echo
				echo -e "\033[1;31m there seems to be an unexpected problem\033[0m"
				echo -e "\033[1;31m it is recommended that you restart your IPCoP box.\033[0m"
				exit 1
			fi
	done


	if [ -e /var/run/identd.pid ]; then
		echo
		echo
		echo -e "\033[1;32m success!\033[0m "
		echo
		echo -e "\033[1m you now have an ident server running on your gateway.\033[0m"
		echo -e "\033[1m you can test it by doing..\033[0m"
		echo
		echo -e "\033[1;35m    telnet $HOSTNAME 113\033[0m"
		echo
		echo -e "\033[1m from another machine. then, at the telnet prompt, input.."
		echo
		echo -e "\033[1;35m    1000,2000 <enter>\033[0m"
		echo
		echo -e "\033[1m you should get a valid (though quite fake) IDENT response.\033[0m"
		echo -e "\033[1m logging in to any decent IRC server is another good IDENT test.\033[0m"
		echo
	fi
}


# uninstall..
cop_uninstall() {

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

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

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

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

	rm /etc/rc.d/rc.fakeidentd > /dev/null 2>&1

	# we leave /etc/fakeuser, the user may have edited this.
	# it is "user data", and therefore sacrosanct.

	# bring down the server..
	if [ -e /var/run/identd.pid ]; then

		kill -TERM `cat /var/run/identd.pid`  > /dev/null 2>&1

		echo
		echo -e "\033[1;33m fakeidentd is shutting down..\033[0m"
		echo -en "\033[1;33m closing connexions (if any)..\033[0m"
		count=1
		while [ -e /var/run/identd.pid ]; do

			echo -en "\033[1;33m.\033[0m";
			sleep 1
			let count=$[$count + 1]
				if [ $count = 5 ]; then
					echo
					echo -e "\033[1;31m there seems to be an unexpected problem :/"
					echo    " it is recommended that you restart your IPCoP box."
					echo
					echo    " fakeidentd will not longer run at bootup, after which, you may"
					echo -e " run this uninstaller again to remove the last of its components.\033[0m"
					echo
					exit 1
				fi
		done
	fi

	echo
	echo
	echo -e "\033[01;32m success!\033[0m"

	# safe to remove this now..
	rm /usr/sbin/identd > /dev/null 2>&1

	echo
	echo -e "\033[1m your original \033[1;35m/etc/rc.d/rc.local\033[0m\033[1m file has been restored"
	echo
	echo    " if you have made any changes to it since fakeidentd was installed"
	echo -e " you can copy those changes back from \033[1;35m/var/log/rc.local.replaced\033[0m"
	echo -e "\033[1m after which time, you may safely delete that file.\033[0m"
	echo

	exit 0;
}


# errors..
errors() {

	echo
	echo -e "\033[1;33m error: $1 \033[0m"
	options
	exit 1;
}

# helpful text
options() {
	echo
	echo -e "\033[1;33m choose one of the following options..\033[0m"
	echo
	echo -e "\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
	echo -e "\033[1;33m you shouldn't ever need the 'force' options\033[0m"
	echo
}


# okay, let's do it..


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

case $OPTION in

	 -h|h|help|-help)
		echo
		echo -e "\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
