#!/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
HEAD=/usr/bin/head
TOUCH=/usr/bin/touch
MKDIR=/usr/bin/mkdir
CUT=/usr/bin/cut

PATCH_ID=120954-01
BKFILESUFFIX=-pre-$PATCH_ID
CONFIG_DIR=/etc/opt/SUNWam/config
AMCONFIG=$CONFIG_DIR/AMConfig.properties

PATH=.:/bin:/usr/bin:/usr/sbin:/etc
export PATH

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_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_amconfig() {
  check_for_con
  check_for_svc
  if [ $con = "no" ] && [ $svc = "no" ]; then
    return
  fi

  if [ ! -r $AMCONFIG ]; then
    $ECHO "Error: $AMCONFIG does not exist or readable."
    $ECHO "       Can not install the patch."
    exit 2 
  fi

  preserve_wars
}

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`
  INSTALL_DIR=$BASE_DIR/SUNWam
}

##############################################################
# copies the named file to a backup copy to be restored if
# patch is removed.
##############################################################
backupFile() {
  file=$1
  if [ ! -f $file$BKFILESUFFIX ]; then
    r=`$ECHO $file | $CUT -d/ -f1`
    if [ x$r = "x" ]; then
      $ECHO "Backing up $file"
    else
      $ECHO "Backing up `pwd`/$file"
    fi
    $CP $file $file$BKFILESUFFIX
  fi
}

##############################################################
# keep services.war and the other two war files for backout
##############################################################
preserve_wars() {
  WEBAPPS_SOURCE_DIR=$INSTALL_DIR/web-src
  cd $INSTALL_DIR

  if [ ! -f services.war ]; then
    URI=`$GREP "^com.iplanet.am.services.deploymentDescriptor=" $AMCONFIG \
        | $CUT -d= -f2`
    WARFILE=$INSTALL_DIR/${URI:-amserver}.war
    if [ -f $WARFILE ]; then
      $CP $WARFILE services.war 
    else
      $ECHO "Error: $WARFILE does not exist."
      $ECHO "       Can not save services.war for backout."
      exit 3 
    fi 
  fi

  if [ ! -f password.war ]; then
    URI=`$GREP "^com.sun.identity.password.deploymentDescriptor=" $AMCONFIG \
        | $CUT -d= -f2`
    WARFILE=$INSTALL_DIR/${URI:-ampassword}.war
    if [ -f $WARFILE ]; then
      $CP $WARFILE password.war 
    else
      $ECHO "Error: $WARFILE does not exist."
      $ECHO "       Can not save password.war for backout."
      exit 3 
    fi 
  fi

  if [ ! -f introduction.war ]; then
    URI=
    WARFILE=$INSTALL_DIR/${URI:-amcommon}.war
    if [ -f $WARFILE ]; then
      $CP $WARFILE introduction.war 
    fi 
  fi
}

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

get_base_dir
check_amconfig

# backup properties files
cd $BASE_DIR/SUNWam/locale/
backupFile amConsole.properties
backupFile amIdRepoService.properties

exit 0
