#!/bin/bash

cleanup()
{
  rm -rf /tmp/_bobcat_
  if [ $1 -ne 0 ]
  then
     rm -rf /opt/bobcat
  fi
  exit $1
}
if [ -f /opt/bobcat/bin/stopServer.sh ]
then
  /opt/bobcat/bin/stopServer.sh server1
  rm -rf /opt/bobcat
fi

if [ -d /tmp/_bobcat_ ]
then
    rm -rf /tmp/_bobcat_
fi
mkdir /tmp/_bobcat_
unzip $1 -d /tmp/_bobcat_ 2>&1 >/dev/null

# Now install

/tmp/_bobcat_/install.sh -installroot /opt/bobcat -hostName localhost
if [ $? -ne 0 ]
then
   echo "Installation failed"
   cleanup 1
fi
/opt/bobcat/bin/wasprofile.sh -create -profileName profile1 -profilePath /opt/bobcat/profiles/profile1 -templatePath /opt/bobcat/profileTemplates/default -nodeName DefaultNode -hostName localhost -cellName DefaultNode -isDefault -portsFile /opt/hsc/data/portdef.props

/opt/bobcat/bin/startServer.sh server1 -script /opt/bobcat/bin/start_server1.sh 2>&1 >/dev/null

echo "Installing ASM Proxy...."
cp /opt/hsc/data/asmproxy.war /opt/bobcat/profiles/profile1/installableApps/

/opt/bobcat/bin/wsadmin.sh -conntype NONE -c "\$AdminApp install {/opt/bobcat/profiles/profile1/installableApps/asmproxy.war} { -usedefaultbindings -nodeployejb -appname asmproxy -contextroot asmproxy} "

echo "Importing ASM server signed certificate to bobcat JVM root CA..."
/opt/bobcat/java/bin/keytool -import -trustcacerts -alias asm -file /opt/hsc/data/asm.cer  -keystore /opt/bobcat/java/jre/lib/security/cacerts -storetype jks -storepass changeit -noprompt


if [ $? -ne 0 ]
then
   echo "Installation failed"
   cleanup 1
fi

if [ $? -ne 0 ]
then
   cleanup 1
fi
grep "^wasexpress" /etc/services 2>/dev/null
if [ $? -ne 0 ]
then
   echo "wasexpress       4411/tcp" >>/etc/services
   echo "wasexpress       4411/udp" >>/etc/services
fi

chmod 555 /opt/bobcat/bin/rc.was
grep "^was" /etc/inittab 2>/dev/null
if [ $? -ne 0 ]
then
   echo "was:2345:once:/opt/bobcat/bin/rc.was >/dev/console 2>&1" >>/etc/inittab
fi
cleanup 0
