#!/bin/ksh

####################################################################################
# Defines
####################################################################################

AWK=/usr/bin/awk
CAT=/usr/bin/cat
ECHO=/usr/bin/echo
GREP=/usr/bin/grep
ID=/usr/bin/id
RM=/usr/bin/rm
MV=/usr/bin/mv
SED=/usr/bin/sed
UNIQ=/usr/bin/uniq

OSTYPE=`/bin/uname -s`
if [ "$OSTYPE" = "Linux" ]; then
  ECHO=/bin/echo
  GREP=/bin/grep
  MV=/bin/mv
  RM=/bin/rm
  SED=/bin/sed
fi

BELL_CHAR='\a'

PS_STATE_FILE="/etc/opt/SUNWps/PSConfig.properties"
SRA_STATE_FILE="/etc/opt/SUNWps/SRAConfig.properties"

LOG_FILE="/var/opt/SUNWam/debug/isconfig.log"

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

GrabConfig() {

  local FILE=$1
  local KEY=$2
  local SEPARATOR=$3

  ANSWER=`$GREP "^$KEY$SEPARATOR" $FILE | $UNIQ | $SED -e "s/$KEY$SEPARATOR//"` 
}

####################################################################################
# Make sure that the user is root.
####################################################################################

CheckUser() {

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

}

####################################################################################
# Make sure required state files are present.
####################################################################################

CheckStateFiles() {

    if [ ! -f $PS_STATE_FILE ]; then
      $ECHO "Error: $PS_STATE_FILE does not exist. $BELL_CHAR"
      exit 1
    fi
    
    if [ ! -f $SRA_STATE_FILE ]; then
      $ECHO "Error: $SRA_STATE_FILE does not exist. $BELL_CHAR"
      exit 1
    fi

}

####################################################################################
# Initialize all global variables.
####################################################################################

Initialize() {

    # Identity server base directory.
    GrabConfig $PS_STATE_FILE "IDSAME_BASEDIR" "="
    if [ "$ANSWER" != "" ]; then
      IDSAME_BASEDIR=$ANSWER
    else
      $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
      exit 1
    fi

    # Portal server base directory.
    GrabConfig $PS_STATE_FILE "BASEDIR" "="
    if [ "$ANSWER" != "" ]; then
      PS_BASEDIR=$ANSWER
    else
      $ECHO "Error: Cannot determine BASEDIR. $BELL_CHAR"
      exit 1
    fi

    # Portal server deploy URI.    
    GrabConfig $PS_STATE_FILE "DEPLOY_URI" "="
    if [ "$ANSWER" != "" ]; then
      DEPLOY_URI=$ANSWER
    else
      $ECHO "Error: Cannot determine DEPLOY_URI. $BELL_CHAR"
      exit 1
    fi

    # Identity server user naming attribute.
    IS_CONFIG_FILE="/etc/opt/SUNWam/config/AMConfig.properties"
    GrabConfig $IS_CONFIG_FILE "com.sun.identity.authentication.super.user" "="
    if [ "$ANSWER" != "" ]; then
      USER_NAMING_ATTR=`$ECHO $ANSWER | $CUT -f1 -d =`
    else
      $ECHO "Error: Cannot determine IDSAME_USER_NAMING. $BELL_CHAR"
      exit 1
    fi

    # Gateway domain.
    GrabConfig $SRA_STATE_FILE "GW_DOMAIN" "="
    if [ "$ANSWER" != "" ]; then
      GW_DOMAIN=$ANSWER
    else
      $ECHO "Error: Cannot determine GW_DOMAIN. $BELL_CHAR"
      exit 1
    fi

    # Others.
    FILE="/etc/opt/SUNWam/config/AMConfig.properties"
    ADMIN_DN=`$GREP "^com.sun.identity.authentication.super.user=" $FILE | $SED -e "s/com.sun.identity.authentication.super.user=//"`
    ROOT_DN=`$GREP "^com.iplanet.am.rootsuffix=" $FILE | $SED -e "s/com.iplanet.am.rootsuffix=//"`
    ORG_DN=`$GREP "^com.iplanet.am.defaultOrg=" $FILE | $SED -e "s/com.iplanet.am.defaultOrg=//"`
    if [ "$ORG_DN" != "$ROOT_DN" ]; then
        ORG_DN="$ORG_DN,$ROOT_DN"
    fi
    PEOPLE_DN="ou=People,$ORG_DN"
    AMADMIN="$IDSAME_BASEDIR/SUNWam/bin/amadmin"

}

####################################################################################
# Deletes all rewriter rulesets
####################################################################################

