#!/bin/ksh
#########################################################################
## Uninstall script for Out Of Office
## Note that ksh is used for this script
#########################################################################

prompt()
{
	printf "%s" ">> "
}

rmfile()
{
	if [ -f $1 ]
	then
		rm $1
	else
		echo "Cannot find $1"
	fi
}

rmdir()
{
	if [ -d $1 ]
	then
		rm -f -R $1
	else
		echo "Cannot find $1"
	fi
}

unlinkfile()
{
	if [ -L $1 ]
	then
		unlink $1
	else
		echo "Cannot find $1"
	fi
}

readini()
{
	cat $1 | grep $2 | cut -d= -f2
}


##
## set PATH
##
PATH=/usr/bin:/usr/sbin:/bin:/sbin
export PATH

##
## determine LARCH
##
Uname="uname"
if [ `$Uname` = AIX ]; then
    LARCH=ibmpow
elif [ `$Uname` = SunOS ]; then
    if [ `$Uname -p` = sparc ]; then
        LARCH=sunspa
    else
                # !!!
        LARCH=sunx86
    fi
elif [ `$Uname` = Linux ]; then
    LARCH=linux
    LINUX_ARCH=`$Uname -m`
fi
if [ -z "$LARCH" ]; then
        # !!!
    echo "Unknown platform `$Uname`"
    exit 1
fi
export LARCH


##
## variables
##
dir_domino_program=
dir_domino_data=

##
## Banner
##
cat <<!
**
**
** To uninstall Out Of Office. Please make sure you have the following:
**
**    login as root.
**    stop all Domino processes.
**
** Press Enter to continue; or Ctrl-C to quit.
**
!

prompt
read continue



##
## check root
##
if [ `id | sed -e 's/^[^(]*(//' -e 's/).*$//'` != "root" ]
then
	echo "You must be root to run the uninstall program.\n"
	exit 1
fi



##
## check if server is running
##
ooo_proceed=0
if [ "`ps -ef | grep /server$ | grep -v grep`" != "" ]
then
	echo "The following Domino process(es) may prevent Out Of Office from uninstalling properly"
	echo ""
	echo "`ps -ef | grep /server$ | grep -v grep`"
	echo ""
	echo "If you want to proceed, press Y and Enter"
	echo "Otherwise, press Enter to quit"
	echo ""
	prompt
	read proceed
	if [ "$proceed" = "y" ]
	then
		ooo_proceed=1
	fi
	if [ "$proceed" = "Y" ]
	then
		ooo_proceed=1
	fi
	if [ $ooo_proceed -ne 1 ]
	then
		echo ""
		echo "You have selected not to uninstall at this time."
		echo ""
		exit 0;
	fi
fi



cat <<!
**
**
** This will uninstall Out Of Office. Are you sure ?
**
** If you want to proceed, press Enter.
** If you want to quit, press Ctrl-C.
**
!

prompt
read continue

##
## proceed
##
if [ -f OOOInstall.log ]
then
	dir_domino_program=`readini OOOInstall.log dir_domino_program`
	dir_domino_data=`readini OOOInstall.log dir_domino_data`
else
	echo "Out Of Office cannot find OOOInstall.log."
	echo "Please make sure you are running the OOOUninstall.sh in the Domino Program directory"
	exit 1;
fi

if [ "$dir_domino_program" = "" ]
then	
	echo "The specified direcotry [$dir_domino_program] does not exist."
	exit 1
fi

if [ "$dir_domino_data" = "" ]
then
	echo "The specified direcotry [$dir_domino_data] does not exists."
	exit 1
fi

##
## remove all files
##


dst=$dir_domino_program/notes/latest/$LARCH/ooo
rmfile $dst

dst=$dir_domino_program/notes/latest/$LARCH/OOO.jar
rmfile $dst

dst=$dir_domino_data/domino/servlet/com/extracomm
rmdir $dst

##
## reset servlets.properties
##

