#!/bin/ksh

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
MKDIR=/usr/bin/mkdir

##############################################################
#
# 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 sdk 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 cross domain sso package is installed.
#
##############################################################
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 1
         fi
     fi
  fi

  BASE_DIR=`pkginfo -r $pkg`
}


##############################################################
#
# used by the license accept routine to get a valid yes or no
# answer.
#
##############################################################
yesno() {
  ok="no"
 
  question=$1
  default=$2

  valids="ynYN"

  while [ "$ok" = "no" ]; do
    if [ "$default" != "${default%y}" ] || [ "$default" != "${default%Y}" ]
    then
      $ECHO "$question [y]/n \c"
      default="y"
    elif [ "$default" != "${default%n}" ] || [ "$default" != "${default%N}" ]
    then
      $ECHO "$question y/[n] \c"
      default="n"
    else
      $ECHO "$question y/n \c"
      default=""
    fi

    read ans


    if [ "$ans" = "" ]; then
      if [ "$default" != "" ]; then
        ans=$default
      fi 
    fi  

    if [ "$ans" != "${ans%[$valids]}" ]; then
      ok="yes"
    fi

    if [ "$ok" = "no" ]; then
      $ECHO
      $ECHO "Invalid response"
      $ECHO
    fi

  done

    # make sure we have a std response so client doesn't need to check

    if [ "$ans" = "y" ]; then
      ans="y"
    elif [ "$ans" = "n" ]; then
      ans="n"
    elif [ "$ans" = "N" ]; then
      ans="n"
    fi
}

##############################################################
#
# Get the users response to accepting the license
#
##############################################################
license_accept() {
  more LICENSE.TXT
  $ECHO
  yesno "Do you agree to the license terms?" "n"
 
  if [ "$ans" = "n" ]; then
    exit 1
  fi
}

##############################################################
#
# copies the named file to a backup copy to be restored if
# patch is removed.
#
##############################################################
backup_file() {
    file=$1
    $CP -p $file .$file-version-62
}


##############################################################
#
# Main start
#
##############################################################
rm -f /tmp/.patch.ans 2>/dev/null 1>/dev/null
touch /tmp/.patch.ans
InstalledVersion="`pkgparam SUNWamsdk SUNW_PRODVERS`"
if [ $InstalledVersion != "2004Q2" ]; then
   echo "This patch is for AM 6.2 version only"   
   exit 0
fi

get_base_dir
if [ ! -d $BASE_DIR/SUNWam/ldaplib/ldapsdk ]; then
   $ECHO "Can't locate the LDAP lib at $BASE_DIR/SUNWam/ldaplib/ldapsdk"
   exit 0
fi

LD_LIBRARY_PATH=$BASE_DIR/SUNWam/ldaplib/ldapsdk:$BASE_DIR/SUNWam/lib/jss:/usr/lib/mps/secv1:/usr/lib/mps
export LD_LIBRARY_PATH
AMCONFIG=/etc/opt/SUNWam/config/AMConfig.properties

if [ ! -f $AMCONFIG ];then
   $ECHO
   $ECHO "This system cannot be upgraded due to a missing resource. The file"
   $ECHO "$AMCONFIG could not be located."
   $ECHO
   exit 1
fi

INSTALL_DIR=$BASE_DIR/SUNWam
DS_HOST=`$GREP com.iplanet.am.directory.host $AMCONFIG | $AWK ' BEGIN { FS="=" } {print $2}`
DS_PORT=`$GREP com.iplanet.am.directory.port $AMCONFIG | $AWK ' BEGIN { FS="=" } {print $2}`
DS_SSL=`$GREP com.iplanet.am.directory.ssl.enabled $AMCONFIG | $SED -e "s/com.iplanet.am.directory.ssl.enabled=//g"`
CERTDB_DIR=`$GREP "^com.iplanet.am.admin.cli.certdb.dir" $AMCONFIG | $SED -e "s/com.iplanet.am.admin.cli.certdb.dir=//g"`
CERTDB_PREFIX=`$GREP "^com.iplanet.am.admin.cli.certdb.prefix"  $AMCONFIG | $SED -e "s/com.iplanet.am.admin.cli.certdb.prefix=//g"`
CERTDB=$CERTDB_DIR/"$CERTDB_PREFIX"cert7.db


cd $INSTALL_DIR/bin
if [ "$DS_SSL" = "true" ]; then
   ./ldapsearch -Z -o -P $CERTDB -b "cn=schema" -D "$DIR_MANAGER" -w "$PASSWORD" -p $DS_PORT -h $DS_HOST "(objectclass=*)" > /dev/null 2>&1
else
   ./ldapsearch -b "cn=schema" -D "$DIR_MANAGER" -w "$PASSWORD" -p $DS_PORT -h $DS_HOST "(objectclass=*)" > /dev/null 2>&1
fi

if [ $? -ne 0 ]; then
   $ECHO
   $ECHO "Could not connect to the Directory Server. Please make"
   $ECHO "sure the user name and password are correct and that"
   $ECHO "the Directory Server is running."
   exit 1
fi

#Backup xml,property,dtd files
cd /etc/opt/SUNWam/config/
backup_file AMConfig.properties

pkginfo -q SUNWamsvcconfig
if [ $? -eq 0 ]; then
   cd /etc/opt/SUNWam/config/xml/
   backup_file amAuthSafeWord.xml
   backup_file amAuth.xml
fi

cd $BASE_DIR/SUNWam/locale/
backup_file amAdminCLI.properties
backup_file amAuthSafeWord.properties

cd $BASE_DIR/SUNWam/dtd/
backup_file amAdmin.dtd
backup_file sms.dtd
