#!/bin/sh 

###############################################
# Sourcing macros
DIRNAME=/usr/bin/dirname
. `$DIRNAME $0`/../lib/psmacros
###############################################

STATE_FILE="$PS_CONFIG_DIR/PSConfig.properties"
PRODUCT="SunOne Portal Server / SRA 6.x"
GETPATCHES=N
VERBOSE=N
PATCHLIST=

###############################################
# supported options
###############################################
Usage() {
  $ECHO "usage: `basename $0` [-p | --patches] [-v | --verbose]"
  $ECHO " [-p | --patches] - list Portal Server patch information"
  $ECHO " [-v | --verbose] - list Portal Server patch information verbosely"
}

###############################################
# Get configuration from file
###############################################
GrabConfig() {
  GC_FILE=$1
  GC_KEY=$2
  GC_SEPARATOR=$3

  ANSWER=`$GREP "^$GC_KEY$GC_SEPARATOR" $GC_FILE | $UNIQ | $SED -e "s/$GC_KEY$GC_SEPARATOR//"`
}

###############################################
# Add patch id to a list without duplicates
###############################################
AddToPatchList() {
  for P in $*; do
    ADDFLAG=1
    for PATCH in $PATCHLIST; do
      if [ $PATCH = $P ]; then
        ADDFLAG=0
      fi
    done

    if [ $ADDFLAG -eq 1 ]; then
      PATCHLIST="$PATCHLIST $P"
    fi
  done
}

###############################################
# Get the list of portal server patches
###############################################
GetPSPatchList() {
  PSPKGS=`pkginfo | grep "Portal Server"`
  PSPKGNAMES=
  for PKG in $PSPKGS; do
    SUNW=`$ECHO $PKG | $GREP "SUNW"`
    if [ $SUNW ]; then
      PSPKGNAMES="$PSPKGNAMES $PKG"
    fi
  done
  
  for PKG in $PSPKGNAMES; do
    PATCHES=`$GREP "PATCHLIST" /var/sadm/pkg/$PKG/pkginfo`
    if [ $? -eq 0 ]; then
      PATCHES=`$ECHO $PATCHES | $AWK 'BEGIN { FS="=" } { print $2 }`
      AddToPatchList $PATCHES
    fi
  done
}

###############################################
# Print installed patch list
###############################################
PrintPatchList() {
  CNT=0
  for PATCH in $PATCHLIST; do
    CNT=`expr $CNT + 1`
    if [ $CNT -eq 1 ]; then
      LIST=$PATCH
    else
      LIST="$LIST, $PATCH"
    fi
  done

  $ECHO
  if [ $CNT -eq 0 ]; then
    $ECHO "Currently, there is no $PRODUCT patch installed!!!"
  elif [ $CNT -eq 1 ]; then
    $ECHO "Currently, only one $PRODUCT patch is installed:"
  else
    $ECHO "Currently, $CNT $PRODUCT patches are installed:"
  fi
  $ECHO

  if [ $VERBOSE = "Y" ]; then
    NOTROOT=N
    if [ `$ID | $AWK '{print $1}'` != "uid=0(root)" ]; then
      NOTROOT=Y
    fi

    for PATCH in $PATCHLIST; do
      $ECHO $PATCH
      $ECHO ------------------------------------------------------------------
      PATCHDIR="/var/sadm/patch/$PATCH"
      README="$PATCHDIR/README.$PATCH"

      if [ $NOTROOT = "Y" ]; then
        $ECHO "You must be root user to read patch synopsis."
      else
        if [ ! -d $PATCHDIR ]; then
          $ECHO "For some unknown reason, no more details about this patch is available."
        elif [ ! -r $README ]; then
          $ECHO "$README is not readable or does not exist, no more information available for this patch."
        else
          $GREP Synopsis $README
        fi
      fi

      $ECHO
      $ECHO
    done
  else
    $ECHO ------------------------------------------------------------------
    $ECHO $LIST
    $ECHO
  fi
  $ECHO
}

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

for i in $*; do
  case $i in
    -p|--patches) GETPATCHES="Y";;
    -v|--verbose) VERBOSE="Y";;
    -*) Usage; exit 2;;
  esac
done

if [ -f $STATE_FILE ]; then
  GrabConfig $STATE_FILE "JDK_DIR" "="
  JDK_DIR=$ANSWER
  GrabConfig $STATE_FILE "BASEDIR" "="
   BASEDIR=$ANSWER 
else
  $ECHO "Error: $STATE_FILE does not exist. $BELL_CHAR"
  exit 1
fi

$TOUCH "$PWD/.permCheck" > /dev/null 2>&1
if [ -f "$PWD/.permCheck" ]; then
  $RM "$PWD/.permCheck"
else
  $ECHO "Error: Current working directory $PWD does not have write permission."
  $ECHO "Info: Please change to a directory with write permission to execute version command"
  exit 1
fi

if [ $GETPATCHES = "Y" ] || [ $VERBOSE = "Y" ]; then
  if [ "$OSTYPE" = "SunOS" ]; then
    GetPSPatchList
    PrintPatchList
  fi
else
  FILE=$1
  if [ "$FILE" = "" ]; then
    VERSION_FILE="$BASEDIR/$PS_PRODUCT_DIR/lib/PSversion.properties"
    if [ -f $VERSION_FILE ]; then
      GrabConfig $VERSION_FILE "build" "="
      BUILD=$ANSWER
      GrabConfig $VERSION_FILE "productname" "="
      PNAME=$ANSWER
      GrabConfig $VERSION_FILE "productversion" "="
      $ECHO $BUILD $PNAME $ANSWER
    else
      $ECHO "Cannot find Portal Server version information."
    fi
  elif [ -f $FILE ]; then
    VERSION_FILE=".version-"`$BASENAME $FILE | $SED s/.jar$//`
    $JDK_DIR/bin/jar xf $FILE $VERSION_FILE
    if [ -f $VERSION_FILE ]; then
      $TAIL -1l $VERSION_FILE
      $RM -f $VERSION_FILE
    else
      $ECHO "$FILE does not contain any Portal Server version information."
    fi
  else
    $ECHO "$FILE does not exist."
  fi 
fi 

exit 0

