#!/usr/bin/qsh
#
# //  DESCRIPTION:
# //      Script file used to remove Web Server definitions.


. $(/usr/bin/dirname $0)/setupCmdLine $@


WAS_PROFILE_NAME=
WEB_SERVER_NAME=
WEB_SERVER_NODE=
REMOVE_NODE=FALSE


printUsage()
{
        echo ""
        echo "removeOs400WebServerDefinition"
        echo "    -webserver.name <webserverName>"
        echo "    -webserver.node <webserverNode>"
        echo "    [-profileName <wasProfile>]"
	echo "    [-removeNode]"
        echo "    [ -help | -? ]"
        echo " "
        echo "Where:"
        echo "    \"webserverName\" is the web server name;"
        echo "    \"wasProfile\" is the WAS profile name, default is \"default\";"
        echo "    \"webserverNode\" is the web server node;"
        echo "    \"-removeNode\" indicates to remove the web server node also"
	echo "        When -removeNode is not specified, the web server node"
	echo "        is not removed;"
        exit 0
}

printError()
{
        echo "ERROR: $1"
        printUsage
}


while [ $# -gt 0 ]; do
   case $1 in
             -help|-?) printUsage 
                       exit 0                
                       ;;
      -webserver.name) shift
                       WEB_SERVER_NAME="$1"
                       ;;
      -webserver.node) shift
                       WEB_SERVER_NODE="$1"
                       ;;
         -profileName) shift
                       WAS_PROFILE_NAME="$1"
                       ;;
          -removeNode) REMOVE_NODE="TRUE"
                       ;;
                   -*) echo "Unsupported option: $1"
                       printUsage 
                       exit 0                
                       ;;
   esac
   shift
done


if [ -z "${WEB_SERVER_NAME}" ] ; then
  printError "Please specify the name of your IHS or Domino webserver using -webserver.name."
  exit 1
fi

if [ -z "${WAS_PROFILE_NAME}" ] ; then
  echo "No profile name was specified, using \"default\""
  WAS_PROFILE_NAME="default"
fi

if [ -z "${WEB_SERVER_NODE}" ] ; then
   printError "Please specify the Node name of your <webserverName>."
   exit 1
fi

# Check if profile is remote. If it is delete the WEB_SERVER_NAME dir
WAS_PROFILE_PROPRTIES_FILE=${USER_INSTALL_ROOT}/properties/.instance.properties

# Check if profile is remote  
/usr/bin/egrep -i "(instance.type)(.*)(HTTP)" ${WAS_PROFILE_PROPRTIES_FILE} > /dev/null
# if profile is remote, delete the WEB_SERVER_NAME directory under config
if [ $? == 0 ] ; then
   if [ ! -e ${USER_INSTALL_ROOT}/config/${WEB_SERVER_NAME} ]; then
      echo "Web server ${WEB_SERVER_NAME} does not exist."
      exit 1
   fi
   rm -rf ${USER_INSTALL_ROOT}/config/${WEB_SERVER_NAME} > /dev/null 2>&1
   echo "Web server ${WEB_SERVER_NAME} removed."
else 
  echo wsadmin -profileName ${WAS_PROFILE_NAME} \
    -conntype NONE \
    -f $(/usr/bin/dirname $0)/removeWebserverDefinition.jacl \
    ${WEB_SERVER_NAME} ${WEB_SERVER_NODE} ${REMOVE_NODE}

 
  $(/usr/bin/dirname $0)/wsadmin -profileName ${WAS_PROFILE_NAME} \
     -conntype NONE \
     -f $(/usr/bin/dirname $0)/removeWebserverDefinition.jacl \
    ${WEB_SERVER_NAME} ${WEB_SERVER_NODE} ${REMOVE_NODE}
fi





