#!/usr/bin/ksh

ps_props=/etc/opt/SUNWps/PSConfig.properties
sra_props=/etc/opt/SUNWps/SRAConfig.properties
gw_props=/etc/opt/SUNWps/GWConfig.properties

if [ -z "`echo $AWK`" ]; then
    AWK=/usr/bin/awk
fi
if [ -z "`echo $BC`" ]; then
    BC=/usr/bin/bc
fi
if [ -z "`echo $CAT`" ]; then
    CAT=/usr/bin/cat
fi
if [ -z "`echo $CHMOD`" ]; then
    CHMOD=/usr/bin/chmod
fi
if [ -z "`echo $COMM`" ]; then
    COMM=/usr/bin/comm
fi
if [ -z "`echo $CP`" ]; then
    CP=/usr/bin/cp
fi
if [ -z "`echo $DF`" ]; then
    DF=/usr/bin/df
fi
if [ -z "`echo $DU`" ]; then
    DU=/usr/bin/du
fi
if [ -z "`echo $ECHO`" ]; then
    ECHO=/usr/bin/echo
fi
if [ -z "`echo $GETENT`" ]; then
    GETENT=/usr/bin/getent
fi
if [ -z "`echo $GETTEXT`" ]; then
    GETTEXT=/usr/bin/gettext
fi
if [ -z "`echo $LN`" ]; then
    LN=/usr/bin/ln
fi
if [ -z "`echo $GREP`" ]; then
    GREP=/usr/bin/grep
fi
if [ -z "`echo $MKDIR`" ]; then
    MKDIR=/usr/bin/mkdir
fi
if [ -z "`echo $MV`" ]; then
    MV=/usr/bin/mv
fi
if [ -z "`echo $PERL`" ]; then
    PERL=/usr/bin/perl
fi
if [ -z "`echo $PING`" ]; then
    PING=/usr/sbin/ping
fi
if [ -z "`echo $PS`" ]; then
    PS=/usr/bin/ps
fi
if [ -z "`echo $RM`" ]; then
    RM=/usr/bin/rm
fi
if [ -z "`echo $SED`" ]; then
    SED=/usr/bin/sed
fi
if [ -z "`echo $TOUCH`" ]; then
    TOUCH=/usr/bin/touch
fi
if [ -z "`echo $UNIQ`" ]; then
    UNIQ=/usr/bin/uniq
fi
if [ -z "`echo $WC`" ]; then
    WC=/usr/bin/wc
fi
if [ -z "`echo $EXPR`" ]; then
    EXPR=/usr/bin/expr
fi

if [ "x$1" == "x--menu" ]; then
  print "`$GETTEXT 'SRA Migration'`"
  exit 0
fi

gatewayInstance=migrated
srapBaseDir=$importDir/srap
gatewayBaseDir=$srapBaseDir/gateway
netletBaseDir=$srapBaseDir/netlet
netfileBaseDir=$srapBaseDir/netfile
gatewayLDIFDir=$gatewayBaseDir/ldif
gatewayXMLDir=$gatewayBaseDir/xml
netletXMLDir=$netletBaseDir/xml
netfileXMLDir=$netfileBaseDir/xml
gatewayXmlComponent=$gatewayXMLDir/srapGatewayConfig.xml
# This is specified in importps
DPADMIN=$dpadmin
ADMIN_DN=$ADMINDN

#This XML contains the subconfig for the migrated gateway instance
gatewayPlatformConf=/etc/opt/SUNWps/platform.conf-orig
gatewayPlatformConfPrefix=/etc/opt/SUNWps/platform.conf

certDir=/etc/opt/SUNWps/cert/$gatewayInstance

error=0
if [ -z "`$ECHO $IDSAME_BASEDIR`" ]; then
    GrabConfig "IDSAME_BASEDIR" $ps_props "="
    IDSAME_BASEDIR=$ANSWER_CONFIG
fi

#LD_LIBRARY_PATH=${IDSAME_BASEDIR}/SUNWam/lib/solaris/sparc:${IDSAME_BASEDIR}/SUNWam/servers/bin/https/lib:${LD_LIBRARY_PATH}

GrabConfig "DEPLOY_TYPE" $ps_props "="
IDSAME_DEPLOY_TYPE=$ANSWER_CONFIG

LD_LIBRARY_PATH=/usr/lib/mps:$LD_LIBRARY_PATH

if [ "x$IDSAME_DEPLOY_TYPE" = "xiWS" ] || [ "x$IDSAME_DEPLOY_TYPE" = "xIWS" ]; then
  LD_LIBRARY_PATH=/usr/lib/mps:${LD_LIBRARY_PATH}:${IDSAME_BASEDIR}/SUNWam/lib/solaris/sparc:${IDSAME_BASEDIR}/SUNWam/servers/bin/https/lib
