#!/bin/sh
#
# Copyright (c) 09/08/98, by Sun Microsystems, Inc. All rights reserved.
#
# "@(#)preremove 1.4 98/09/08 SMI"
#

# Exit codes for installation scripts 
e_ok=0      
e_fatal=1      # stop installation on this exit
e_warning=2    # Installation will go on. 
e_int=3        # Interrupted. Stop installation
e_reboot=10    # User must reboot after installation of all selected packages
e_rebootnow=20 # User must reboot right after installation of current package
               # To be added to one of the single-digit exit code above

# Trap interrupt
trap `exit $e_int` 15

AWK=/bin/awk
CP=/bin/cp
ECHO=/bin/echo
GREP=/bin/grep
KILL=/bin/kill
MKDIR=/bin/mkdir
PKGP=/bin/pkgparam
PS=/bin/ps
RM=/bin/rm
CAT=/usr/bin/cat

# Call stop jdmk agent
$ECHO "stopping jaw"
if [ -r /tmp/.jawPID ] ; then
	  pid=`$CAT /tmp/.jawPID`
	  err=`kill -TERM $pid  2>&1`
	  if [ $? -ne 0 ]; then
	    $ECHO "\nError: $err."
	  fi
	  /usr/bin/rm -f /tmp/.jawPID
else
	  PROC=`/usr/bin/ps -ef | $GREP com.sun.jaw.impl.base.Main | $GREP -v grep | $AWK '{print $2}'`
	  if [ -n "$PROC" ] ; then
	    err=`kill -TERM $PROC 2>&1`
	    if [ $? -ne 0 ]; then
	      $ECHO "\nError: $err."
	    fi
	  else
	    $ECHO "\nError: jaw is not running."
	  fi
fi

# Get Java DMK version

VERSION=`$PKGP SUNWjawag VERSION | sed -e 's/ //g'`

# Backup the IP ACL file used by the SNMP Adaptor Server.

jaw_acl_file="/etc/opt/SUNWconn/jaw/conf/jaw.acl"

if [ -f ${jaw_acl_file} ]; then
  $MKDIR -p /var/tmp/jaw_${VERSION}/conf
  $CP ${jaw_acl_file} /var/tmp/jaw_${VERSION}/conf
  $RM -f ${jaw_acl_file}
  $ECHO "File ${jaw_acl_file} has been backed up to /var/tmp/jaw_${VERSION}/conf."
fi

# Backup the files used by the Launcher Service.

launcher_dir="/etc/opt/SUNWconn/jaw/launch"
launcher_nb_files=`ls ${launcher_dir} 2> /dev/null | wc -l`

if [ $launcher_nb_files -gt 0 ]; then
  $MKDIR -p /var/tmp/jaw_${VERSION}/launch
  $CP ${launcher_dir}/* /var/tmp/jaw_${VERSION}/launch
  $RM -f ${launcher_dir}/*
  $ECHO "Files in ${launcher_dir} have been backed up to /var/tmp/jaw_${VERSION}/launch."
fi

# Remove the files used by the MLet Cache and Versioning Service.

mletcache_dir="/var/opt/SUNWconn/jaw/mletcache"
mletcache_nb_files=`ls ${mletcache_dir} 2> /dev/null | wc -l`

if [ $mletcache_nb_files -gt 0 ]; then
  $RM -f ${mletcache_dir}/*
fi

# Happy End
exit $e_ok
