#!/bin/sh
JAVA_HOME=/opt/egurkha/jre/bin
PATH=/opt/egurkha/jre/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 HTTP server: \c"
		fi
		read server_path
	done
	cd $server_path/bin
	# 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 '/^LD_PRELOAD=\/opt\/egurkha\/lib/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 LD_PRELOAD/d' > $conf2
	cat $conf2 | sed '/^LD_LIBRARY_PATH=\/opt\/egurkha\/lib/d' > $conf1
	cat $conf1 | sed '/export LD_LIBRARY_PATH WEB SERVER/d' > $conf2
	cat $conf2 | sed '/java ApacheModuleInfo/d' > $conf1
	cat $conf1 >> 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 "
        #java ParseApachectl -serverRoot $server_path
	chmod 755 apachectl

	#echo "Is this web server enabled for SSL support?  y/n [n]: \c"
	#read ans
	#if [ -z "$ans" ]
	#then
	#	ans="n"
	#fi
	ans="y"
	if [ $ans = "y" ]
	then
		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 "The eG SSL enabled webadapter cannot be installed."
				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
				rm -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 "****************************************************************"
			echo "Continue? y/n [n]:\c"
			read jj
			if [ -z "$jj" ]
			then 
				jj="n"
			fi
			if [ $jj = "n" ]
			then
				rm -f $conf1
				rm -f $conf2
				exit
			fi
		fi
		echo "Configuring the eG web adapter ..."
		# Copy the apache SSL library  (mod_egurkha.so) in the ../libexec directory #
		# if the server is IBMHTTPServer copy "mod_ibm_egurkha.so"  #
		#if [ $choice -eq 3 ]
		#then
		#	cp /opt/egurkha/lib/mod_ibm_egurkha.so ${server_path}/libexec/
		#	chmod 755 ${server_path}/libexec/mod_ibm_egurkha.so
#
		#else	
		#	cp /opt/egurkha/lib/mod_egurkha.so ${server_path}/libexec/
		#	chmod 755 ${server_path}/libexec/mod_egurkha.so
		#fi
		if [ "$apache_type" = "$APACHE2" ]
		then
			cp /opt/egurkha/lib/mod_eg2.so ${server_path}/modules/
			chmod 755 ${server_path}/modules/mod_eg2.so
		else
			cp /opt/egurkha/lib/mod_eg1.so ${server_path}/libexec/
			chmod 755 ${server_path}/libexec/mod_eg1.so
		fi
		# 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

		module=eg"$apache_type"_module	
		#echo "Module $module"
		ans=`java SetupNewSSLApache -serverpath $filePath -moduleName $module -moduleFile mod_eg$apache_type`
		#if [ $choice -eq 3 ]
		#then
		#	ans=`java SetupSSLApache -serverpath $filePath -servertype ibmhttpserver` 
		#else
		#	ans=`java SetupSSLApache -serverpath $filePath -servertype apache`
		#fi
		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
else
if [ $choice -eq 2 ]
then
# if server is NES then proceed #
	while [ -z "$server_path" ]
	do
		echo "Enter the root directory of the SunONE/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=\/opt\/egurkha\/lib/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_HOME/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 "********************************************************************"