c_startup=OOO
c_servlet=OOO_CLASSES

dominodata=`dirname $dir_domino_data`"/"`basename $dir_domino_data`

tmpfile=/tmp/servlets.properties.000001
rm -f $tmpfile
servletfile=$dominodata/servlets.properties
old_servletfile=$dir_domino_program/_uninst/ooo/servlets.properties.ooo.uninstall

while read line
do
	if [ "$line" != "" ]
	then
		if [ `echo "$line" | cut -c1-1 ` != "#" ]
		then
			pname=`echo "$line" | cut -d= -f1`
			if [ "$pname" != "" ]
			then
				case "$pname"
				in
				"servlets.startup")
					if [ "`echo "$line" | grep "$c_startup"`" != "" ]
					then
						line=`echo "$line" | sed 's/[ ,]*OOO[ ]*//' | sed 's/=[ ,]*/=/'`
						if [ "$line" != "$pname""=" ]
						then
							echo "$line" >> $tmpfile
						fi
					else
						echo "$line" >> $tmpfile
					fi
					;;
				"servlet.OOO.code") 
					;;
				*) 
					echo "$line" >> $tmpfile ;;
				esac
			else
				echo "$line" >> $tmpfile
			fi
		else
			echo "$line" >> $tmpfile
		fi
	fi
done < "$servletfile"

##
## backup the original notes.ini and replace it with the new one
##
cp -p "$servletfile" "$old_servletfile"
if [ -f $tmpfile ]
then
	cp $tmpfile "$servletfile"
	rm -f $tmpfile
else
	rmfile "$servletfile"
fi

##
## reset notes.ini
##

addin=ooo
dominodata=`dirname $dir_domino_data`"/"`basename $dir_domino_data`

tmpfile=/tmp/notes.ini.000001
rm -f $tmpfile
notesini=$dominodata/notes.ini
old_notesini=$dir_domino_program/_uninst/ooo/notes.ini.ooo.uninstall

while read line
do
	if [ "$line" != "" ]
	then
		if [ `echo "$line" | cut -c1-1 ` != "#" ]
		then
			pname=`echo "$line" | cut -d= -f1`
			if [ "$pname" != "" ]
			then
				case "$pname"
				in
				"ServerTasks")
					if [ "`echo "$line" | grep "$addin"`" != "" ]
					then
						echo "$line" | sed 's/[ ,]*ooo[ ]*//' | sed 's/=[ ,]*/=/' >> $tmpfile
					else
						echo "$line" >> $tmpfile
					fi
					;;
				"JavaUserClassesExt")
					if [ "`echo "$line" | grep "$c_servlet"`" != "" ]
					then
						line=`echo "$line" | sed 's/[ ,]*OOO_CLASSES[ ]*//' | sed 's/=[ ,]*/=/'`
						if [ "$line" != "$pname""=" ]
						then
							echo "$line" >> $tmpfile
						fi
					else
						echo "$line" >> $tmpfile
					fi
					;;
				"OOO_CLASSES")
					;;
				"OOOSetup")
					;;
				"OOODirectory")
					;;
				*) 
					echo "$line" >> $tmpfile ;;
				esac
			else
				echo "$line" >> $tmpfile
			fi
		else
			echo "$line" >> $tmpfile
		fi
	fi
done < "$notesini"

##
## backup the original notes.ini and replace it with the new one
##
cp -p "$notesini" "$old_notesini"
cp $tmpfile "$notesini"
rm -f $tmpfile


echo ""
echo "Out Of Office uninstallation completed."
echo ""
echo "It is recommended that you restart the machine for the changes to take effect."
echo "Do you want to restart it now ?"
echo "Press Y and Enter to restart, or other key to quit."
echo ""
prompt
read restart
if [ "$restart" = "y" ]
then
	reboot
	exit 0
fi
if [ "$restart" = "Y" ]
then
	reboot
	exit 0
fi

echo ""
echo "You have selected not to restart at this moment."
echo ""