CleanRewriterRuleSets() {

    $ECHO "Deleting default_gateway_ruleset..." 
    $PS_BASEDIR/SUNWps/bin/rwadmin remove --runasdn "$ADMIN_DN" --password "${IDSAME_ADMIN_PASSWORD}" --rulesetid "default_gateway_ruleset"
    
    $ECHO "Deleting exchange_2000sp3_owa_ruleset..." 
    $PS_BASEDIR/SUNWps/bin/rwadmin remove --runasdn "$ADMIN_DN" --password "${IDSAME_ADMIN_PASSWORD}" --rulesetid "exchange_2000sp3_owa_ruleset"
    
    $ECHO "Deleting wml_ruleset..." 
    $PS_BASEDIR/SUNWps/bin/rwadmin remove --runasdn "$ADMIN_DN" --password "${IDSAME_ADMIN_PASSWORD}" --rulesetid "wml_ruleset"
    
    $ECHO "Deleting inotes_ruleset..." 
    $PS_BASEDIR/SUNWps/bin/rwadmin remove --runasdn "$ADMIN_DN" --password "${IDSAME_ADMIN_PASSWORD}" --rulesetid "inotes_ruleset"
    
    $ECHO "Deleting iplanet_mail_ruleset..." 
    $PS_BASEDIR/SUNWps/bin/rwadmin remove --runasdn "$ADMIN_DN" --password "${IDSAME_ADMIN_PASSWORD}" --rulesetid "iplanet_mail_ruleset"
    
}

####################################################################################
# Cleans gateway from IS
####################################################################################

CleanGateway() {

  local FILE="$PS_BASEDIR/SUNWps/export/scrubds/srapGateway.scrubds"
  
  $SED -e "s#%GW_DOMAIN%#$GW_DOMAIN#g" \
	   -e "s#%ORG_DN%#$ORG_DN#g" \
	   -e "s#%PEOPLE_DN%#$PEOPLE_DN#g" \
	   -e "s#%USER_NAMING_ATTR%#$USER_NAMING_ATTR#g" \
	   -e "s#%IDSAME_BASEDIR%#$IDSAME_BASEDIR#g" $FILE > ${FILE}.xml
	 
  $ECHO "Applying $FILE..."

  $AMADMIN --runasdn "$ADMIN_DN" --password "${IDSAME_ADMIN_PASSWORD}" --verbose --continue --data ${FILE}.xml >> ${LOG_FILE}

  $RM ${FILE}.xml

}

####################################################################################
# Cleans netfile from IS
####################################################################################

CleanNetfile() {

  local FILE="$PS_BASEDIR/SUNWps/export/scrubds/srapNetfile.scrubds"
  
  $SED -e "s#%IDSAME_BASEDIR%#$IDSAME_BASEDIR#g" \
       -e "s#%ORG_DN%#$ORG_DN#g" \
       -e "s#%DEPLOY_URI%#$DEPLOY_URI#g" $FILE > ${FILE}.xml
	 
  $ECHO "Applying $FILE..."

  $AMADMIN --runasdn "$ADMIN_DN" --password "${IDSAME_ADMIN_PASSWORD}" --verbose --continue --data ${FILE}.xml >> ${LOG_FILE}
  
  $RM ${FILE}.xml

}

####################################################################################
# Cleans netlet from IS
####################################################################################

CleanNetlet() {

  local FILE="$PS_BASEDIR/SUNWps/export/scrubds/srapNetlet.scrubds"
  
  $SED -e "s#%IDSAME_BASEDIR%#$IDSAME_BASEDIR#g" \
       -e "s#%ORG_DN%#$ORG_DN#g" \
       -e "s#%DEPLOY_URI%#$DEPLOY_URI#g" $FILE > ${FILE}.xml
	 
  $ECHO "Applying $FILE..."

  $AMADMIN --runasdn "$ADMIN_DN" --password "${IDSAME_ADMIN_PASSWORD}" --verbose --continue --data ${FILE}.xml  >> ${LOG_FILE}

  $RM ${FILE}.xml

}

####################################################################################
# Deletes all SRA services from IS
####################################################################################

DeleteServices() {

    $ECHO "Deleting gateway access list service..."
    $AMADMIN -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" --verbose -r srapGatewayAccessService  >> ${LOG_FILE}
    
    $ECHO "Deleting gateway service..."
    $AMADMIN -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" --verbose -r srapGatewayService  >> ${LOG_FILE}
    
    $ECHO "Deleting netfile service..."
    $AMADMIN -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" --verbose -r srapNetFileService  >> ${LOG_FILE}
    
    $ECHO "Deleting netlet service..."
    $AMADMIN -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" --verbose -r srapNetletService  >> ${LOG_FILE}
    
}

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

# Make sure that the user is root.
CheckUser

# Make sure that the required state files are present.
CheckStateFiles

# Initialize all global variables.
Initialize

# Clean rewriter rulesets from IS.
CleanRewriterRuleSets

# Clean gateway support from IS
CleanGateway

# Clean netfile from IS.
CleanNetfile

# Clean  netlet from IS.
CleanNetlet

# Delete all SRA services.
DeleteServices

exit 0
