#!/bin/ksh
# Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
#
# @(#)addEsmUser.sh	1.4 05/09/27
#
# This software is the confidential and proprietary information of Sun
# Microsystems, Inc. ("Confidential Information"). You shall not
# disclose such Confidential Information and shall use it only in
# accordance with the terms of the license agreement you entered into
# with Sun.
#
# SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
# THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
# TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
# ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
# DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
#

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

AWK=/usr/bin/awk
BASENAME=/usr/bin/basename
CAT=/usr/bin/cat
CP=/usr/bin/cp
ECHO=/usr/bin/echo
EXPR=/usr/bin/expr
GREP=/usr/bin/grep
ID=/usr/bin/id
LN=/usr/bin/ln
MV=/usr/bin/mv
NAWK=/usr/bin/nawk
RM=/usr/bin/rm
SED=/usr/bin/sed
UNIQ=/usr/bin/uniq

exitStatus=0;
ESM_BASE="`pkginfo -r SUNWesmportal`"
BASE_DIR="`pkginfo -r SUNWesmdb`/SUNWesmportal/config"
WEB_BASEDIR="`pkginfo -r SUNWwbsvr`"
BELL_CHAR='\a'
PS_FILE="PSConfig.properties"
STATE_FILE="/etc`pkginfo -r SUNWps`/SUNWps/${PS_FILE}"

usage() {
    echo 
    echo "usage: $PROGNAME -h"
    echo "       $PROGNAME username"
    echo 
    echo "Options:"
    echo "none: prints this message"
    echo "-h  : prints this message"
    echo "name: Adds named user"
    echo
    exit 1
}

get_opts() {
    while getopts :h gopt
    do 
	    case $gopt in
	    h) usage
	        ;;
	    *) ;;
	    esac
    done
    shift `$EXPR $OPTIND - 1`
}

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

###############################################
# Main
###############################################
PROGNAME=`$BASENAME $0`
USER_NAME="$1"
get_opts $*

# Need get_password () from esmlib.sh
. ${BASE_DIR}/../esmlib.sh

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

#
# Verify portlet exists
#
if [ -z "${USER_NAME}" ]; then
  $ECHO
  $ECHO "Usage: ${PROGNAME} username $BELL_CHAR"
  $ECHO
  exit 1
fi

#
# Need amAdmin password to perform these functions
#
$ECHO "Must supply amAdmin password. $BELL_CHAR"
get_password

#
# Need portal server config properties to perform these functions
#
if [ ! -f $STATE_FILE ]; then
  $ECHO
  $ECHO "Error: $STATE_FILE does not exist. $BELL_CHAR"
  $ECHO
  exit 1
fi

#
# Need portal server config properties to perform these functions
#
GrabConfig $STATE_FILE "IDSAME_BASEDIR" "="
if [ "$ANSWER" != "" ]; then
  IDSAME_BASEDIR=$ANSWER
else
  $ECHO
  $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
  $ECHO
  exit 1
fi

#
# Need portal server config properties to perform these functions
#
GrabConfig $STATE_FILE "BASEDIR" "="
if [ "$ANSWER" != "" ]; then
  PS_BASEDIR=$ANSWER
else
  $ECHO
  $ECHO "Error: Cannot determine BASEDIR. $BELL_CHAR"
  $ECHO
  exit 1
fi

#
# Need portal server config properties to perform these functions
#
GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
if [ "$ANSWER" != "" ]; then
  DEPLOY_INSTANCE=$ANSWER
else
  $ECHO
  $ECHO "Error: Cannot determine DEPLOY_INSTANCE. $BELL_CHAR"
  $ECHO
  exit 2
fi

FILE="$IDSAME_BASEDIR/SUNWam/lib/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=//"`
HOST=`$GREP "^com.iplanet.am.directory.host=" $FILE | $SED -e "s/com.iplanet.am.directory.host=//"`
if [ "$ORG_DN" != "$ROOT_DN" ]; then
  ORG_DN="$ORG_DN,$ROOT_DN"
fi
ESM_ORG_NAME="esmjss.com"
ORG_NAMING_ATTR="o"
ESM_ORG_DN="${ORG_NAMING_ATTR}=${ESM_ORG_NAME},${ORG_DN}"
SERVER_ADMIN="${WEB_BASEDIR}/https-admserv/config/magnus.conf"
SERVER_USER="${WEB_BASEDIR}/https-${DEPLOY_INSTANCE}/config/magnus.conf"

#
#DEBUG
#
#$ECHO
#$ECHO "STATE_FILE:\t${STATE_FILE}"
#$ECHO "JDK_DIR:\t${JDK_DIR}"
#$ECHO "IDSAME_BASEDIR:\t${IDSAME_BASEDIR}"
#$ECHO "PS_BASEDIR:\t${PS_BASEDIR}"
#$ECHO "ADMIN_DN:\t${ADMIN_DN}"
#$ECHO "ROOT_DN:\t${ROOT_DN}"
#$ECHO "ORG_DN:\t\t${ORG_DN}"
#$ECHO "HOST:\t\t${HOST}"
#$ECHO "BASE_DIR:\t\t${BASE_DI}"
#$ECHO

#
# Add Esm user
#
$ECHO "Create username $USER_NAME"
$CAT > /tmp/createConverted.xml << EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests
    PUBLIC "-//iPlanet//Sun ONE Identity Server 6.1 Admin CLI DTD//EN"
    "jar://com/iplanet/am/admin/cli/amAdmin.dtd"
>

<!--  CREATE REQUESTS -->

<Requests>
   <!-- Create Users -->
  <PeopleContainerRequests DN="ou=People,$ESM_ORG_DN">
    <CreateUser createDN="$USER_NAME">
      <AttributeValuePair>
	    <Attribute name="givenname"/>
	    <Value>StorEdge Management Portal User</Value>
      </AttributeValuePair>
      <AttributeValuePair>
	    <Attribute name="cn"/>
	    <Value>$USER_NAME</Value>
      </AttributeValuePair>
      <AttributeValuePair>
	    <Attribute name="sn"/>
	    <Value>StorEdge Management Portal User</Value>
      </AttributeValuePair>
      <AttributeValuePair>
	    <Attribute name="userPassword"/>
	    <Value>$USER_NAME</Value>
      </AttributeValuePair>
      <AttributeValuePair>
        <Attribute name="sunPortalDesktopDefaultChannelName"/>
        <Value>EsmTabContainer</Value>
      </AttributeValuePair>
      <AttributeValuePair>
        <Attribute name="sunPortalDesktopType"/>
        <Value>esmportal</Value>
      </AttributeValuePair>
    </CreateUser>
  </PeopleContainerRequests>
</Requests>	
EOF
$IDSAME_BASEDIR/SUNWam/bin/amadmin -v -u "$ADMIN_DN" -w "$password" --continue -t /tmp/createConverted.xml
if [ $? = 0 ]; then
    $ECHO "Create username $USER_NAME successful"
    $ECHO "Password set to $USER_NAME"
else
    $ECHO "Create username $USER_NAME FAILED"
fi
$RM -rf /tmp/createConverted.xml

exit $exitStatus
