#!/bin/ksh

DATE=/usr/bin/date
AWK=/usr/bin/awk
CHMOD=/usr/bin/chmod
CP=/usr/bin/cp
ECHO=/usr/bin/echo
GREP=/usr/bin/grep
ID=/usr/bin/id
INSTALLF=/usr/sbin/installf
LN=/usr/bin/ln
MKDIR=/usr/bin/mkdir
RM=/usr/bin/rm
SED=/usr/bin/sed
UNIQ=/usr/bin/uniq
CUT=/usr/bin/cut
MV=/usr/bin/mv
GETTEXT=/usr/bin/gettext

OSTYPE=`/bin/uname -s`
if [ "$OSTYPE" = "Linux" ]; then
  CHMOD=/bin/chmod
  CP=/bin/cp
  DATE=/bin/date
  ECHO=/bin/echo
  GREP=/bin/grep
  LN=/bin/ln
  MKDIR=/bin/mkdir
  MV=/bin/mv
  RM=/bin/rm
  SED=/bin/sed
fi

BELL_CHAR='\a'

ETC_DIR="/etc/opt/SUNWps"
IS_PRODUCT_DIR="SUNWam"

################################################################
# Exit if non root user is executing this script
################################################################

CheckUser() {

    if [ `$ID | $AWK '{print $1}'` != "uid=0(root)" ]; then
        $ECHO "You must be root user. $BELL_CHAR"
        exit 1
    fi

}

################################################################
# Get configuration from specified file
################################################################

GrabConfig() {

    GRABCONFIG_FILE=$1
    GRABCONFIG_KEY=$2
    GRABCONFIG_SEPARATOR=$3

    ANSWER=`$GREP "^$GRABCONFIG_KEY$GRABCONFIG_SEPARATOR" $GRABCONFIG_FILE | $UNIQ | $SED -e "s/$GRABCONFIG_KEY$GRABCONFIG_SEPARATOR//"`

}

###############################################
# Replace line in file
###############################################

ReplaceLine() {

  local FILE=$1
  local MATCH=$2
  local TEXT=$3

  $CP $FILE $FILE-tmp
  $SED -e "/$MATCH/ {
c\\
$TEXT
}" $FILE-tmp > $FILE
  $RM $FILE-tmp

}

################################################################
# Get the Identity server base directory reading from state file.
################################################################

GetISBaseDir() {

    NLP_STATE_FILE=${ETC_DIR}/NLPConfig.properties

    GrabConfig $NLP_STATE_FILE "IDSAME_BASEDIR" "="
    if [ "$ANSWER" != "" ]; then
        IS_BASEDIR=$ANSWER
    else
        IS_BASEDIR="IS_BASEDIR"
    fi

}

################################################################
# Unconfigures/Removes an instance of all netlet proxy.
################################################################

RemoveInstance() {

    local INSTANCE_NAME=$1

    # Stop the netlet proxy instance.
    ${NETLETPROXY} -n ${INSTANCE_NAME} stop

    # Remove the configuration state file.
    $RM -f ${ETC_DIR}/NLPConfig-${INSTANCE_NAME}.properties

    # Check if the configuration is being used by rewriter proxy.
    CONFIG_SHARED="n"

    RWP_CONFIG_FILE=${ETC_DIR}/RWPConfig-${INSTANCE_NAME}.properties

    if [ -f "$RWP_CONFIG_FILE" ]; then
        CONFIG_SHARED="y"
    fi

    # If the rewriter proxy is not using this config, remove the configuration.
    if [ "$CONFIG_SHARED" = "n" ]; then

      if [ "$PURGE_MODE" = "n" ]; then

        # Back-up the certificate database of this all netlet proxy instance
        $MV ${ETC_DIR}/cert/${INSTANCE_NAME} ${ETC_DIR}/cert/${INSTANCE_NAME}.${TIMESTAMP}

        # Back-up platform.conf file.
        $MV ${ETC_DIR}/platform.conf.${INSTANCE_NAME} ${ETC_DIR}/platform.conf.${INSTANCE_NAME}.${TIMESTAMP}
    
        # Back-up auth password file
        $MV ${ETC_DIR}/.auth_password.${INSTANCE_NAME} ${ETC_DIR}/.auth_password.${INSTANCE_NAME}.${TIMESTAMP}

      else 

        # Remove the certificate database
        $RM -rf ${ETC_DIR}/cert/${INSTANCE_NAME}

        # Remove platform.conf file
        $RM -f ${ETC_DIR}/platform.conf.${INSTANCE_NAME}
        
        # Remove auth password file.
        $RM -f ${ETC_DIR}/.auth_password.${INSTANCE_NAME}

      fi

      # Remove AMConfig config file.
      GetISBaseDir
      $RM -f /etc/opt/${IS_PRODUCT_DIR}/config/AMConfig-${INSTANCE_NAME}.properties

    else

      ReplaceLine ${ETC_DIR}/platform.conf.${INSTANCE_NAME} "netletproxy.port=" ""

    fi

}


################################################################
# Removes all netlet proxy instances
################################################################
RemoveAllInstances() {

    # Disble all netlet proxy watch dog.
    $NETLETPROXY watchdog off

    # Remove instances.
    for x in ${ETC_DIR}/platform.conf.*; do
        if [ -r $x ]; then
            INSTANCE_NAME=`$ECHO $x | $SED -e "s#${ETC_DIR}/platform.conf.##"`
            RemoveInstance $INSTANCE_NAME
        fi
    done

}

################################################################
# Init
################################################################

Init() {

    # Time stamp
    TIMESTAMP=`$DATE +%Y%m%d%H%M`

    # netletd script
    NETLETPROXY="/etc/init.d/netletd"

}

################################################################
# Main
################################################################

# Make sure root is executing this script.
CheckUser

# Initialize the timestamp
Init

# Check if the instances should be purged.
if [ "$1" == "--purge" ]; then
    PURGE_MODE="y"
    INSTANCE_NAME="$2"
else
    PURGE_MODE="n"
    INSTANCE_NAME="$1"
fi

# Remove the instance(s)
if [ "$INSTANCE_NAME" = "" ]; then 
    RemoveAllInstances
else
    RemoveInstance "$INSTANCE_NAME"
fi

exit 0
