if [ $# != 3 ]
then
	echo
	echo "Usage: hpcfgserver.sh <WebSphere Installation Directory>"
	echo "   <Host Publisher Installation Root> <Host Publisher Language Specification>"
	echo
	echo "Where:"
	echo "   <WebSphere Installation Directory> is the directory under which WebSphere"
	echo "   is installed, such as /usr/lpp/WebSphere for WebSphere V1.1. For V1.2, you"
	echo "   would specify the target server model root directory."
	echo
	echo "   <Host Publisher Installation Root> is the root directory under which the"
	echo "   /usr/lpp/HostPublisher directory structure exists. Typically, this would"
	echo "   be the root directory (/)."
	echo
	echo "   <Host Publisher Language Specification> is the locale specifier indicating"
	echo "   which language to enable. For example, en_US for U.S. English."
	echo
	exit 1
fi
if [ ! -d $1 ]
then
	echo
	echo "WebSphere is not installed. Host Publisher configuration cannot continue."
	echo "You must run hpuninstall.sh to properly remove Host Publisher."
	echo
	exit 1
fi
if [ ! -d $2 ]
then
	echo
	echo "Host Publisher is not installed. Host Publisher configuration cannot continue."
	echo "You must run hpuninstall.sh to properly remove Host Publisher."
	echo
	exit 1
fi

# Remove trailing slash
ROOT_DIR=${2%/}
echo
echo "Begin configuration of Host Publisher Server"
echo
echo "Building soft links...\c"
$ROOT_DIR/usr/lpp/HostPublisher/HPAdmin/createlinks.sh $ROOT_DIR $3
echo "done."
echo
echo "Configuring Web server...\c"

cd /etc
cp httpd.conf httpd.conf.`date +%m%d%y`

awk 'BEGIN {inserted="0"}
     {
      if ($2 == "/IBMWebAS/*" && inserted=="0") {print $0; print "Pass    /HostPublisher/doc/*     '$ROOT_DIR'/usr/lpp/HostPublisher/common/doc/*";
                print "Pass    /HostPublisher/*     /var/HostPublisher/Server/production/documents/*"; 
                print "Pass    /_IBM_HP_WebAdmin_/doc/*       '$ROOT_DIR'/usr/lpp/HostPublisher/common/doc/*";
                print "Pass    /_IBM_HP_WebAdmin_/*    /var/HostPublisher/Server/production/documents/*"; inserted="1";}
      else if ($2 != "/HostPublisher/*" && $2 != "/HostPublisher/doc/*" && $2 != "/_IBM_HP_WebAdmin_/*" && $2 != "/_IBM_HP_WebAdmin_/doc/*") print $0;
     }' httpd.conf.`date +%m%d%y` > httpd.conf
echo "done."

if [ -f $1/properties/was.conf ]
then
   # In case this is a WAS 1.2 system, or 1.1 with PTF, we need to configure using the was.conf file
   echo
   echo "Configuring WebSphere servlet service...\c"
 
   cd $1/properties
   cp was.conf was.conf.`date +%m%d%y`
   awk 'BEGIN {FS="="}
     {	
        if ($1=="servlets.startup" && match($2,"HPAdmin") == 0) print $0" HPAdmin";
        else if (match($1,"servlet.HPAdmin") == 0) print $0;
     }
     END {print; print "servlet.HPAdmin.code=com.ibm.HostPublisher.Server.HPAdminServlet";
          print "servlet.HPAdmin.initArgs=install_dir='$ROOT_DIR'/usr/lpp/HostPublisher,log_dir=/var/HostPublisher/log,server_dir=/var/HostPublisher/Server";
        }' was.conf.`date +%m%d%y` > was.conf

   awk 'BEGIN {FS="="}
     {	
        if ($1=="session.urlrewriting.enable") print "session.urlrewriting.enable=true";
        else print $0;
     }' was.conf > was.conf.tmp

   echo "done."
   echo
   echo "Configuring JVM properties...\c"

   append=:/var/HostPublisher/Server/production/beans
   for i in `ls $ROOT_DIR/usr/lpp/HostPublisher/common/*.*`
   do
	cat was.conf.tmp | sed 's|:'$i'||g' > was.conf.tmp2
	append=$append":$i"
	cp was.conf.tmp2 was.conf.tmp
   done

   awk 'BEGIN {FS="="} 
     { if ($1 == "ncf.jvm.classpath") print $0"'$append'";
       else print $0;
     }' was.conf.tmp > was.conf
   rm was.conf.tmp*
   echo "done."
   echo
   echo "Running updateproperties to commit changes...\c"
   $1/config/updateproperties $1
   echo "done."
else
   echo
   echo "Configuring WebSphere servlet service...\c"
 
   cd $1/AppServer/properties/server/servlet/servletservice
   cp servlets.properties servlets.properties.`date +%m%d%y`
   awk 'BEGIN {FS="="}
     {	
        if ($1=="servlets.startup" && match($2,"HPAdmin") == 0) print $0" HPAdmin";
        else if ($1=="servlets.classpath" && match($2,"/var/HostPublisher/Server/production/beans") == 0) { if ($2=="")print $0"/var/HostPublisher/Server/production/beans"; else print $0":/var/HostPublisher/Server/production/beans";}
        else if (match($1,"servlet.HPAdmin") == 0) print $0;
     }
     END {print; print "servlet.HPAdmin.code=com.ibm.HostPublisher.Server.HPAdminServlet";
          print "servlet.HPAdmin.description=IBM SecureWay Host Publisher Server";
          print "servlet.HPAdmin.allow_delete=false";
          print "servlet.HPAdmin.initArgs=install_dir='$ROOT_DIR'/usr/lpp/HostPublisher,log_dir=/var/HostPublisher/log,server_dir=/var/HostPublisher/Server";
        }' servlets.properties.`date +%m%d%y` > servlets.properties

   cp session.properties session.properties.`date +%m%d%y`
   awk 'BEGIN {FS="="}
     {	
        if ($1=="enable.urlrewriting") print "enable.urlrewriting=true";
        else print $0;
     }' session.properties.`date +%m%d%y` > session.properties

   echo "done."
   echo
   echo "Configuring JVM properties...\c"

   cp jvm.properties jvm.properties.`date +%m%d%y`
   cp jvm.properties jvm.tmp

   append=:/var/HostPublisher/Server/production/beans
   for i in `ls $ROOT_DIR/usr/lpp/HostPublisher/common/*.*`
   do
	cat jvm.tmp | sed 's|:'$i'||g' > jvm.tmp2
	append=$append":$i"
	cp jvm.tmp2 jvm.tmp
   done

   awk 'BEGIN {FS="="} 
     { if ($1 == "ncf.jvm.classpath") print $0"'$append'";
       else print $0;
     }' jvm.tmp > jvm.properties
   rm jvm.tmp*
   echo "done."
fi
echo
echo "Host Publisher Server configuration complete."
echo "You must restart your Web server for these changes to take effect."
echo




