#!/bin/ksh

AWK=/usr/bin/awk
CHMOD=/usr/bin/chmod
CP=/usr/bin/cp
ECHO=/usr/bin/echo
GREP=/usr/bin/grep
ID=/usr/bin/id
INSTALLF=/usr/sbin/installf
LN=/usr/bin/ln
MKDIR=/usr/bin/mkdir
RM=/usr/bin/rm
SED=/usr/bin/sed
UNIQ=/usr/bin/uniq

OSTYPE=`/bin/uname -s`
if [ "$OSTYPE" = "Linux" ]; then
  CHMOD=/bin/chmod
  CP=/bin/cp
  ECHO=/bin/echo
  GREP=/bin/grep
  LN=/bin/ln
  MKDIR=/bin/mkdir
  RM=/bin/rm
  SED=/bin/sed
fi

BELL_CHAR='\a'

PS_STATE_FILE="/etc/opt/SUNWps/PSConfig.properties"
JCIFS_PKGINFO_FILE="/var/sadm/pkg/SUNWjcifs/pkginfo"

###############################################
# Get configuration from file
###############################################
GrabConfig() {
  GRABCONFIG_FILE=$1
  GRABCONFIG_KEY=$2
  GRABCONFIG_SEPARATOR=$3

  ANSWER=`$GREP "^$GRABCONFIG_KEY$GRABCONFIG_SEPARATOR" $GRABCONFIG_FILE | $UNIQ | $SED -e "s/$GRABCONFIG_KEY$GRABCONFIG_SEPARATOR//"`
}

###############################################
# Main
###############################################

if [ `$ID | $AWK '{print $1}'` != "uid=0(root)" ]; then
  $ECHO "You must be root user. $BELL_CHAR"
  exit 1
fi

if [ ! -f $PS_STATE_FILE ]; then
  $ECHO "Error: $PS_STATE_FILE does not exist. $BELL_CHAR"
  exit 1
fi

GrabConfig $PS_STATE_FILE "BASEDIR" "="
if [ "$ANSWER" != "" ]; then
  PS_BASEDIR=$ANSWER
else
  $ECHO "Error: Cannot determine BASEDIR. $BELL_CHAR"
  exit 1
fi

if [ -f $JCIFS_PKGINFO_FILE ]; then
  GrabConfig $JCIFS_PKGINFO_FILE "BASEDIR" "="
  $LN -s $ANSWER/jcifs/lib/jcifs-0.7.3.jar $PS_BASEDIR/SUNWps/web-src/WEB-INF/lib/jcifs-0.7.3.jar
  if [ "$OSTYPE" = "SunOS" ]; then
    $INSTALLF "SUNWpsnf" $PS_BASEDIR/SUNWps/web-src/WEB-INF/lib/jcifs-0.7.3.jar
    $INSTALLF -f "SUNWpsnf"
  fi
fi

exit 0
