#!/bin/ksh

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

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

BELL_CHAR='\a'

STATE_FILE="$PS_CONFIG_DIR/PSConfig.properties"
INPUT_FILE="/tmp/scrubds_map.xml"
LOG_FILE="$IDSAME_VAR_DIR/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//"` 
}

###############################################
# Remove an object from the display profile
###############################################
RemoveObjectFromDP() {
  local TYPE=$1
  local OBJNAME=$2
  local ADMIN_DN=$3
  local ADMIN_PASSWORD=$4
  local ORG_DN=$5

  #$ECHO Removing $TYPE $OBJNAME ...
  if [ "$ORG_DN" != "" ]; then
    $ECHO "remove -t $TYPE -n $OBJNAME -u $ADMIN_DN -w $ADMIN_PASSWORD -d $ORG_DN" >> $INPUT_FILE
  else
    $ECHO "remove -t $TYPE -n $OBJNAME -u $ADMIN_DN -w $ADMIN_PASSWORD -g" >> $INPUT_FILE
  fi
}

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

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

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

GrabConfig $STATE_FILE "IDSAME_BASEDIR" "="
if [ "$ANSWER" != "" ]; then
  IDSAME_BASEDIR=$ANSWER
else
  $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "BASEDIR" "="
if [ "$ANSWER" != "" ]; then
  PS_BASEDIR=$ANSWER
else
  $ECHO "Error: Cannot determine BASEDIR. $BELL_CHAR"
  exit 1
fi

AMADMIN="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/bin/amadmin"
DPADMIN="$PS_BASEDIR/$PS_PRODUCT_DIR/bin/dpadmin"

FILE="$IDSAME_CONFIG_DIR/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
SCHEMA_DN="cn=schema"
PEOPLE_DN="ou=People,$ORG_DN"
AUTHLESS_DN="uid=authlessanonymous,$PEOPLE_DN"

#***
# Remove "isconfig" state concerning MA request and service files.
#
#  TODO:  This is a temporary fix until the installer framework provides
#  a real interface with which to deregister the request and service files.
#***

SERVICE_DIR="$PS_VAR_DIR/tmp/service"
REQUEST_DIR="$PS_VAR_DIR/tmp/request"

DEREG_FILES="SunAMClientData SunMobileAppABService SunMobileAppCalendarService SunMobileAppMailService popCollections psWireless"

for F in $DEREG_FILES; do
  $RM -f $SERVICE_DIR/$F.xml $REQUEST_DIR/$F.xml
done

#***
# Remove MA's DP entries...
#***

CORE_CONTAINERS="JSPNativeContainer TemplateNativeContainer WirelessTemplateContentChannel WirelessTemplateLayoutChannel WirelessTemplateClientConfigChannel WirelessJSPClientConfigContainer VoiceJSPDesktopContainer WirelessJSPContentChannel WirelessJSPLayoutChannel JSPRenderingLayoutChannel JSPRenderingContentChannel JSPRenderingEditContainer JSPRenderingContainer JSPRenderingErrorChannel WirelessDesktopDispatcher"

SAMPLE_CONTAINERS="WirelessDesktopDispatcher JSPNativeContainer TemplateNativeContainer VoiceJSPDesktopContainer JSPRenderingContainer"

PROVIDERS="WirelessDesktopDispatcherProvider WirelessTemplateDesktopProvider WirelessTemplateContentProvider WirelessTemplateLayoutProvider WirelessTemplateClientConfigProvider WirelessJSPDesktopProvider JSPRenderingProvider JSPRenderingContainerProvider RenderingWrappingProvider JSPSingleRenderingContainerProvider"

SAMPLE_ANONCHANNELS="Wrapped_Bookmark Wrapped_Login Wrapped_Notes Register Wrapped_Register Wrapped_SampleXML AnonJSPNativeContainer AnonJSPRenderingContainer VoiceAnonJSPDesktopContainer WirelessDesktopDispatcher WirelessTemplateLayoutChannel WirelessTemplateContentChannel JSPRenderingContentChannel JSPRenderingLayoutChannel WirelessJSPContentChannel WirelessJSPLayoutChannel"


if [ -n "$SAMPLEPORTAL_PRESENT" ]
then
  $ECHO "Removing sample Wireless Containers from display profile..."
  for J in $SAMPLE_CONTAINERS; do
    RemoveObjectFromDP "channel" $J "$ADMIN_DN" "${IDSAME_ADMIN_PASSWORD}" "$ORG_DN"
  done
  $ECHO ""

  $ECHO "Removing sample Wireless Anonymous channels from display profile..."
  for I in $SAMPLE_ANONCHANNELS; do
    RemoveObjectFromDP "channel" $I $ADMIN_DN "${IDSAME_ADMIN_PASSWORD}" "$AUTHLESS_DN"
  done
fi


$ECHO "Removing core Wireless Containers from display profile..."
for J in $CORE_CONTAINERS; do
  RemoveObjectFromDP "channel" $J "$ADMIN_DN" "${IDSAME_ADMIN_PASSWORD}"
done
$ECHO ""

$ECHO "Removing Wireless Providers from display profile..."
for K in $PROVIDERS; do
  RemoveObjectFromDP "provider" $K "$ADMIN_DN" "${IDSAME_ADMIN_PASSWORD}"
done
$ECHO ""

$ECHO "Removing Data from display profile..."
#$DPADMIN batch --continue --runasdn $ADMIN_DN --password $ADMIN_PASSWORD --file $INPUT_FILE

#***
# Cleanup what of IS we can...
#***
$ECHO "Removing MA related services from list of required services..."
SCRUB_XML_FILE="/tmp/scrubds_wireless.xml"
$CAT > $SCRUB_XML_FILE << EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests
    PUBLIC "-//iPlanet//iDSAME 5.0 Admin CLI DTD//EN"
    "file:$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/dtd/amAdmin.dtd"
>

<Requests>
  <OrganizationRequests DN="$ORG_DN">
    <RemoveServiceTemplateAttributeValues serviceName="iPlanetAMAdminConsoleService" schemaType="Organization">
      <AttributeValuePair>
        <Attribute name="iplanet-am-required-services"/>
        <Value>SunMobileAppABService</Value>
        <Value>SunMobileAppCalendarService</Value>
        <Value>SunMobileAppMailService</Value>
      </AttributeValuePair>
    </RemoveServiceTemplateAttributeValues>
  </OrganizationRequests>

<!-- Remove MA's IS services... -->
<!-- Deleting services... -->

 <ServiceConfigurationRequests serviceName="SunMobileAppABService">
    <DeleteAllServiceConfiguration userAtt="true"/>
  </ServiceConfigurationRequests>
  <ServiceConfigurationRequests serviceName="SunMobileAppCalendarService">
    <DeleteAllServiceConfiguration userAtt="true"/>
  </ServiceConfigurationRequests>
  <ServiceConfigurationRequests serviceName="SunMobileAppMailService">
    <DeleteAllServiceConfiguration userAtt="true"/>
  </ServiceConfigurationRequests>

<!-- Remove Mobile Services Attribute for On-line Help -->
<!-- Deleting attributes added to iPlanetAMAdminConsoleService ServiceSchema... -->

  <SchemaRequests serviceName="iPlanetAMAdminConsoleService" SchemaType="Organization">
    <RemovePartialDefaultValues>
      <AttributeValuePair>
        <Attribute name="iplanet-am-admin-console-online-help"/>
        <Value>Mobile_Help|/ma/mobileservices.html|SunMobileAppMailService</Value>
      </AttributeValuePair>
    </RemovePartialDefaultValues>
  </SchemaRequests>

</Requests>
EOF

$AMADMIN --runasdn "$ADMIN_DN" --password "${IDSAME_ADMIN_PASSWORD}" --data $SCRUB_XML_FILE
$RM -f $SCRUB_XML_FILE

exit 0
