#!/bin/sh

# Copyright  2005 Sun Microsystems, Inc.  All rights reserved.
#
# Sun Microsystems, Inc. has intellectual property rights relating to
# technology embodied in the product that is described in this document.
# In particular, and without limitation, these intellectual property rights
# may include one or more of the U.S. patents listed at
# http://www.sun.com/patents and one or more additional patents or pending
# patent applications in the U.S. and in other countries.
#
# U.S. Government Rights - Commercial software.  Government users are subject
# to the Sun Microsystems, Inc. standard license agreement and applicable
# provisions of the FAR and its supplements.
#
# Use is subject to license terms.
#
# This distribution may include materials developed by third parties.Sun,
# Sun Microsystems and  the Sun logo are trademarks or registered trademarks
# of Sun Microsystems, Inc. in the U.S. and other countries.  
#
# Copyright  2005 Sun Microsystems, Inc. Tous droits rservs.
# Sun Microsystems, Inc. dtient les droits de proprit intellectuels relatifs
#  la technologie incorpore dans le produit qui est dcrit dans ce document.
# En particulier, et ce sans limitation, ces droits de proprit
# intellectuelle peuvent inclure un ou plus des brevets amricains lists
#  l'adresse http://www.sun.com/patents et un ou les brevets supplmentaires
# ou les applications de brevet en attente aux Etats - Unis et dans les
# autres pays.
#
# L'utilisation est soumise aux termes du contrat de licence.
#
# Cette distribution peut comprendre des composants dvelopps par des
# tierces parties.
#
# Sun,  Sun Microsystems et  le logo Sun sont des marques de fabrique ou des
# marques dposes de Sun Microsystems, Inc. aux Etats-Unis et dans
# d'autres pays.

#
#  set shell variables
#

OS=`uname`
if [ $OS = "Linux" ]; then
    CONFIG_DIR=/etc/opt/sun/identity/config
else
    CONFIG_DIR=/etc/opt/SUNWam/config
fi
export CONFIG_DIR
SVC_XML_DIR_IMPORT=${CONFIG_DIR}/xml
export SVC_XML_DIR_IMPORT


################################################################################
#create dit
#
################################################################################

create_dit() {
    # try multiple times
    i=2
    fail=0
    while [ $i != 3 ]
    do
        sleep $i
        ${PKGDIR}/bin/ldapmodify -D "$DIRMGRDN" -w "$DIRMGRPASSWD" -c -a -h $DS_HOST -p $DS_PORT -f $1
        fail=$?
        if [ $fail = 0 ]
        then
            break
        fi
        i=`expr $i + 1`
        $NECHO "`gettext 'sleep'` $i"
    done
    if [ $fail = 0 ]
    then
        $NECHO "`gettext 'ldif import succeeded.'`" >> $LOGFILE
    else
        $NECHO "`gettext 'ldif import failed.'`" >> $LOGFILE
    fi
}
################################################################################
# execute amadmin
################################################################################

## import service data into repository

import_service_data() {
    SVC_MODIFICATION_FILES=$*
    eval $NECHO "`gettext 'Applying service modifications $SVC_MODIFICATION_FILES'`"
    eval $NECHO "`gettext 'Applying service modifications $SVC_MODIFICATION FILES'`" >> $LOGFILE
    $ADMIN_CLI --runasdn "$AMADMIN_DN" -w "$ADMINPASSWD" -c -v -t ${SVC_MODIFICATION_FILES}
}

## import a new service into repository

import_new_service_schema() {
    NEW_SVC_SCHEMA=$*
    eval $NECHO "`gettext 'Importing new service'`" 
    eval $NECHO "`gettext 'Importing new service $NEW_SVC_SCHEMA'`" >> $LOGFILE
    $ADMIN_CLI --runasdn "$AMADMIN_DN" -w "$ADMINPASSWD" -c -v -s ${NEW_SVC_SCHEMA}
}

## migrate services tree in 6.3 DIT to Realm Tree

migrate_to_realm() {
    $ADMIN_CLI --runasdn "$AMADMIN_DN" -w "$ADMINPASSWD" -c -M $ROOT_AM_SUFFIX
}

### get value of an attribute in repository

get_attribute_value() {
    SVC_XML_FILE=$*
    ans=`$ADMIN_CLI --runasdn "$AMADMIN_DN" -w "$ADMINPASSWD" -t ${SVC_XML_FILE} |  grep "\[\]"`
    if [ "$ans" = "" ]; then
       return 1
    else
       return 0;
    fi
}

### check for existance of attribute in repository
check_attribute_exists() {
    SVC_XML_FILE=$*
    ans=`$ADMIN_CLI --runasdn "$AMADMIN_DN" -w "$ADMINPASSWD" -t ${SVC_XML_FILE}`
    if [ "$ans" = "Success 0: Successfully completed." ]; then
       return 0
    else
       return 1;
    fi   
}
