#!/bin/sh
JAVA_HOME=/usr/java/opt/java1.3/bin
PATH=/usr/java/opt/java1.3/bin:$PATH
CLASSPATH=/opt/egurkha/lib/eg_agent.jar:/opt/egurkha/lib/classes111.zip
choice=$1
export PATH
export CLASSPATH

APACHE=1
APACHE2=2
#TMPDIR check this enviroment variable value
if test -z "$TMPDIR" ; then
         TMPDIR="/tmp"
fi

conf1=$TMPDIR/eg_1`date +%HH``date +%MM``date +%SS`
conf2=$TMPDIR/eg_2`date +%SS``date +%MM``date +%HH`
# if server is apache if (choice == 1)  #
if [ $choice -eq 1 -o $choice -eq 2 ]
then
	while [ -z "$server_path" ]
	do
		if [ $choice -eq 1 ]
		then
		echo "Enter the root directory of the Apache web server: \c"
		elif [ $choice -eq 2 ]
		then
		echo "Enter the root directory of the IBM web server: \c"
		fi
		read server_path
	done
	cd $server_path/bin

	if [ -x "httpd" ]
	then
		cnt=`./httpd -l | grep "mod_so.c" | wc -l`
		if [ $cnt -le 0 ]
		then
			echo "************************************************************"
			echo "This web server has not been configured with DSO support ..."
                        echo "Please reconfigure this web server with DSO support and "
                        echo "run the command /opt/egurkha/bin/setup_webadapter to "
                        echo "configure the web server with eG's web adapter capability"
                        echo "************************************************************"
                        rm -f $conf1
                        m -f $conf2
                        exit;
                fi
        else
                echo "****************************************************************"
                echo "The setup process is not able to check whether this web server "
                echo "is configured for DSO support. Please make sure that this web "
                echo "server has been configured for DSO support before proceeding"
                echo "further..."
                echo "****************************************************************"
		exit
	fi
	# If the backup for ../bin/apachectl not exist create a copy # 
	if [ ! -f "${server_path}/bin/apachectl.pre_egurkha" ]
	then
		cp apachectl apachectl.pre_egurkha
	fi
	cp apachectl $conf1
	cp apachectl $conf2
	apache_type=$APACHE
	#find out is the server version is 2.x or not 	
	#If the server has either 'threaded' or 'perchild' modules are exist
	#then it is a apache2.0 (By default all the versions of apache is prefork module).
	val=`$server_path/bin/httpd -l  | grep "perchild" | wc -l` 
	if [ $val -ge 1 ]
	then
		apache_type=$APACHE2
	fi
	val=`$server_path/bin/httpd -l | grep "worker" | wc -l` 
	if [ $val -ge 1 ]
	then
		apache_type=$APACHE2
	fi
	# Deleting all the lines in the apachectl file
	cat apachectl | sed '//d' > apachectl 2>/dev/null
	#ls -l apachectl
	# removing all the old entries in the conf1 file	
	cat $conf1 | sed '/#!\/bin\/sh/d' > $conf2
	cat $conf2 | sed '/WEB_SERVER=/d' > $conf1
	cat $conf1 | sed '/WEB_SERVER_ROOT/d' > $conf2
	cat $conf2 | sed '/NUM_SEMAPHORES=/d' > $conf1
	cat $conf1 | sed '/REQUEST_INTERVAL/d' > $conf2
	cat $conf2 | sed '/EXTSHM=ON/d' > $conf1
	cat $conf1 | sed '/^PATH=/d'  > $conf2
	cat $conf2 | sed '/^CLASSPATH=/d' > $conf1
	cat $conf1 | sed '/export PATH CLASSPATH/d' > $conf2
	cat $conf2 | sed '/export WEB_SERVER/d' > $conf1
	cat $conf1 | sed '/export WEB_SERVER_ROOT/d' > $conf2
	cat $conf2 | sed '/export NUM_SEMAPHORES/d' > $conf1
	cat $conf1 | sed '/export REQUEST_INTERVAL/d' > $conf2
	cat $conf2 | sed '/SSL_FACTOR=/d' > $conf1
	cat $conf1 | sed '/export SSL_FACTOR EXTSHM/d' > $conf2
	cat $conf2 | sed '/^eg_remove_share/d' > $conf1
	cat $conf1 | sed '/java ApacheModuleInfo/d' > $conf2

	echo '#!/bin/sh' > apachectl
	echo "PATH=$JAVA_HOME:\$PATH" >> apachectl
	echo "CLASSPATH=$CLASSPATH:\$CLASSPATH" >> apachectl
	echo 'export PATH CLASSPATH' >> apachectl
	echo "java ApacheModuleInfo -serverRoot $server_path" >> apachectl
	echo 'WEB_SERVER=apache' >> apachectl
	echo "WEB_SERVER_ROOT=$server_path" >> apachectl
	echo "NUM_SEMAPHORES=3" >> apachectl
	echo 'REQUEST_INTERVAL=5' >> apachectl
	#if apache version2.0 with threaded or perchild module then
	# different library which support threading
	echo 'EXTSHM=ON' >> apachectl
	echo 'SSL_FACTOR=$1' >> apachectl
	echo 'export WEB_SERVER' >> apachectl
	echo 'export WEB_SERVER_ROOT' >> apachectl
	echo 'export NUM_SEMAPHORES' >> apachectl
	echo 'export REQUEST_INTERVAL' >> apachectl
	echo 'export SSL_FACTOR EXTSHM' >> apachectl
	echo '$WEB_SERVER_ROOT/bin/eg_remove_share $1' >> apachectl
	cat $conf2 >> apachectl
	# This will check the apachectl file for LD_PRELOAD options
        # if already LD_PRELOAD is used in apachectl it will suffix the
        # the existing library options for webadapter.
        #echo "ParseApachectl started "
	chmod 755 apachectl
	# Copy the apache SSL library  (mod_egurkha.so) in the ../libexec directory #
	rm  -f ${server_path}/libexec/mod_egurkha.so
	cp /opt/egurkha/lib/mod_egurkha.so ${server_path}/libexec/
	chmod 755 ${server_path}/libexec/mod_egurkha.so
	cp /opt/egurkha/bin/remove_share ${server_path}/bin/
	cp /opt/egurkha/bin/eg_remove_share ${server_path}/bin/
	# If the file not exist, create a copy as backup #
	if [ ! -f "${server_path}/conf/httpd.conf.pre_egurkha" ]
	then
       		cp "${server_path}/conf/httpd.conf" "${server_path}/conf/httpd.conf.pre_egurkha"
	fi	
	filePath=${server_path}/conf/httpd.conf
	ans=`java SetupSSLApache -serverpath $filePath`
	if [ "$ans" != "TRUE" ]
	then
		echo "***************************************************************"
		echo "Failure updating the configuration file for the web server! ..."
		echo "This web server may not be enabled for SSL support..."
		echo "Please check the server configurations and try again ..."
		echo "***************************************************************"
		rm -f $conf1
		rm -f $conf2
		exit
	fi
fi
rm -f $conf1
rm -f $conf2
echo " " 
echo "*******************************************************************"
echo "If there are any errors in the above process, you may not have "
echo "permission to update the web server's configuration. "
echo "Please have the web server's administrator run the command "
echo "   /opt/egurkha/bin/setup_webadapter "
echo " "
echo "If there were no errors, the web adapter has been successfully"
echo "configured. For the web adapter to be effective, please restart the"
echo "configured webserver. "
echo "********************************************************************"
