#!/bin/bash
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 ]
then
	while [ -z "$server_path" ]
	do
		echo "Enter the root directory of the web server: "
		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
	
	#default NUM_SEMAPHORES value is 3
	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` 
	#changed to handle apache 2 better
	val=`$server_path/bin/httpd -version | grep "Server version" | cut -d':' -f 2 | cut -d'/' -f 2 | cut -d'.' -f 1`
	version=`$server_path/bin/httpd -version | grep "Server version" | cut -d':' -f 2 | cut -d'/' -f 2 | cut -d'.' -f 1,2`
	if [ $val -ge 1 ]
	then
		apache_type=$APACHE2
	else
		apache_type=$APACHE1
	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 '/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.
        #java ParseApachectl -serverRoot $server_path
	chmod 755 apachectl

	#echo "Is this web server enabled for SSL support?  y/n [n]: "
	#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]:"
			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 #
		module_type=$apache_type
		if [ "$apache_type" = "$APACHE2" ]
		then
			if [ "$version" = "2.4" ]
			then
				cp /opt/egurkha/lib/mod_eg24.so ${server_path}/modules/
				chmod 755 ${server_path}/modules/mod_eg24.so
				apache_type="24"
			else
				cp /opt/egurkha/lib/mod_eg2.so ${server_path}/modules/
				chmod 755 ${server_path}/modules/mod_eg2.so
			fi
		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"$module_type"_module	
		#echo "Module $module"
		ans=`java SetupNewSSLApache -serverpath $filePath -moduleName $module -moduleFile mod_eg$apache_type`
		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 iPlanet web server: "
		read server_path
	done
	echo "Configuring the eG web adapter ..."
	cd $server_path
	cp /opt/egurkha/bin/eg_nes_mon .
	# If the backup for ../bin/start not exist create a copy #
	if [ ! -f "${server_path}/bin/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}/bin/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 '/WEB_SERVER=/d' > $conf1
	cat $conf1 | sed '/WEB_SERVER_ROOT=/d' > $conf2
	cat $conf2 | sed '/eg_nes_mon/d' > $conf1
	cat $conf1 | sed '/LD_PRELOAD=/d' > $conf2
	cat $conf2 | sed '/export WEB_SERVER/d' > $conf1
	cat $conf1 | sed '/export WEB_SERVER_ROOT/d' > $conf2
	cat $conf2 | sed '/export LD_PRELOAD/d' > $conf1

	echo '#!/bin/sh' > start
	echo 'WEB_SERVER=netscape' >> start
	echo "WEB_SERVER_ROOT=$server_path" >> start
	echo 'export WEB_SERVER' >> start
	echo 'export WEB_SERVER_ROOT' >> start
	echo "nohup \$WEB_SERVER_ROOT/eg_nes_mon \$WEB_SERVER_ROOT > /dev/null 2>&1 &" >> start
	echo 'LD_PRELOAD=/opt/egurkha/lib/libnes_eg_wa.so' >> start
	echo 'export LD_PRELOAD' >> 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

	echo "ps -aef -o pid,args | grep \"\$WEB_SERVER_ROOT\" | grep \"eg_nes_mon\" | sed 's/^ //' | cut -f1 -d ' ' | xargs kill -9 > /dev/null 2>&1" >> stop
	cat $conf1 >> stop

	echo "Is this web server enabled for SSL support?  y/n [y]: "
	read ans
	if [ -z "$ans" ]
	then
		ans="y"
	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
		filePath=${server_path}/config/obj.conf
		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 "********************************************************************"
