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

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

checkpath()
{
	if [ "$1" = "" ]
	then
		echo "Directory not specified!"
		exit 1
	fi
	if [ "`echo "$1" | cut -c1-1`" != "/" ]
	then
		echo "Please use absolute path!"
		exit 1
	fi
	if [ "`echo "$1" | grep '\.'`" != "" ]
	then
		echo "Please use absolute path!"
		exit 1
	fi
}

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

rmdir()
{
	if [ -d $1 ]
	then
		/bin/rm -r $1
	fi
}

readparam()
{
	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


##
## library extension
##
SHARED_LIB_EXT=so


##
## umask
##
umask 0


##
## set working directory
##
cd `dirname $0`


##
## variables
##
dir_domino_program=
dir_domino_data=
usr_domino=
id_notes_all=
id_notes_usr=
id_notes_group=
licensekey=
dir_domino_data_default=
usr_domino_default=
ooo_proceed=

##
## get parameter from install.param
##
if [ -f "install.param" ]
then
        dir_domino_data_default=`readparam install.param dir_domino_data=`
        usr_domino_default=`readparam install.param usr_domino=`
fi



##
## Banner
##
cat <<!
**
**
** To install 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 install 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 installing 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 install at this time."
		echo ""
		exit 0;
	fi
fi



##
## Welcome
##
cat <<!
**
**
**
** Thank you for using Out Of Office !
** This installation program will guide you through the installation process.
** Please gather the following information before you proceed.
** 1. The path to the Domino program directory
** 2. The path to the Domino data directory
** 3. The Domino UNIX user account
**
** If you want to proceed, press Enter.
** If you want to quit, press Ctrl-C.
**
!

prompt
read continue

dir_domino_program_default=""
if [ -d "/opt/lotus" ]
then
        dir_domino_program_default="/opt/lotus"
fi
if [ -d "/opt/ibm/lotus" ]
then
        dir_domino_program_default="/opt/ibm/lotus"
fi

##
## get program directory for Domino
##
echo "Please specify the Domino program directory"
if [ "$dir_domino_program_default" != "" ]
then
        echo "Or press enter to accept the default"
        echo $dir_domino_program_default
fi
prompt
read dir_domino_program
echo ""
if [ "$dir_domino_program" = "" ]
then
        if [ "$dir_domino_program_default" != "" ]
        then
                dir_domino_program=$dir_domino_program_default
        fi
fi
checkpath $dir_domino_program
if [ ! -d $dir_domino_program ]
then
	echo "The specified directory [$dir_domino_program] does not exist."
	exit 1;
fi
dir_domino_program="`dirname $dir_domino_program`""/""`basename $dir_domino_program`"
if [ ! -d "$dir_domino_program""/""notes" ]
then
	echo "The specified directory is not a valid Domino program directory."
	exit 1;
fi



##
## get data directory for Domino
##
echo "Please specify the Domino data directory"
if [ "$dir_domino_data_default" != "" ]
then
        echo "Or press enter to accept the default"
        echo $dir_domino_data_default
fi
prompt
read dir_domino_data
echo ""
if [ "$dir_domino_data" = "" ]
then
        if [ "$dir_domino_data_default" != "" ]
        then
                dir_domino_data=$dir_domino_data_default
        fi
fi
checkpath $dir_domino_data
if [ ! -d $dir_domino_data ]
then
	echo "The specified directory [$dir_domino_data] does not exist."
	exit 1;
fi
if [ "`dirname $dir_domino_data`" != "/" ]
then
dir_domino_data="`dirname $dir_domino_data`""/""`basename $dir_domino_data`"
fi
if [ "`dirname $dir_domino_data`" = "/" ]
then
        dir_domino_data="/""`basename $dir_domino_data`"
fi

##
## get OOO directory for Domino
##
#echo "Please specify the Out Of Office Manager directory"
#if [ "$dir_domino_data" != "" ]
#then
#        echo "Or press enter to accept the default"
#        echo $dir_domino_data
#fi
#prompt
#read dir_ooo
#echo ""
#if [ "$dir_ooo" = "" ]
#then
#        if [ "$dir_domino_data" != "" ]
#        then
#                dir_ooo=$dir_domino_data
#        fi
#fi
#checkpath $dir_ooo
#if [ ! -d $dir_ooo ]
#then
##	echo "The specified directory [$dir_ooo] does not exist."
##	exit 1;
#	mkdir -p $dir_ooo
#fi
#if [ "`dirname $dir_ooo`" != "/" ]
#then
#dir_ooo="`dirname $dir_ooo`""/""`basename $dir_ooo`"
#fi
#if [ "`dirname $dir_ooo`" = "/" ]
#then
#        dir_ooo="/""`basename $dir_ooo`"
#fi
dir_ooo=$dir_domino_data

##
## get Domino UNIX user
##
echo "Please specify the Domino UNIX user account"
if [ "$usr_domino_default" != "" ]
then
        echo "Or press enter to accept the default"
        echo $usr_domino_default
fi
prompt
read usr_domino
echo ""
if [ "$usr_domino" = "" ]
then
        if [ "$usr_domino_default" != "" ]
        then
                usr_domino=$usr_domino_default
        fi
        if [ "$usr_domino" = "" ]
        then
	echo "UNIX user not specified."
	exit 1;
        fi
fi
id $usr_domino 1>/dev/null 2>/dev/null
if [ $? != 0 ]
then
	echo "The specified UNIX user does not exist."
	exit 1;
fi
id_notes_all=`id $usr_domino`
id_notes_usr=`echo $id_notes_all | awk '{print $1}' | sed -e 's/^[^(]*(//' -e 's/).*$//'`
id_notes_grp=`echo $id_notes_all | awk '{print $2}' | sed -e 's/^[^(]*(//' -e 's/).*$//'`


##
## ready to go; ask for permit
##
cat <<!
**
** Out Of Office has gathered the necessary information.
** It will now perform the installation.
** 
** If you want to proceed, press Enter.
** If you want to quit, press Ctrl-C.
** 
!
prompt
read continue

src=bin/ooo
dst=$dir_domino_program/notes/latest/$LARCH/ooo
if [ -f $dst ]
then
	errmsg=`cmp $src $dst 2>&1`
	if [ "$errmsg" != "" ]
	then
		ooo_process="`ps -ef | grep /ooo$ | grep -v grep`"
		if [ "$ooo_process" != "" ]
		then
			echo "Out Of Office is already running. Setup cannot continue."
			echo "If you have multiple Domino partitions with Out Of Office,"
			echo "please stop them first before you install Out Of Office."
			echo ""
			exit 1;
		fi
	fi
fi



##
## delete Servlet directory
##
dst=$dir_domino_data/domino/servlet/com/extracomm
if [ -f $dst ]
then
rm -r $dst
fi


##
## setup install log
##

mkdir -p $dir_domino_program/_uninst/ooo
if [ $? != 0 ]
then
	echo "Cannot create Out Of Office installation directory."
	exit 1;
fi
chown $id_notes_usr:$id_notes_grp $dir_domino_program/_uninst/ooo
if [ $? != 0 ]
then
	echo "Cannot setup Out Of Office installation directory. Please check permission."
	exit 1;
fi

install_log=$dir_domino_program/_uninst/ooo/OOOInstall.log
rm -f $install_log
echo "dir_domino_program=""$dir_domino_program" >> $install_log
echo "dir_domino_data=""$dir_domino_data" >> $install_log


##
## copy uninstall.sh to ooo directory
##
uninstall_sh=$dir_domino_data/OOOUninstall.sh
rm -f $uninstall_sh
src=OOOUninstall.sh
dst=$dir_domino_program/_uninst/ooo/OOOUninstall.sh
cp -p $src $dst
if [ $? != 0 ]
then
	exit 1;
fi
chown $id_notes_usr:$id_notes_grp $dst
if [ $? != 0 ]
then
	exit 1;
fi
chmod 755 $dst
if [ $? != 0 ]
then
	exit 1;
fi


##
## copy Out Of Office AddIn to program directory
##
skip_copy=0
src=bin/ooo
dst=$dir_domino_program/notes/latest/$LARCH/ooo
if [ -f $dst ]
then
	errmsg=`cmp $src $dst 2>&1`
	if [ "$errmsg" = "" ]
	then
		skip_copy=1
	fi
fi
if [ $skip_copy -ne 1 ]
then
	cp -p $src $dst
	if [ $? != 0 ]
	then
		exit 1;
	fi
	echo "ooo_addin=""$dst" >> $install_log
	chown root:bin $dst
	if [ $? != 0 ]
	then
		exit 1;
	fi
	chmod 555 $dst
	if [ $? != 0 ]
	then
		exit 1;
	fi
fi


##
## copy Out Of Office Servlet to directory
##
skip_copy=0
src=servlet/OOO.jar
dst=$dir_domino_program/notes/latest/$LARCH/OOO.jar
if [ -f $dst ]
then
	errmsg=`cmp $src $dst 2>&1`
	if [ "$errmsg" = "" ]
	then
		skip_copy=1
	fi
fi
if [ $skip_copy -ne 1 ]
then
	cp -p $src $dst
	if [ $? != 0 ]
	then
		exit 1;
	fi
	echo "ooo_jar=""$dst" >> $install_log
	chown root:bin $dst
	if [ $? != 0 ]
	then
		exit 1;
	fi
	chmod 555 $dst
	if [ $? != 0 ]
	then
		exit 1;
	fi
fi

##
## copy Out Of Office Database to directory
##
skip_copy=0
src=database/OOO.ntf
dst=$dir_ooo/OOO.ntf
if [ -f $dst ]
then
	errmsg=`cmp $src $dst 2>&1`
	if [ "$errmsg" = "" ]
	then
		skip_copy=1
	fi
fi
if [ $skip_copy -ne 1 ]
then
	cp -p $src $dst
	if [ $? != 0 ]
	then
		exit 1;
	fi
	echo "ooo_database=""$dst" >> $install_log
	chown $id_notes_usr:$id_notes_grp $dst
	if [ $? != 0 ]
	then
		exit 1;
	fi
	chmod 644 $dst
	if [ $? != 0 ]
	then
		exit 1;
	fi
fi

src=database/OOOEx.ntf
dst=$dir_ooo/OOOEx.ntf
if [ -f $dst ]
then
	errmsg=`cmp $src $dst 2>&1`
	if [ "$errmsg" = "" ]
	then
		skip_copy=1
	fi
fi
if [ $skip_copy -ne 1 ]
then
	cp -p $src $dst
	if [ $? != 0 ]
	then
		exit 1;
	fi
	echo "ooo_database=""$dst" >> $install_log
	chown $id_notes_usr:$id_notes_grp $dst
	if [ $? != 0 ]
	then
		exit 1;
	fi
	chmod 644 $dst
	if [ $? != 0 ]
	then
		exit 1;
	fi
fi

##
## setup servlets.properties
##
c_startup=OOO
c_servlet=com.extracomm.OOOServlet

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

param_startup=0
param_code=0

if [ -f $servletfile ]
then
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")
					param_startup=1
					if [ "`echo "$line" | grep "$c_startup"`" != "" ]
					then
						echo "$line" >> $tmpfile
					else
						echo "$line"",""$c_startup" >> $tmpfile
					fi
					;;
				"servlet.OOO.code") 
					param_code=1
					if [ "`echo "$line" | grep "$c_servlet"`" != "" ]
					then
						echo "$line" >> $tmpfile
					else
						echo "$line"",""$c_startup" >> $tmpfile
					fi
					;;
				*) 
					echo "$line" >> $tmpfile ;;
				esac
			else
				echo "$line" >> $tmpfile
			fi
		else
			echo "$line" >> $tmpfile
		fi
	fi
