#!/bin/ksh

###############################################
# Sourcing macros
DIRNAME=/usr/bin/dirname
. `$DIRNAME $0`/../lib/psmacros
###############################################

BELL_CHAR='\a'

################################################################
# 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//"`

}

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

GetISBaseDir() {

    GW_STATE_FILE=$PS_CONFIG_DIR/GWConfig.properties

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

}

################################################################
# Unconfigures/Removes an instance of gateway.
################################################################

RemoveInstance() {

    local INSTANCE_NAME=$1

    # Stop the gateway instance.
    ${GATEWAY} -n ${INSTANCE_NAME} stop

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

        # Back-up the certificate database of this gateway instance
        $MV $PS_CONFIG_DIR/cert/${INSTANCE_NAME} $PS_CONFIG_DIR/cert/${INSTANCE_NAME}.${TIMESTAMP}

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

    else 

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

        # Remove platform.conf file
        $RM -f $PS_CONFIG_DIR/platform.conf.${INSTANCE_NAME}
        
        # Remove auth password file.
        $RM -f $PS_CONFIG_DIR/.auth_password.${INSTANCE_NAME}
    fi
        
    # Remove AMConfig config file.
    GetISBaseDir
    $RM -f $IDSAME_CONFIG_DIR/config/AMConfig-${INSTANCE_NAME}.properties

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

}


################################################################
# Removes all gateway instances
################################################################
RemoveAllInstances() {

    # Disble gateway watch dog.
    $GATEWAY watchdog off

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

}

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

Init() {

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

    # Gateway script
    GATEWAY="/etc/init.d/gateway"

}

################################################################
# 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
