#!/bin/ksh

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

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

BELL_CHAR='\a'

STATE_FILE="$PS_CONFIG_DIR/PSConfig.properties"

###############################################
# 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
    $DPADMIN remove -t $TYPE -n "$OBJNAME" -u "$ADMIN_DN" -w "$ADMIN_PASSWORD" -d "$ORG_DN"
  else
    $DPADMIN remove -t $TYPE -n "$OBJNAME" -u "$ADMIN_DN" -w "$ADMIN_PASSWORD" -g
  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



#***
# Cleanup what of IS we can...
#***
$ECHO "Removing MA related services from list of required services..."
FILE="/tmp/scrubds_wireless.xml"
$CAT > $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>

</Requests>
EOF
$AMADMIN --runasdn "$ADMIN_DN" --password "${IDSAME_ADMIN_PASSWORD}" --data $FILE
$RM -f $FILE





#***
# Remove MA's IS services...
#***

$ECHO "Deleting services..."

FILE="/tmp/scrubds_wireless.xml"
$CAT > $FILE << EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests PUBLIC "-//iPlanet//Sun ONE Identity Server 6.0 Admin CLI DTD//EN" "jar://com/iplanet/am/admin/cli/amAdmin.dtd" >
<Requests>
  <ServiceConfigurationRequests serviceName="SunMobileAppABService">
    <DeleteAllServiceConfiguration userAtt="true"/>
  </ServiceConfigurationRequests>
  <ServiceConfigurationRequests serviceName="SunMobileAppCalendarService">
    <DeleteAllServiceConfiguration userAtt="true"/>
  </ServiceConfigurationRequests>
  <ServiceConfigurationRequests serviceName="SunMobileAppMailService">
    <DeleteAllServiceConfiguration userAtt="true"/>
  </ServiceConfigurationRequests>
</Requests>
EOF
$AMADMIN --runasdn "$ADMIN_DN" --password "${IDSAME_ADMIN_PASSWORD}" --data $FILE
$RM -f $FILE





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


###############################################
# Remove Mobile Services Attribute for On-line Help
###############################################
$ECHO "Deleting attributes added to iPlanetAMAdminConsoleService ServiceSchema..."
$CAT > $FILE << EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests PUBLIC "-//iPlanet//Sun ONE Identity Server 6.0 Admin CLI DTD//EN" "file:$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/dtd/amAdmin.dtd">
<Requests>
  <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 $FILE


exit 0
