#!/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
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_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() {
  if [ ! -r $AMCONFIG ]; then
    $ECHO "Error: $AMCONFIG does not exist or readable."
    $ECHO "	  Can not remove the patch."
    exit 1
  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 1
      fi
    fi
  fi

  BASE_DIR=`pkginfo -r $pkg`
  INSTALL_DIR=$BASE_DIR/SUNWam
}

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

check_amconfig
get_base_dir

exit 0
