#!/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

##############################################################
#
# Test if the sdk package is installed.
#
##############################################################
check_for_sdk() {
   pkginfo -q SUNWamsdk
   if [ $? -eq 0 ]; then
      sdk="yes"
   else
      sdk="no"
   fi
}

##############################################################
#
# Test if the services package is installed.
#
##############################################################
check_for_svc() {
   pkginfo -q SUNWamsvc
   if [ $? -eq 0 ]; then
      svc="yes"
   else
      svc="no"
   fi
}

##############################################################
#
# Test if the console package is installed.
#
##############################################################
check_for_con() {
   pkginfo -q SUNWamcon
   if [ $? -eq 0 ]; then
      con="yes"
   else
      con="no"
   fi
}

##############################################################
#
# Test for web server services package is installed.
#
##############################################################
check_for_iws() {
   pkginfo -q SUNWamsws
   if [ $? -eq 0 ]; then
      iws="yes"
   else
      iws="no"
   fi
}

##############################################################
#
# Test if common domain services package is installed.
#
##############################################################
check_for_fcd() {
   pkginfo -q SUNWamfcd
   if [ $? -eq 0 ]; then
      fcd="yes"
   else
      fcd="no"
   fi
}

##############################################################
#
# Test if cross domain sso package is installed.
#
##############################################################
check_for_cds() {
   pkginfo -q SUNWamcds
   if [ $? -eq 0 ]; then
      cds="yes"
   else
      cds="no"
   fi
}

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

##############################################################
#
# Test if console is deployed on app server.
#
##############################################################
check_for_sac() {
   pkginfo -q SUNWamsac
   if [ $? -eq 0 ]; then
      sac="yes"
   else
      sac="no"
   fi
}

##############################################################
#
# Test if console is deployed on web logic.
#
##############################################################
check_for_wlc() {
   pkginfo -q SUNWamwlc
   if [ $? -eq 0 ]; then
      wlc="yes"
   else
      wlc="no"
   fi
}

##############################################################
#
# Test if console is deployed on web sphere.
#
##############################################################
check_for_wsc() {
   pkginfo -q SUNWamwsc
   if [ $? -eq 0 ]; then
      wsc="yes"
   else
      wsc="no"
   fi
}


##############################################################
#
#
#
##############################################################
get_base_dir() {
  check_for_sdk
  if [ "$sdk" = "yes" ]; then
    pkg="SUNWamsdk"
  else
     check_for_fcd
     if [ "$fcd" = "yes" ]; then
        pkg="SUNWamfcd"
     else
         check_for_cds
         if [ "$cds" = "yes" ]; then
           pkg="SUNWamcds"
         else
           exit
         fi
     fi
  fi

  BASE_DIR=`pkginfo -r $pkg`
}

##############################################################
#
# Replace a line of text in the given file
#
##############################################################
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 a block of text in a file.
#
##############################################################
replace_block() {
  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
#
##############################################################
delete_block() {
  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
}

##############################################################
#
# Restores a file to the prepatch state.
#
##############################################################
restore_file() {
    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
}

##############################################################
#
# Run an ldapsearch command and send the results 
# to a file.
#
##############################################################
ldap_search() {
   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
}

##############################################################
#
# Run an ldapmodify command on the given file.
#
##############################################################
ldap_modify() {
   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
#
##############################################################
