#!/bin/sh

# Copyright  2006 Sun Microsystems, Inc.  All rights reserved.
#
# Sun Microsystems, Inc. has intellectual property rights relating to
# technology embodied in the product that is described in this document.
# In particular, and without limitation, these intellectual property rights
# may include one or more of the U.S. patents listed at
# http://www.sun.com/patents and one or more additional patents or pending
# patent applications in the U.S. and in other countries.
#
# U.S. Government Rights - Commercial software.  Government users are subject
# to the Sun Microsystems, Inc. standard license agreement and applicable
# provisions of the FAR and its supplements.
#
# Use is subject to license terms.
#
# This distribution may include materials developed by third parties.Sun,
# Sun Microsystems and  the Sun logo are trademarks or registered trademarks
# of Sun Microsystems, Inc. in the U.S. and other countries.  
#
# Copyright  2006 Sun Microsystems, Inc. Tous droits rservs.
# Sun Microsystems, Inc. dtient les droits de proprit intellectuels relatifs
#  la technologie incorpore dans le produit qui est dcrit dans ce document.
# En particulier, et ce sans limitation, ces droits de proprit
# intellectuelle peuvent inclure un ou plus des brevets amricains lists
#  l'adresse http://www.sun.com/patents et un ou les brevets supplmentaires
# ou les applications de brevet en attente aux Etats - Unis et dans les
# autres pays.
#
# L'utilisation est soumise aux termes du contrat de licence.
#
# Cette distribution peut comprendre des composants dvelopps par des
# tierces parties.
#
# Sun,  Sun Microsystems et  le logo Sun sont des marques de fabrique ou des
# marques dposes de Sun Microsystems, Inc. aux Etats-Unis et dans
# d'autres pays.

ECHO=/bin/echo
GREP=/bin/grep
SED=/bin/sed
AWK=/bin/awk
CUT=/bin/cut
LS=/bin/ls
MV=/bin/mv
WC=/usr/bin/wc
CP="/bin/cp -p"
RM="/bin/rm -f"
gettext=/usr/bin/gettext

PRODUCT="sun-saml2"
VERSION_FILE=version
PATCH_ID=122985-01
BKFILESUFFIX=-pre-$PATCH_ID
IS_INSTALL_ETCDIR=/etc/opt/sun
PRODUCT_DIR=identity
CONFIG_DIR=${IS_INSTALL_ETCDIR}/${PRODUCT_DIR}/config
AMCONFIG=$CONFIG_DIR/AMConfig.properties

############################################################
# get base dir
############################################################
get_base_dir() {
  $MV /tmp/AMConfig.properties $AMCONFIG
  BASEDIR=`$GREP com.iplanet.am.installdir= $AMCONFIG \
    | $SED -e 's/\/identity//g' | $SED -e 's/com.iplanet.am.installdir=//g'`
  INSTALL_DIR=$BASEDIR/$PRODUCT_DIR
}

##############################################################
# replaces a line in the specified file.
##############################################################
replace_line() {
  file=$1
  match=$2
  new=$3

  $CP $file $file-orig-$$
  $SED -e "
/$match/ {
c\\
$new
}" $file > $file-tmp
  $MV $file-tmp $file
  $RM $file-orig-$$
}

##############################################################
# update the version file
##############################################################
updateVersionString() {
  cd $CONFIG_DIR
  if [ ! -f $INSTALL_DIR/saml2/lib/$VERSION_FILE ]; then
    $ECHO "Sun Java System SAML v2 plugins 1.0" > $INSTALL_DIR/saml2/lib/$VERSION_FILE$BKFILESUFFIX
  fi

  HASIT=`$GREP $PATCH_ID $INSTALL_DIR/saml2/lib/$VERSION_FILE | $WC -l | $SED -e 's/ //g'`
  if [ x$HASIT != "x0" ]; then
    return
  fi

  $CP $INSTALL_DIR/saml2/lib/$VERSION_FILE $INSTALL_DIR/saml2/lib/$VERSION_FILE$BKFILESUFFIX
  $ECHO $PATCH_ID >> $INSTALL_DIR/saml2/lib/$VERSION_FILE
}

##############################################################
# main processing
##############################################################
CURRENTDIR=`pwd`
get_base_dir
updateVersionString
echo "After the patch installation, run \"saml2setup update -s <saml2silent>\" to update the staging directory and re-generate WAR, after redeploying the new
WAR, restart your web container."
echo "Refer to the patch release notes for more details regarding post installation intructions."
