#!/bin/sh
JAVA_HOME=/usr/java1.2/bin
PATH=/usr/java1.2/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 3 ]
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 3 ]
		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 #
	echo "Is this web server enabled for SSL support?  y/n [n]: \c"
        read ans
        if [ -z "$ans" ]
        then
                ans="n"
        fi
        if [ $ans = "y" ]
        then
		#echo "This is ssl server"
		rm -f ${server_path}/libexec/mod_ssl_egurkha.so
		cp /opt/egurkha/lib/mod_ssl_egurkha.so ${server_path}/libexec/
		chmod 755 ${server_path}/libexec/mod_ssl_egurkha.so
	else
		#echo "This is  non ssl server"
		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
	fi	
	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 -sslenabled $ans`
	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
else
if [ $choice -eq 2 ]
then
# if server is NES then proceed #
	while [ -z "$server_path" ]
	do
		echo "Enter the root directory of the iPlanet web server:\c "
		read server_path
	done
	echo "Configuring the eG web adapter ..."
	cd $server_path
	# check is it a Iplanet6.0 server by checking the 
	# existance if "/server_path/config/server.xml" file 
	isIplanet6=1
	if [ ! -f "${server_path}/config/server.xml" ]
	then
		isIplanet6=0
		cp /opt/egurkha/bin/eg_nes_mon .
	fi
	# If the backup for ../bin/start not exist create a copy #
	if [ ! -f "${server_path}/start.pre_egurkha" ]
        then
		cp start start.pre_egurkha
	fi
	# If the backup for ../bin/stop not exist create a copy #
	if [ ! -f "${server_path}/stop.pre_egurkha" ]
        then
		cp stop stop.pre_egurkha
		chmod +x stop.pre_egurkha
		cp stop stop.egurkha
		chmod +x stop.egurkha
	fi

	cp start $conf1
	cp start $conf2

	# Deleting all the lines in the start file
	cat start | sed '//d' > start 2>/dev/null
	# removing all the old entries in the conf1 file	

	cat $conf1 | sed '/#!\/bin\/sh/d' > $conf2
	cat $conf2 |sed '/^CLASSPATH=\/opt\/egurkha/d' > $conf1
	cat $conf1 | sed '/^export CLASSPATH/d' > $conf2
	cat $conf2 | sed '/java IplanetConfig/d' > $conf1
	cat $conf1 | sed '/NUM_SEMAPHORES=/d' >$conf2
	cat $conf2 | sed '/export NUM_SEMAPHORES/d' > $conf1
	cat $conf1 | sed '/WEB_SERVER=/d' > $conf2
	cat $conf2 | sed '/WEB_SERVER_ROOT=/d' > $conf1
	cat $conf1 | sed '/eg_nes_mon/d' > $conf2
	cat $conf2 | sed '/LD_PRELOAD=/d' > $conf1
	cat $conf1 | sed '/export WEB_SERVER/d' > $conf2
	cat $conf2 | sed '/export WEB_SERVER_ROOT/d' > $conf1
	cat $conf1 | sed '/export LD_PRELOAD LD_LIBRARY_PATH/d' > $conf2
	cat $conf2 | sed '/^LD_LIBRARY_PATH=\/opt\/egurkha\/lib/d' > $conf1


	echo '#!/bin/sh' > start
	echo "CLASSPATH=$CLASSPATH:\$CLASSPATH" >> start
	echo 'export CLASSPATH' >> start
	echo "java IplanetConfig -serverRoot $server_path" >> start
	echo 'NUM_SEMAPHORES=2' >> start
	echo 'export NUM_SEMAPHORES' >> start
	echo 'WEB_SERVER=netscape' >> start
	echo "WEB_SERVER_ROOT=$server_path" >> start
	echo "LD_LIBRARY_PATH=/opt/egurkha/lib:\$LD_LIBRARY_PATH" >> start
	echo 'export WEB_SERVER' >> start
	echo 'export WEB_SERVER_ROOT' >> start
	# If it is iplanet6 don't add the eg_nes_mon
	if [ $isIplanet6 -ne 1 ] 
	then
		echo "nohup \$WEB_SERVER_ROOT/eg_nes_mon \$WEB_SERVER_ROOT > /dev/null 2>&1 &" >> start
	fi
	echo 'LD_PRELOAD=/opt/egurkha/lib/libnes_eg_wa.so:$LD_PRELOAD' >> start
	echo 'export LD_PRELOAD LD_LIBRARY_PATH' >> start
	
	cat $conf1 >> start 
	rm -f $conf1 $conf2
	cp stop $conf1
	cat $conf1 | sed '/#!\/bin\/sh/d' > $conf2
	cat $conf2 | sed '/WEB_SERVER_ROOT/d' > $conf1

	echo '#!/bin/sh' > stop
	echo "WEB_SERVER_ROOT=$server_path" >> stop
	echo 'export WEB_SERVER_ROOT' >> stop
	
	if [ $isIplanet6 -ne 1 ]
	then
	echo "/usr/bin/ps -aef -o pid,args | grep \"\$WEB_SERVER_ROOT\" | grep \"eg_nes_mon\" | /usr/bin/awk '{ print \$1 }' | xargs kill -9 > /dev/null 2>&1" >> stop
	fi

	cat $conf1 >> stop

	echo "Is this web server enabled for SSL support?  y/n [n]: \c"
	read ans
	if [ -z "$ans" ]
	then
		ans="n"
	fi
	if [ $ans = "y" ]
	then
		# configuring the obj.conf file by using a java program #
		cd ${server_path}/config
		if [ ! -f obj.conf.pre_egurkha ]
		then
			cp obj.conf obj.conf.pre_egurkha
		fi
		if [ ! -f magnus.conf.pre_egurkha ]
		then
			cp magnus.conf magnus.conf.pre_egurkha
		fi
		filePath=${server_path}/config/
		result=`java SetupSSLNetscape -serverpath $filePath`
		if [ "$result" != "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	
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 "********************************************************************"