done < "$servletfile"
fi

if [ $param_startup -ne 1 ]
then
	echo "servlets.startup=""$c_startup" >> $tmpfile
fi

if [ $param_code -ne 1 ]
then
	echo "servlet.OOO.code=""$c_servlet" >> $tmpfile
fi


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

addin=ooo
javauserclass=OOO_CLASSES
ooo_class=$dir_domino_program/notes/latest/$LARCH/OOO.jar
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.install

param_servertasks=0
param_javaclass=0
param_oooclass=0
param_ooosetup=0
param_ooodirectory=0
param_ooolicense=0
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")
					param_servertasks=1
					if [ "`echo "$line" | grep "$addin"`" != "" ]
					then
						echo "$line" >> $tmpfile
					else
						echo "$line"",""$addin" >> $tmpfile
					fi
					;;
				"JavaUserClassesExt")
					param_javaclass=1
					if [ "`echo "$line" | grep "$javauserclass"`" != "" ]
					then
						echo "$line" >> $tmpfile
					else
						echo "$line"",""$javauserclass" >> $tmpfile
					fi
					;;
				"OOO_CLASSES")
					param_oooclass=1
					echo "OOO_CLASSES=""$ooo_class" >> $tmpfile
					;;
				"OOOSetup")
					param_ooosetup=1
					echo "OOOSetup=2" >> $tmpfile
					;;
				"OOODirectory")
					param_ooodirectory=1
					echo "OOODirectory=" >> $tmpfile
					;;
				"OOOLicense")
					param_ooolicense=1
					echo "$line" >> $tmpfile
					;;
				*) 
					echo "$line" >> $tmpfile ;;
				esac
			else
				echo "$line" >> $tmpfile
			fi
		else
			echo "$line" >> $tmpfile
		fi
	fi
done < "$notesini"

if [ $param_servertasks -ne 1 ]
then
	echo "ServerTasks=""$addin" >> $tmpfile
fi
if [ $param_javaclass -ne 1 ]
then
	echo "JavaUserClassesExt=""$javauserclass" >> $tmpfile
fi
if [ $param_oooclass -ne 1 ]
then
	echo "OOO_CLASSES=""$ooo_class" >> $tmpfile
fi
if [ $param_ooosetup -ne 1 ]
then
	echo "OOOSetup=2" >> $tmpfile
fi
if [ $param_ooodirectory -ne 1 ]
then
	echo "OOODirectory=" >> $tmpfile
fi
if [ $param_ooolicense -ne 1 ]
then
	echo "OOOLicense=" >> $tmpfile
fi

##
## 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 installation 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 ""
