#!/bin/bash
# This script sets up the Apache server to access
# websm help

#check if the apache server is installed
cd /
#echo "The package is : $package"
if [ -f /usr/sbin/httpd2 ]
then
        #echo "Apache 2 Package is installed....."
	grep -q ^DocumentRoot /etc/apache2/default-server.conf
	if [ $? -eq 0 ]; then
	   sed -e 's/^DocumentRoot/#DocumentRoot/g' /etc/apache2/default-server.conf > /tmp/__default_server_conf
	   mv /tmp/__default_server_conf /etc/apache2/default-server.conf
	fi
	chown root.www /usr/local/etc/httpd/users
        if [ -f /opt/hsc/data/hmchttpd.conf ]
        then
                #echo "the config file exists"
		#check if the ServerName is already set
		hname=""
	        hname=`grep -v ^# /opt/hsc/data/hmchttpd.conf | grep ServerName | cut -d ' ' -f 2`	
		if [ "$hname" != "" ]
		then 

		     #echo "Server Name exists hence needs to be changed"
		     awk '// {if ($1 ~ /^ServerName/) {printf("%s\n", hn)} else {print}}' hn="ServerName $HOSTNAME" /opt/hsc/data/hmchttpd.conf > /opt/hsc/data/hmchttpd.mod
		     cp /opt/hsc/data/hmchttpd.mod /opt/hsc/data/hmchttpd.conf
		     rm /opt/hsc/data/hmchttpd.mod
		
		else	

                     # add the Server Name to the end of the file 
                     #echo "The host name is $HOSTNAME"
                     echo "ServerName $HOSTNAME" >> /opt/hsc/data/hmchttpd.conf
		fi
	    fi

	    if [ ! -L /srv/www/help ]
	    then
	        #echo "Setting up the link"
	        ln -s /opt/hsc/help /srv/www/
	    fi
	
	    cd /
#	    sed -e 's/-FollowSymLinks/+FollowSymLinks/g' /etc/httpd/httpd.conf > /tmp/_newHttpd.conf_
#	    mv /tmp/_newHttpd.conf_ /etc/httpd/httpd.conf
	 

            serverflg=`grep ^APACHE_SERVER_FLAGS /etc/sysconfig/apache2 | cut -d'=' -f2`
            if [ "$serverflg" == "\"\"" ]; then
	       sed -e 's/APACHE_SERVER_FLAGS=\"\"/APACHE_SERVER_FLAGS=\"-D SSL\"/g' /etc/sysconfig/apache2 > /tmp/_new_apache2_conf_
               mv /tmp/_new_apache2_conf_ /etc/sysconfig/apache2
            fi
            inclfiles=`grep ^APACHE_CONF_INCLUDE_FILES /etc/sysconfig/apache2 | cut -d'=' -f2`
            if [ "$inclfiles" == "\"\"" ]; then
	       sed -e 's/APACHE_CONF_INCLUDE_FILES=\"\"/APACHE_CONF_INCLUDE_FILES=\"\/opt\/hsc\/data\/hmchttpd.conf \/opt\/hsc\/data\/websmhttpd.conf\"/g' /etc/sysconfig/apache2 > /tmp/_new_apache2_conf_
               mv /tmp/_new_apache2_conf_ /etc/sysconfig/apache2
            fi
fi
