#!/bin/sh
########################################################################
# 
#   Copyright (C) 2006 Novell, Inc. All Rights Reserved.
# 
#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Lesser General Public
#   License as published by the Free Software Foundation; version 2.1
#   of the License.
# 
#   This library is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   Library Lesser General Public License for more details.
# 
#   You should have received a copy of the GNU Lesser General Public
#   License along with this library; if not, Novell, Inc.
#  
#   To contact Novell about this file by physical or electronic mail, 
#   you may find current contact information at www.novell.com.
#  
#   Author: Juan Carlos Luciani <jluciani@novell.com>
#   
########################################################################

########################################################################
#
# Script for editing svc.settings files
#
########################################################################

# Source our environment variables file
. /etc/CASA/authtoken/svc/envvars

TEST_IBM_JVM=$($JAVA_HOME/bin/java -version 2>&1 | grep -i ibm)

# Check if we need to determine which server.xml file to use
if [ -f /srv/www/casaats/conf/server.xml ]; then
   # No need to determine which file to use
   SERVER_XML_FILE_PATH=/srv/www/casaats/conf/server.xml
else
   # Determine which server.xml file to use
   if [ -z "${TEST_IBM_JVM}" ]; then
      # Assume Sun JVM
      # Use PKCS12 version if PKCS12 store exists
      if [ -f /etc/ssl/servercerts/keystore.p12 ]; then
         SERVER_XML_FILE_PATH=/srv/www/casaats/conf/server-pkcs12-sun.xml
      else
         SERVER_XML_FILE_PATH=/srv/www/casaats/conf/server-sun.xml
      fi
   else
      # IBM JVM
      # Use PKCS12 version if PKCS12 store exists
      if [ -f /etc/ssl/servercerts/keystore.p12 ]; then
         SERVER_XML_FILE_PATH=/srv/www/casaats/conf/server-pkcs12-ibm.xml
      else
         SERVER_XML_FILE_PATH=/srv/www/casaats/conf/server-ibm.xml
      fi
   fi
fi

echo "server.xml path = "$SERVER_XML_FILE_PATH


SSL_CONNECTOR_BEGIN="<!-- SSL_CONNECTOR_BEGIN -->"
SSL_CONNECTOR_END="<!-- SSL_CONNECTOR_END -->"
SSL_CONNECTOR_COMMENT_BEGIN="<!-- SSL_CONNECTOR_COMMENT_BEGIN"
SSL_CONNECTOR_COMMENT_END="SSL_CONNECTOR_COMMENT_END -->"
AJP_CONNECTOR_BEGIN="<!-- AJP_CONNECTOR_BEGIN -->"
AJP_CONNECTOR_END="<!-- AJP_CONNECTOR_END -->"
AJP_CONNECTOR_COMMENT_BEGIN="<!-- AJP_CONNECTOR_COMMENT_BEGIN"
AJP_CONNECTOR_COMMENT_END="AJP_CONNECTOR_COMMENT_END -->"


# Assume success
retVal=0

# Perform the operation requested
if [ $# -eq 2 ]; then
   if [ $1 = "-e" ]; then
      if [ $2 = "ssl" ]; then
         echo "Enabling ssl connector"
         sed -i s:"$SSL_CONNECTOR_COMMENT_BEGIN":"$SSL_CONNECTOR_BEGIN":g $SERVER_XML_FILE_PATH
         sed -i s:"$SSL_CONNECTOR_COMMENT_END":"$SSL_CONNECTOR_END":g $SERVER_XML_FILE_PATH
      else
         if [ $2 = "ajp" ]; then
            echo "Enabling ajp connector"
            sed -i s:"$AJP_CONNECTOR_COMMENT_BEGIN":"$AJP_CONNECTOR_BEGIN":g $SERVER_XML_FILE_PATH
            sed -i s:"$AJP_CONNECTOR_COMMENT_END":"$AJP_CONNECTOR_END":g $SERVER_XML_FILE_PATH
            ln -s /etc/CASA/authtoken/svc/casaats.conf /etc/apache2/conf.d/casaats.conf
         else
            echo "Connector type not supported"
            retVal=1
         fi
      fi
   else
      if [ $1 = "-d" ]; then
         if [ $2 = "ssl" ]; then
            echo "Disabling ssl connector"
            sed -i s:"$SSL_CONNECTOR_BEGIN":"$SSL_CONNECTOR_COMMENT_BEGIN":g $SERVER_XML_FILE_PATH
            sed -i s:"$SSL_CONNECTOR_END":"$SSL_CONNECTOR_COMMENT_END":g $SERVER_XML_FILE_PATH
            rm -f /etc/apache2/conf.d/casaats.conf
         else
            if [ $2 = "ajp" ]; then
               echo "Disabling ajp connector"
               sed -i s:"$AJP_CONNECTOR_BEGIN":"$AJP_CONNECTOR_COMMENT_BEGIN":g $SERVER_XML_FILE_PATH
               sed -i s:"$AJP_CONNECTOR_END":"$AJP_CONNECTOR_COMMENT_END":g $SERVER_XML_FILE_PATH
            else
               echo "Connector type not supported"
               retVal=1
            fi
         fi
      else
         if [ $1 = "-s" ]; then
            if [ $2 = "ssl" ]; then
               echo "Checking ssl connector status"
               TEST_SSL_CONNECTOR=$(grep "$SSL_CONNECTOR_BEGIN" $SERVER_XML_FILE_PATH)
               if [ -z "${TEST_SSL_CONNECTOR}" ]; then
                  echo "Connector disabled"
               else
                  echo "Connector enabled"
               fi
            else
               if [ $2 = "ajp" ]; then
                  echo "Checking ajp connector status"
                  TEST_AJP_CONNECTOR=$(grep "$AJP_CONNECTOR_BEGIN" $SERVER_XML_FILE_PATH)
                  if [ -z "${TEST_AJP_CONNECTOR}" ]; then
                     echo "Connector disabled"
                  else
                     echo "Connector enabled"
                  fi
               else
                  echo "Connector type not supported"
                  retVal=1
               fi
            fi
         else
            if [ $1 = "-file" ]; then
               echo "Process properties file"
               # Proceed based on the JVM that we are utilizing
               if [ -z "${TEST_IBM_JVM}" ]; then
                  # Assuming the Sun JVM in which case we need to load the xerces-j2 jar at boot time to avoid load class errors.
                  $JAVA_HOME/bin/java -Xbootclasspath/a:/usr/share/java/xerces-j2.jar -jar /usr/share/java/CASA/authtoken/bin/CasaTomcatConnectorEditor.jar $*
               else
                  $JAVA_HOME/bin/java -jar /usr/share/java/CASA/authtoken/bin/CasaTomcatConnectorEditor.jar $*
               fi
            else
               echo "Invalid operation requested"
               retVal=1
            fi
         fi 
      fi 
   fi
else
   echo "Invalid number of parameters"
            retVal=1
fi

