#!/bin/sh
# 
# This script is executed after the patch has been removed
#
# this utility removes non-customized resources from
# imbrand.jar, for all installed locales.
# Also start the services, if present.
#

SVR_PKG="SUNWiim"
rel_docroot=`pkgparam -R $ROOTDIR SUNWiimc IIM_DOCROOT`

pkginfo $SVR_PKG > /dev/null 2>&1
if [ $? != 0 ]; then
  # Server pkg not installed, no need for server shutdown
  no_server=1
  rel_svr_basedir=""
else
  no_server=0
  rel_svr_basedir=`pkgparam -R $ROOTDIR $SVR_PKG BASEDIR`
  rel_svr_basedir=$rel_svr_basedir/SUNWiim
fi

if [ "$ROOTDIR" = "" ]
then
  rootdir="/"
else
  rootdir="$ROOTDIR"
fi

if [ "$rootdir" != "/" ]; then
  docroot="${rootdir}${rel_docroot}"
  svr_basedir="${rootdir}${rel_svr_basedir}"
else
  docroot="${rel_docroot}"
  svr_basedir="${rel_svr_basedir}"
fi

# Start the services
if [ $no_server = 0 ]; then
  if test -x $svr_basedir/sbin/imadmin -a -r $svr_basedir/config/iim.conf ; then
    $svr_basedir/sbin/imadmin start
  fi
fi

ref_jar="imres.jar"
cust_jar="imbrand.jar"

backup=".imbrand.jar.unstripped.05"

if [ `uname -i` = "i86pc" ] ; then
  patchid="116645"
  min_patchrev="04"
else
  patchid="115732"
  min_patchrev="04"
fi

debug() {
  if [ "x"$VERBOSE != "x" ] ; then
    echo "postbackout: $1"
  fi
}

get_pkg_name() {
  locale=$1
  if [ $locale = "en" ]; then echo "SUNWiimc"; fi
  if [ $locale = "de" ]; then echo "SUNWdiimc"; fi
  if [ $locale = "es" ]; then echo "SUNWeiimc"; fi
  if [ $locale = "fr" ]; then echo "SUNWfiimc"; fi
  if [ $locale = "ja" ]; then echo "SUNWjiimc"; fi
  if [ $locale = "ko" ]; then echo "SUNWkiimc"; fi
  if [ $locale = "zh" ]; then echo "SUNWciimc"; fi
  if [ $locale = "zh_TW" ]; then echo "SUNWhiimc"; fi
}


#rev=`pkgparam -R ${rootdir} SUNWiimc PATCHLIST | grep $PATCHID | cut -d\- -f2`
current_patch=`pkgparam -R ${rootdir} SUNWiimc PATCHLIST`
check_ret=0
if [ "x$current_patch" != x ]
then
  for patch in `echo $current_patch`
  do
    check_ret=`echo "$patch" | \
                     awk '{ FS="-" ; \
                     if (($1 == '${patchid}') && ($2 <= '${min_patchrev}'))
                         print 0 ; \
                     else print 1;}'`
    if [ $check_ret -eq 0 ]; then
      break
    fi
  done
fi

if [ $check_ret -eq 1 ]; then
  debug "current patch revision older than ${min_patchrev}.  Nothing to restore"
  exit 0
fi

for locale in de en es fr ja ko zh zh_TW
do
  if [ -f $docroot/$locale/${backup} ]
  then
    cp ${docroot}/$locale/${backup} ${docroot}/$locale/${cust_jar}
    debug "Restored ${docroot}/$locale/${cust_jar}"

    # restore the modified files into the pkg database
    pkgname=`get_pkg_name $locale`
    removef -R ${rootdir} $pkgname ${rel_docroot}/$locale/${backup}
    rm -f ${docroot}/$locale/${backup}
    removef -R ${rootdir} -f $pkgname 

    debug "Updated $pkgname contents"
  fi
done

exit 0

