#!/bin/bash
#
#This script controls the installation of OfficeTalk on a machine
#The installation is divided into two parts:
#The first part is performed by the argument -c and installs the common
#files for all users.
#The second part is performed by the argument -u <user> and installs the
#user related files
#
#The syntax:
#
#sh setup [-u <user>] [-c]
#-c        Installs the conmmon files in /opt/OfficeTalk
#-u <user> Installs the user related files in /home/<user>/OfficeTalk
#          You must substitute <user> with a registered linux user
#
#Preconditions:
#--------------
#
#It is asumed, that the script is started from pwd
#
#Installed: bash-shell
#           KDE
#           Directory /opt for applications (the destination)
#
#The script ist tested under Suse-Linux
#
#You can change source and destinations, if required, by modifying the lines assigning
#the variables $source, $dest and $userdest
#
#usage:
#------
#
#First execution:              sh setup -c [-u <user>]
#Second and further execution: sh -u <user>
#
commonOption=" "
userOption=" "
user=" "
if test $# -eq 0
then
	echo "usage: setup [-c] [-u <user>]"
	exit
fi
while [ $# != 0 ]
do
	if [ "$1" = -u ]
	then
		userOption=$1
        	user=$2
        	shift
        	shift
 	else
 		if [ "$1" = -c ]
 		then
 			commonOption=$1
 			shift
 		else
 			echo "Bad argument $1"
 			exit
 		fi
 	fi
done

source=.                	#The source (pwd)
parentdest=/opt         	#The destinations parent directory
dest=$parentdest/OfficeTalk  	#The destination for common files
userdest=/home/$user    	#The destination for user related files

#Verify the directory for the common files
if [ "$commonOption" = -c ]
then
	if [ ! -d $parentdest -o ! -x $parentdest ]
	then
		echo "$parentdest is not a directory or missing access perrmission"
		echo "Installation aborted !"
		exit
	fi
fi	

#Verify the directory for the user files
if [ "$userOption" = -u ]
then
	if [ ! -d $userdest -o ! -x $userdest ]
	then
		echo "$userdest is not a directory or missing access permission"
		echo "Installation aborted !"
		exit
	fi
fi

#
# Install common parts
#
if [ "$commonOption" = -c ]
then
	if test -e $dest
	then
		echo -n "Common directory does exist, update it (y/n): "
		read doInstall
	else
		doInstall="y"
		echo -n "Install common environment (y/n): "
		read commonEnvironment
	fi
	if [ "$doInstall" = y -o "$doInstall" = Y ]
	then
		if test ! -e $dest
		then
			echo Creating the common directories
	     		mkdir $dest
     			mkdir $dest/Help
 		    	mkdir $dest/Bin
     			mkdir $dest/messages
 		     	mkdir $dest/Library
      			chmod -R a+rx $dest
      		fi
	      	echo Copying common files
      		echo Copying readme and change files
        	cp readmefirst $dest
        	cp readme.html $dest
        	cp changes.html $dest
        	echo Copying communication libraries
        	cp Library\ Release/*.pcl $dest/Library
         	cp Library/*.sql $dest/Library
        	cp Library/*.wsdl $dest/Library
	       	echo Copying the help files
        	cp Help/* $dest/Help
        	echo Copying programm file visual
        	cp visual $dest/Bin
		cp herald.so $dest/Bin
        	chmod a+x $dest/Bin/visual
        	echo Copying program file
        	cp OfficeTalkMSP.im $dest/Bin/OfficeTalk.im
        	echo Copying licence
        	cp LicenceEvaluation.dat $dest/Bin/Licence.dat
        	chmod a+w $dest/Bin/Licence.dat
        	echo Copying message files
        	cp -r messages/OfficeTalk/* $dest/messages
        	chmod -R a+r $dest
	fi
	if [ "$commonEnvironment" = y -o "$commonEnvironment" = Y ]
	then
       		echo Creating common environment $dest/OfficeTalk.ini
        	echo "[OfficeTalk]">$dest/OfficeTalk.ini
          	chmod a+r $dest/OfficeTalk.ini
	fi	
fi
	
#
# Install user related parts
#
if [ "$userOption" = -u ]
then
        echo Copying user files
	if test -e $userdest/OfficeTalk.ini
	then
		echo -n "User environment does exist, overwrite it (y/n): "
		read doInstall
	else
		doInstall="y"
	fi
	if [ "$doInstall" = y -o "$doInstall" = Y ]
        then
        	echo Creating user environment $userdest/OfficeTalk.ini
        	echo "[OfficeTalk]">$userdest/OfficeTalk.ini
       		echo "LicenceDirectory=$dest/Bin">>$userdest/OfficeTalk.ini
       	       	echo "UseDatabase=0">>$userdest/OfficeTalk.ini
        	echo "DatabaseDirectory=$userdest/OfficeTalk">>$userdest/OfficeTalk.ini
        	echo "OpenWithBrowser=1">>$userdest/OfficeTalk.ini
        	echo "PDFReader=kghostview">>$userdest/OfficeTalk.ini
        	echo "Toolbar=1">>$userdest/OfficeTalk.ini
        	echo "LRULogins=OfficeTalk,schmack,kleve.weis,müller,meier,martin,kaul,lusky,knote">>$userdest/OfficeTalk.ini
        	echo "OrganigramBrowserContent=1">>$userdest/OfficeTalk.ini
        	echo "ProcessGraphToolbar=1">>$userdest/OfficeTalk.ini
        	echo "ToDoGroupToolbar=1">>$userdest/OfficeTalk.ini
        	chown $user.users $userdest/OfficeTalk.ini
        fi
	
	if test -e $userdest/OfficeTalk/OfficeTalk.bos
	then
		echo -n "Internal database does exit, update it (y/n): "
		read doInstall
	else
		doInstall="y"
	fi
	if [ "$doInstall" = y -o "$doInstall" = Y ]
        then
         	if test ! -e $userdest/OfficeTalk
        	then
        		mkdir $userdest/OfficeTalk
        	fi
	       	echo Copying internal database
        	cp OfficeTalk.bos $userdest/OfficeTalk
        	chown -R $user.users $userdest/OfficeTalk
        fi
       	if test ! -e $userdest/Desktop
        then
        	echo "KDE directory Desktop not found, icon not installed"
        else
   		if test ! -e $userdest/Desktop/OfficeTalk.kdelnk
   		then
                	echo Coying KDE Icon
                   	cp OfficeTalk.kdelnk $userdest/Desktop
                   	chown $user.users $userdest/Desktop/OfficeTalk.kdelnk
                   	cp RunOfficeTalk $userdest
                   	chown $user.users $userdest/RunOfficeTalk
                 fi
     	fi
     	
        if test ! -e $userdest/.bashrc
        then
        	echo "Startscript .bashrc not found. Extend your startscript with environmentvariable OFFICETALKMESSAGES by hand"
        else
        	grep -q "^export OFFICETALKMESSAGES=" < $userdest/.bashrc
        	if test $? -ne 0
        	then
        		echo "Add export for OFFICETALKMESSAGES in .bashrc"
        		echo -e "\nexport OFFICETALKMESSAGES=$dest/messages">>$userdest/.bashrc
        	fi
       		grep -q "^export OFFICETALKHOME=" < $userdest/.bashrc	
 		if test $? -ne 0
 		then
           		echo "Add export for OFFICETALKHOME in .bashrc"
     			echo "export OFFICETALKHOME=$dest/Bin">>$userdest/.bashrc
 		fi

        fi
fi
echo ----- All is done -----