elif [ "x$IDSAME_DEPLOY_TYPE" = "xWEBSPHERE" ]; then
  LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${IDSAME_BASEDIR}/SUNWam/lib/solaris/sparc/jss:${IDSAME_BASEDIR}/SUNWam/lib/solaris/sparc
else
  LD_LIBRARY_PATH=/usr/lib/mps:${LD_LIBRARY_PATH}:${IDSAME_BASEDIR}/SUNWam/lib/solaris/sparc/ias
fi
export LD_LIBRARY_PATH

#Check for Lihue installation, if detected migrate the 3.0 gateway certificates
if [ ! -d $certDir ]; then
    $MKDIR -p $certDir
fi

if [ -d $srapBaseDir/gatecert ]; then
    $CP -r $srapBaseDir/gatecert/* $certDir
    $CP -r $srapBaseDir/gatecert/.* $certDir
    if [ $? -ne 0 ]; then
        error=1
    else
        if [ ! -f $gatewayPlatformConf ]; then
            print "`$GETTEXT 'Error gateway platform configuration file does not exist.'`" | tee -a $errorFile $reportFile
            #exit 1
            # Dont abort , just log this and continue.
        else
            $CP $gatewayPlatformConf $gatewayPlatformConfPrefix.$gatewayInstance
            MIG_CERTDIR="\/etc\/opt\/SUNWps\/cert\/migrated"
            FILE=$gatewayPlatformConfPrefix.$gatewayInstance

            GrabConfig "BASEDIR" $ps_props "="
            BASEDIR=$ANSWER_CONFIG
            PRODUCT_DIR=SUNWps

            JDK_PKGINFO_FILE="/var/sadm/pkg/SUNWpsjdk/pkginfo"
            IDSAME_PKGINFO_FILE="/var/sadm/pkg/SUNWamsvc/pkginfo"
            IDSAME_SDK_PKGINFO_FILE="/var/sadm/pkg/SUNWamsdk/pkginfo"

            if [ -f $JDK_PKGINFO_FILE ]; then
              GrabConfig "JDK_PATH" $JDK_PKGINFO_FILE "="
              JAVA_HOME=$ANSWER_CONFIG
            elif [ -f $IDSAME_PKGINFO_FILE ]; then
              GrabConfig "JDK_BASE_DIR" $IDSAME_PKGINFO_FILE "="
              JAVA_HOME=$ANSWER_CONFIG
            elif [ -f $IDSAME_SDK_PKGINFO_FILE ]; then
              GrabConfig "JDK_BASE_DIR" $IDSAME_SDK_PKGINFO_FILE "="
              JAVA_HOME=$ANSWER_CONFIG
            else
              JAVA_HOME="/usr/j2se"
            fi

            GrabConfig "SERVER_PROTOCOL" $ps_props "="
            SERVER_PROTOCOL=$ANSWER_CONFIG
            GrabConfig "SERVER_HOST" $ps_props "="
            SERVER_HOST=$ANSWER_CONFIG
            GrabConfig "SERVER_PORT" $ps_props "="
            SERVER_PORT=$ANSWER_CONFIG
            SERVER_URL=${SERVER_PROTOCOL}://${SERVER_HOST}:${SERVER_PORT}
            GrabConfig "GW_HOST" $gw_props "="
            GW_HOST=$ANSWER_CONFIG
            GrabConfig "GW_IP" $gw_props "="
            GW_IP=$ANSWER_CONFIG
            GrabConfig "GW_PORT" $gw_props "="
            GW_PORT=$ANSWER_CONFIG
            GrabConfig "GW_VIRTUAL_HOST" $gw_props "="
            GW_VIRTUAL_HOST=$ANSWER_CONFIG
            GrabConfig "DEPLOY_URI" $ps_props "="
            DEPLOY_URI=$ANSWER_CONFIG

            GrabConfig "GW_PROTOCOL" $gw_props "="
            GW_PROTOCOL=$ANSWER_CONFIG

            $SED -e "s#SERVER_PROTO#${SERVER_PROTOCOL}#g" \
               -e "s#\${JAVA_BASE_DIR}#${JAVA_HOME}#g" \
               -e "s#SERVER_HOST#${SERVER_HOST}#g" \
               -e "s#SERVER_PORT#${SERVER_PORT}#g" \
               -e "s#SERVER_URL#$SERVER_URL#g" \
               -e "s#GW_HOST#${GW_HOST}#g" \
               -e "s#GATEWAY_IPADDR#${GW_IP}#g" \
               -e "s#GW_PORT#${GW_PORT}#g" \
               -e "s#GW_VIRTUAL_HOST#$GW_VIRTUAL_HOST#g" \
               -e "s#GW_PROTOCOL#${GW_PROTOCOL}#g" \
               -e "s#DEPLOY_URI#${DEPLOY_URI}#g" \
               -e "s#CERT_DB_DIR#$MIG_CERTDIR#g" \
            $FILE > $FILE-tmp

            $CP $FILE-tmp $FILE
            $RM $FILE-tmp

            if [ "x${GW_IP}" != "x" ]; then
              $GREP "gateway.bindipaddress=" $FILE > /dev/null 2>&1
              if [ $? -ne 0 ]; then
                $ECHO "" >> $FILE
                $ECHO "gateway.bindipaddress=${GW_IP}" >> $FILE
              fi
            fi

            $GREP "gateway.sockretries=" $FILE > /dev/null 2>&1
            if [ $? -ne 0 ]; then
              $ECHO "" >> $FILE
              $ECHO "gateway.sockretries=3" >> $FILE
            fi

            print "`$GETTEXT 'Gateway platform configuration file created.'`" | tee -a $errorFile $reportFile
        fi
    fi
else
     print "`$GETTEXT 'No certificates to copy'`" | tee -a $errorFile $reportFile
fi

if [ -z "`$ECHO $libDir`" ]; then
    libDir=`(cd $scriptDir/../lib;pwd)`
fi

if [ -z "`$ECHO $BASEDIR`" ]; then
    GrabConfig "BASEDIR" $ps_props "="
    BASEDIR=$ANSWER_CONFIG
fi

export SHARE_DIR=/usr/share/lib
MIGCLASSPATH="${BASEDIR}/SUNWps/web-src/WEB-INF/lib/gwservices.jar:${IDSAME_BASEDIR}/SUNWam/lib/acmecrypt.jar:${SHARE_DIR}/activation.jar:${IDSAME_BASEDIR}/SUNWam/lib/activation.jar:${IDSAME_BASEDIR}/SUNWam/lib/am_sdk.jar:${IDSAME_BASEDIR}/SUNWam/lib:${IDSAME_BASEDIR}/SUNWam/lib/am_services.jar:${IDSAME_BASEDIR}/SUNWam/lib/am_sso_provider.jar::${SHARE_DIR}/jaxp-api.jar:${IDSAME_BASEDIR}/SUNWam/lib/iaik_ssl.jar:${IDSAME_BASEDIR}/SUNWam/lib/jaas.jar:${SHARE_DIR}/SUNWam/lib/jaxp.jar:${SHARE_DIR}/mps/jss3.jar:${SHARE_DIR}/mail.jar:${IDSAME_BASEDIR}/SUNWam/lib/servlet.jar:${IDSAME_BASEDIR}/SUNWam/lib/swec.jar:${SHARE_DIR}/xercesImpl.jar:${IDSAME_BASEDIR}/SUNWam/locale:${libDir}/srapmigration.jar"

if [ $error -eq 1 ]; then
    print "`$GETTEXT 'Error in certificate copy'`" | tee -a $errorFile $reportFile
    exit 1
fi

print "`$GETTEXT 'Create the policy templates'`" | tee -a $errorFile $reportFile

# Create the policy templates
$amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $gatewayXMLDir/authConfig.xml 2>&1 | tee -a $errorFile $reportFile
checkError $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $gatewayXMLDir/authConfig.xml

# import netlet specific attributes
for x in .netlet_complete; do
    if [ -f $srapBaseDir/$x ]; then

        if [ ! -d $netletXMLDir ];then
            print "`$GETTEXT 'Error netlet import directory does not exist.'`" | tee -a $errorFile $reportFile
            exit 1
        else
            $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $netletXMLDir/NetletOrgAttrs.xml 2>&1 | tee -a $errorFile $reportFile
            checkError $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $netletXMLDir/NetletOrgAttrs.xml
            $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $netletXMLDir/NetletRoleAttrs.xml 2>&1 | tee -a $errorFile $reportFile
            checkError $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $netletXMLDir/NetletRoleAttrs.xml
            $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $netletXMLDir/NetletPolicies.xml 2>&1 | tee -a $errorFile $reportFile
            checkError $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $netletXMLDir/NetletPolicies.xml
            # Lower the priority of the assigned policies.
            #if [ -f $netletXMLDir/orgList ]; then
            #   print "`$GETTEXT 'Lowering the priorities of the assigned Netlet policies.'`" | tee -a $errorFile $reportFile
            #   ${JAVA_HOME}/bin/java -classpath ${MIGCLASSPATH}  -Dcom.iplanet.coreservices.configpath=${IDSAME_BASEDIR}/SUNWam/config/ums -Dmax_conn_pool=10 -Dmin_conn_pool=1 -DLOCALE_STRING=${LOCALE_STRING} migration.modules.srap.ldap.AssignPolicyPriority "$ADMIN_DN" "$DS_DIRMGR_PASSWORD" $netletXMLDir/orgList netlet
            #fi
        fi
         print "`$GETTEXT 'Netlet data migrated.'`" | tee -a $errorFile $reportFile
    fi
done

# import Netfile specific attributes

for x in .netfile_complete; do
    if [ -f $srapBaseDir/$x ]; then

        if [ ! -d $netfileXMLDir ];then
              print "`$GETTEXT 'Error netfile import directory does not exist.'`" | tee -a $errorFile $reportFile
            exit 1
        else
            $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $netfileXMLDir/NetfileOrgAttrs.xml 2>&1 | tee -a $errorFile $reportFile
            checkError $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $netletXMLDir/NetfileOrgAttrs.xml
            $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $netfileXMLDir/NetfileRoleAttrs.xml 2>&1 | tee -a $errorFile $reportFile
            checkError $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $netletXMLDir/NetfileRoleAttrs.xml
            $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $netfileXMLDir/NetfilePolicies.xml 2>&1 | tee -a $errorFile $reportFile
            checkError $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $netfileXMLDir/NetfilePolicies.xml
        fi
        print "`$GETTEXT 'Netfile data migrated.'`" | tee -a $errorFile $reportFile
    fi
done

# import Gateway sub-config
#Check for the Gateway support package, if found do the Gateway migration

  FILE="/var/opt/SUNWps/tmp/migSUNWpsgws_requests.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}/SUNWam/dtd/amAdmin.dtd"
>
<Requests>
  <SchemaRequests serviceName="iPlanetAMAdminConsoleService" SchemaType="Organization">
    <AddDefaultValues>
      <AttributeValuePair>
        <Attribute name="iplanet-am-required-services"/>
        <Value>srapGatewayAccessService</Value>
      </AttributeValuePair>
    </AddDefaultValues>
  </SchemaRequests>
</Requests>
EOF
  print "`$GETTEXT 'Adding srapGatewayAccessService to required services list.'`"
  $amadmin --runasdn "$ADMIN_DN" --password "$DS_DIRMGR_PASSWORD" --verbose --continue --data $FILE

    for x in .gateway_complete; do
        if [ -f $srapBaseDir/$x ]; then
            print "`$GETTEXT 'Importing gateway data.'`" | tee -a $errorFile $reportFile
            cd $ldapSharedDir
            if [  -f $gatewayLDIFDir/user.ldif ]; then
                ./ldapmodify -c -h $DS_HOST -p $DS_PORT -D "$DS_DIRMGR_DN" -w "$DS_DIRMGR_PASSWORD" -f $gatewayLDIFDir/user.ldif 2>&1 | tee -a $errorFile $reportFile
                checkError ./ldapmodify -c -h $DS_HOST -p $DS_PORT -D "$DS_DIRMGR_DN" -w "$DS_DIRMGR_PASSWORD" -f $gatewayLDIFDir/user.ldif
            fi
            if [ ! -d $gatewayXMLDir ];then
                print "`$GETTEXT 'Error gateway import directory does not exist.'`" | tee -a $errorFile $reportFile
                exit 1
            else
                #Upload the gateway migrated instance.
                $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $gatewayXmlComponent 2>&1 | tee -a $errorFile $reportFile
                checkError $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $gatewayXmlComponent
            fi

            $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $gatewayXMLDir/GatewayOrgAttrs.xml 2>&1 | tee -a $errorFile $reportFile
            checkError $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $gatewayXMLDir/GatewayOrgAttrs.xml 2>&1
            $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $gatewayXMLDir/GatewayRoleAttrs.xml 2>&1 | tee -a $errorFile $reportFile
            checkError $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $gatewayXMLDir/GatewayRoleAttrs.xml 2>&1
            $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $gatewayXMLDir/GatewayPolicies.xml 2>&1 | tee -a $errorFile $reportFile
            checkError $amadmin -u "$ADMINDN" -w "$DS_DIRMGR_PASSWORD" --continue --data $gatewayXMLDir/GatewayPolicies.xml

            print "`$GETTEXT 'Gateway data migrated.'`" | tee -a $errorFile $reportFile
        fi
    done
