#!/bin/sh

# 
# This script is executed after the patch has been installed
#
# This utility removes non-customized resources from imbrand.jar, for all 
# installed locales.
# Also start the services, which were stopped before patch installation.
#

SVR_PKG="SUNWiim"

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

rel_docroot=`pkgparam -R $ROOTDIR SUNWiimc IIM_DOCROOT`

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"

work_dir="/tmp/SUNWiim.postpatch.dir"
current_dir=`pwd`

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

check_java_version() {

  java_cmd=$1

  rc=`$java_cmd -version 2>/dev/null 2>&1`
  if [ $? -ne 0 ] ;
  then
    echo "Cannot run java ($java_cmd). Please fix this problem and run setup again"
    echo
    return 3
  fi
  
  javaFullVersion=`($java_cmd -version 2>&1) | grep version | cut -d\" -f2`
  version=`($java_cmd -version 2>&1) | grep version | cut -d\" -f2 | cut -d\. -f1-2`
  majorversion=`($java_cmd -version 2>&1) | grep version | cut -d\" -f2 | cut -d\. -f1`
  minorversion=`($java_cmd -version 2>&1) | grep version | cut -d\" -f2 | cut -d\. -f2`
  
  if [ $majorversion -gt 0 ] ;
  then
    if [ $minorversion -gt 2 ] ;
    then
      return 0
    fi
  fi
  return 1

}




set_java_home() {

  OSARCH=`/bin/uname -s`

  if [ "$OSARCH" = "Linux" ];
  then
    javaPkgname=j2re
  else
    javaPkgname=SUNWj3rt
  fi

  installJavaPackage=1

  # try JAVA_HOME
  if [ "$JAVA_HOME" != "" ]
  then
    check_java_version $JAVA_HOME/bin/java
    if [ $? -eq 0 ]
    then
      java_home="$JAVA_HOME"
      return 0
    fi
  fi

  # look for installed packages
  if [ "$OSARCH" = "Linux" ]
  then
    result=`rpm -q --all|egrep 'jdk|jre|j2se|j2re|j2sdk'|head -1`
    rc=$?
    if [ X$result = X"" ]; 
    then
      rc=1
    fi
  else
    # Check for the java pkg SUNWj3rt first
    result=`pkginfo $javaPkgname 2>/dev/null 2>&1`
    rc=$?
  fi
  
  if [ $rc -eq 0 ]; 
  then
    if [ "$OSARCH" = "Linux" ]; 
    then
      java_home=`rpm -q -l $result|grep 'bin.jar$'| head -1 | sed -e "s/\/bin\/jar//"`
    else
      basedir=`pkgparam -v $javaPkgname | grep BASEDIR= | cut -f2 -d = | sed -e s/\'//g`
      java_home=$basedir/j2se
    fi

    check_java_version $java_home/bin/java
    if [ $? -eq 0 ]
    then
      return 0
    fi
  fi

  # look for it in well-know places
  java_home="/usr/j2se"
  check_java_version $java_home/bin/java
  if [ $? -eq 0 ]
  then
    return 0
  fi

  java_home="/usr/java"
  check_java_version $java_home/bin/java
  if [ $? -eq 0 ]
  then
    return 0
  fi

  # look for it in the path
  java_cmd=`which java`
  check_java_version $java_cmd
  if [ $? -eq 0 ]
  then
    java_home=`echo $java_cmd | sed -e "s/\/bin\/java//"`
    return 0
  fi

  return 1
}


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
}

set_java_home
rc=$?
if [ $rc -ne 0 ]
then
  echo "Cannot locate a valid version of Java.  Please set JAVA_HOME"
  exit 1
else 
  echo "Will be using $java_home/bin/jar"
fi


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

    create_backup=0
    if [ ! -f $docroot/$locale/${backup} ]
    then
      create_backup=1
      cp ${docroot}/${locale}/${cust_jar} $docroot/$locale/${backup}
    fi

    pkgname=`get_pkg_name $locale`
    #removef -R ${rootdir} $pkgname ${rel_docroot}/$locale/${cust_jar}
    #removef -R ${rootdir} -f $pkgname

    rm -rf $work_dir
    mkdir $work_dir
    mkdir $work_dir/cust
    mkdir $work_dir/ref
    cp $docroot/$locale/${ref_jar} $work_dir/ref
    cp $docroot/$locale/${cust_jar} $work_dir/cust
    cd $work_dir/ref
    $java_home/bin/jar xf ${ref_jar}
    cd $work_dir/cust
    $java_home/bin/jar xf ${cust_jar}
    rm ${cust_jar}
    
    for file in `find com/sun/im/desktop -name "*Padded.gif"`
    do
      debug "Removing obsolete resource: $file"
      rm -f $file
    done

    for file in `find com/sun/im/desktop -name "*.*"`
    do
      ref=$work_dir/ref/`echo $file | sed -e "s/\/desktop\//\/desktop\/default\//g"`
      if [ -f $ref ]
      then
	#debug "Comparing $file with $ref"
	diff $file $ref 2>/dev/null 1>&2
	different=$?
	if [ $different -eq 0 ]
	then
	  debug "Removing non-customized resource: $file"
	  rm $file
	fi
      else
	debug "$ref does not exist"
      fi

      if [ -f $file -a "$file" = "com/sun/im/desktop/brand/app_icon.gif" ]
      then
        sum=`sum $file | awk '{print $1}'`
	if [ $sum = "29303" ]
        then
	  debug "Removing non-customized resource: $file"
          rm $file
        fi
      fi

      if [ -f $file -a "$file" = "com/sun/im/desktop/brand/tray_icon.ico" ]
      then
        sum=`sum $file | awk '{print $1}'`
	if [ $sum = "44711" ]
        then
	  debug "Removing non-customized resource: $file"
          rm $file
        fi
      fi

      if [ -f $file -a "$file" = "com/sun/im/desktop/brand/logon_splash.gif" ]
      then
        sum=`sum $file | awk '{print $1}'`
	if [ $sum = "56329" ]
        then
	  debug "Removing non-customized resource: $file"
          rm $file
        fi
      fi

      if [ -f $file -a "$file" = "com/sun/im/desktop/brand/brand.properties" ]
      then
        sed -e "s/Sun\ ONE\ Intant\ Messenger//g" -e "s/Intant\ Messenger//g" -e "s/6\.0//g" $file > ${file}.tmp
	mv ${file}.tmp $file
      fi

    done
    
    $java_home/bin/jar cf ${cust_jar} .
    cd $current_dir

    dircmp $work_dir/cust/com/sun/im/desktop $work_dir/ref/com/sun/im/desktop > /dev/null 2>&1 
    different=$?
    if [ $different -ne 0 ]
    then
      cp $work_dir/cust/${cust_jar} $docroot/$locale/${cust_jar}

      # declare the modified files into the pkg database
      installf -R ${rootdir} $pkgname ${rel_docroot}/$locale/${cust_jar} v 0644 root other
      if [ create_backup -eq 1 ]
      then
	installf -R ${rootdir} $pkgname ${rel_docroot}/$locale/${backup} v 0644 root other
      fi
      installf -R ${rootdir} -f $pkgname
      debug "Updated $pkgname contents"
    fi

  fi

done

exit 0

