#!/bin/sh 

AWK=/usr/bin/awk
NAWK=/usr/bin/nawk
ECHO=/usr/bin/echo
MV=/usr/bin/mv
CP=/usr/bin/cp
RM=/usr/bin/rm
GREP=/usr/bin/grep
SED=/usr/bin/sed
HEAD=/usr/bin/head
TOUCH=/usr/bin/touch

VERSION_FILE=.version

check_for_sdk() {
   pkginfo -q SUNWamsdk
   if [ $? -eq 0 ]; then
      sdk="yes"
   else
      sdk="no"
   fi
}

check_for_svc() {
   pkginfo -q SUNWamsvc
   if [ $? -eq 0 ]; then
      svc="yes"
   else
      svc="no"
   fi
}

check_for_con() {
   pkginfo -q SUNWamcon
   if [ $? -eq 0 ]; then
      con="yes"
   else
      con="no"
   fi
}

check_for_iws() {
   pkginfo -q SUNWamsws
   if [ $? -eq 0 ]; then
      iws="yes"
   else
      iws="no"
   fi
}

check_for_fcd() {
   pkginfo -q SUNWamfcd
   if [ $? -eq 0 ]; then
      fcd="yes"
   else
      fcd="no"
   fi
}

check_for_cds() {
   pkginfo -q SUNWamcds
   if [ $? -eq 0 ]; then
      cds="yes"
   else
      cds="no"
   fi
}

check_for_sam() {
   pkginfo -q SUNWamsam
   if [ $? -eq 0 ]; then
      sam="yes"
   else
      sam="no"
   fi
}

check_for_sac() {
   pkginfo -q SUNWamsac
   if [ $? -eq 0 ]; then
      sac="yes"
   else
      sac="no"
   fi
}

check_for_wlc() {
   pkginfo -q SUNWamwlc
   if [ $? -eq 0 ]; then
      wlc="yes"
   else
      wlc="no"
   fi
}

check_for_wsc() {
   pkginfo -q SUNWamwsc
   if [ $? -eq 0 ]; then
      wsc="yes"
   else
      wsc="no"
   fi
}


get_base_dir() {
  check_for_svc
  if [ "$svc" = "yes" ]; then
    pkg="SUNWamsvc"
  fi

#  set_platform_locale_to_C
  BASE_DIR=`pkginfo -r $pkg`
  export BASE_DIR
#  restore_platform_default_locale
}

##############################################################
#
#
replace_line() {
  file=$1
  match=$2
  new=$3

  $CP $file $file-orig-$$
  sed -e "
/$match/ {
c\\
$new
}" $file > $file-tmp
mv $file-tmp $file
rm $file-orig-$$
}

###############################################
# Replace block
###############################################
ReplaceBlock() {
  FILE=$1
  START_TAG=$2
  END_TAG=$3
  NEW_TEXT_FILE=$4

  $CP $FILE $FILE-tmp
  $NAWK 'BEGIN {
           IN_REPLACE = 0;
         };
         {
           if (index($0, START_TAG) > 0) {
            while (getline str < NEW_TEXT_FILE > 0) {
              if (length(str) > 0) {
                printf("%s\n", str);
              }
            }
             IN_REPLACE = 1;
           } else if (index($0, END_TAG) > 0) {
             if (IN_REPLACE == 0) {
               printf("%s\n", $0);
             }
             IN_REPLACE = 0;
           } else {
             if (IN_REPLACE == 0) {
               printf("%s\n", $0);
             }
           }
         };
         END {};' START_TAG="$START_TAG" END_TAG="$END_TAG" NEW_TEXT_FILE="$NEW_TEXT_FILE" $FILE-tmp > $FILE
  $RM -f $FILE-tmp
}

###############################################
# Delete block
###############################################
DeleteBlock() {
  FILE=$1
  START_TAG=$2
  END_TAG=$3

  $CP $FILE $FILE-tmp
  $NAWK 'BEGIN {
           IN_REPLACE = 0;
         };
         {
           if (index($0, START_TAG) > 0) {
             IN_REPLACE = 1;
           } else if (index($0, END_TAG) > 0) {
             if (IN_REPLACE == 0) {
               printf("%s\n", $0);
             }
             IN_REPLACE = 0;
           } else {
             if (IN_REPLACE == 0) {
               printf("%s\n", $0);
             }
           }
         };
         END {};' START_TAG="$START_TAG" END_TAG="$END_TAG" $FILE-tmp > $FILE
  $RM -f $FILE-tmp
}

###############################################
# Insert before tag
###############################################
InsertBlock() {
  FILE=$1
  TAG=$2
  NEW_TEXT_FILE=$3

  $CP $FILE $FILE-tmp
  $NAWK 'BEGIN {};
         {
           if (index($0, TAG) > 0) {
             while (getline str < NEW_TEXT_FILE > 0) {
               if (length(str) > 0) {
                 printf("%s\n", str);
               }
             }
             printf("%s\n", $0);
           } else {
             printf("%s\n", $0);
           }
         };
         END {};' TAG="$TAG" NEW_TEXT_FILE="$NEW_TEXT_FILE" $FILE-tmp > $FILE
  $RM -f $FILE-tmp
}


##############################################
#
restoreFile() {
    file=$1
    backupVersion=.$file-version-61
    backupFile=.$file.bak

    if [ -f $file ]; then
	$MV $file $backupFile
    fi

    if [ -f $backupVersion ]; then
	$MV $backupVersion $file
	$TOUCH $file
    fi
}

ldapsearch() {
   cd $INSTALL_DIR/bin
   if [ "$DS_SSL" = "true" ]; then
      ./ldapsearch -Z -o -P $CERTDB -b "ou=1.0,ou=$1,ou=services,$ROOT_SUFFIX" -D "$DIR_MANAGER" -w "$PASSWORD" -h $DS_HOST -p $DS_PORT "(objectclass=*)" sunserviceschema > $2
   else
      ./ldapsearch -o -b "ou=1.0,ou=$1,ou=services,$ROOT_SUFFIX" -D "$DIR_MANAGER" -w "$PASSWORD" -h $DS_HOST -p $DS_PORT "(objectclass=*)" sunserviceschema > $2
   fi
}

ldapmodify() {
   cd $INSTALL_DIR/bin
   if [ "$DS_SSL" = "true" ]; then
      ./ldapmodify -Z -c -P $CERTDB -D "$DIR_MANAGER" -w "$PASSWORD" -h $DS_HOST -p $DS_PORT -f $1 > /dev/null 2>&1
   else
      ./ldapmodify -c -D "$DIR_MANAGER" -w "$PASSWORD" -h $DS_HOST -p $DS_PORT -f $1 > /dev/null 2>&1
   fi
}

##############################################
# main start

get_base_dir
INSTALL_DIR=$BASE_DIR/SUNWam
LD_LIBRARY_PATH=$BASE_DIR/SUNWam/ldaplib/solaris/sparc/ldapsdk:$BASE_DIR/SUNWam/lib/solaris/sparc/jss:/usr/lib/mps/secv1:/usr/lib/mps

exit 0
