#
# ident "@(#)preremove.src	1.15 04/11/01 SMI"
#
# Copyright 1999-2004 Sun Microsystems, Inc.  All rights reserved.
#

PATH=/bin:/usr/bin:/sbin:/usr/sbin

if [ -z "$PKG_INSTALL_ROOT" ]; then PKG_INSTALL_ROOT=""; fi
if [ -z "$CLIENT_BASEDIR" ]; then CLIENT_BASEDIR="/"; fi
if [ -z "$BASEDIR" ]; then BASEDIR="$PKG_INSTALL_ROOT$CLIENT_BASEDIR"; fi

set -eu

#
# Clean up /var/opt/
#

PKGID=SUNWut
#
# VAROPTDIR, the /var/opt path without the prepended PKG_INSTALL_ROOT
# path. ROOT_VAROPTDIR includes the PKG_INSTALL_ROOT path.  This is
# needed mainly to support alternative root installation.
VAROPTDIR=/var/opt/$PKGID
ROOT_VAROPTDIR=$PKG_INSTALL_ROOT/var/opt/$PKGID

#
# no need to prepend the PKG_INSTALL_ROOT to UTLIBDIR because it's
# only used within the crontab.
UTLIBDIR=/opt/$PKGID/lib

#
# Remove symlinks
#
rm -rf $ROOT_VAROPTDIR/tokens $ROOT_VAROPTDIR/idle $ROOT_VAROPTDIR/displays
rm -rf $ROOT_VAROPTDIR/dispinfo
rm -rf $ROOT_VAROPTDIR/snmp 2> /dev/null

#
# Clean up directories
#
for dir in $ROOT_VAROPTDIR/log $ROOT_VAROPTDIR/cgitokens; do
  if [ -d "$dir" ]; then
    find $dir -type f -exec rm {} \;
  fi
done

#
# Clean up /etc/opt/
#

ETCOPTDIR=$PKG_INSTALL_ROOT/etc/opt/$PKGID

rm -f $ETCOPTDIR/utadmin.conf
rm -f $ETCOPTDIR/utadmin.pw
rm -f $ETCOPTDIR/gmSignature
rm -f $ETCOPTDIR/ut_disable_*
rm -f $ETCOPTDIR/jre
rm -f $ETCOPTDIR/guijre
[ -d $ETCOPTDIR/net ] && find $ETCOPTDIR/net -type f -exec rm -f {} \;

#
# Cleanup /etc/dt
#
# NOTE: these directoires do not exist on Linux
EDTDIR=$PKG_INSTALL_ROOT/etc/dt/config

for i in Xservers Xconfig; do
  #
  # Remove the symlink made by utacleanup
  #
  if [ -h $EDTDIR/$i ]; then
    rm -f $EDTDIR/$i
  fi

  #
  # If a saved "prototype" template file exists, move it into place as
  # the new site-customised config file.
  #
  proto=$EDTDIR/$i.$PKGID.prototype
  if [ -f $proto ]; then
    mv -f $proto $EDTDIR/$i
  fi
done

#
# cleanup the syslog entries
#
# SYSLOG_CONF, the path for syslog.conf file without the prepended
# PKG_INSTALL_ROOT path.  ROOT_SYSLOG_CONF includes the PKG_INSTALL_ROOT
# path.
SYSLOG_CONF=/etc/syslog.conf
ROOT_SYSLOG_CONF=$PKG_INSTALL_ROOT$SYSLOG_CONF

OLDMASK=`umask`
umask 077

USER_INFO=`grep -n "user\.info.*${VAROPTDIR}/log/messages" $ROOT_SYSLOG_CONF \
  2> /dev/null | awk -F: '{ printf "-e \"%sd\" ", $1}'`
 
LOCAL1_INFO=`grep -n "local1\.info.*${VAROPTDIR}/log/admin_log" $ROOT_SYSLOG_CONF \
  2> /dev/null | awk -F: '{ printf "-e \"%sd\" ", $1}'`

if [ -n "$USER_INFO" -o -n "$LOCAL1_INFO" ]; then
  TMPFILE=${ROOT_VAROPTDIR}/tmp/syslog.conf.$$
  cat /dev/null >$TMPFILE
  eval "sed $USER_INFO $LOCAL1_INFO $ROOT_SYSLOG_CONF >> $TMPFILE"
  cat $TMPFILE > $ROOT_SYSLOG_CONF
  rm -f $TMPFILE
  # In an altroot install we have no way of getting the attention of a
  # syslogd running over the altroot.
  if [ -z "$PKG_INSTALL_ROOT" ] ; then
    pkill -HUP syslogd || true
  fi
fi

#
# cleanup crontab entries
#
#
# NOTE: we cannot use crontab to list the current entries when installing on
# an alternate root.  Will address this when we add the support for alternate
# root.
OS=`uname -s`
if [ -n "$PKG_INSTALL_ROOT" ] ; then

  echo "*********************************************************************"
  echo "You are removing this software from an alternative root filesystem."
  echo "This script is unable to delete cron jobs in the alternative root."
  echo "After booting from the alternative root filesystem please remove any"
  echo "$UTLIBDIR/utlog commands from the crontab entries for"
  echo "the 'root' user."
  echo "*********************************************************************"
  sleep 4

else
  TMPFILE=${ROOT_VAROPTDIR}/tmp/crontab.tmp.$$
  cat /dev/null >$TMPFILE
  if [ $OS = "SunOS" ]; then
    (set +e; crontab -l root 2>/dev/null >>$TMPFILE 2> /dev/null)
    CRONTAB_ST=$?
  else
    (set +e; crontab -u root -l 2>/dev/null >>$TMPFILE 2> /dev/null)
    CRONTAB_ST=$?
  fi
  if [ $CRONTAB_ST -eq 0 ]; then
    #
    # On Linux, the crontab automatically adds some comments to the top
    # of the crontab every time we update the crontab.  We need to remove
    # these comments.  Otherwise, we will end up with duplicated comment
    # lines.  crontab adds the following text:
    #
    # DO NOT EDIT THIS FILE - edit the master and reinstall.
    # (/tmp/crontab.??? installed on Fri Apr 16 16:19:09 2004)
    # (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
    #
    set +e
    head -1 $TMPFILE | \
        grep "^# DO NOT EDIT THIS FILE - edit the master and reinstall" \
	> /dev/null 2>&1 
    GREP_ST=$?
    set -e
    if [ $GREP_ST -eq 0 ]; then
      # comment exists, remove them
      TMPFILE_COMM=$ROOT_VAROPTDIR/tmp/crontab_comment.tmp.$$
      cat /dev/null >$TMPFILE_COMM
      head -3 $TMPFILE | sed \
           -e "/^# DO NOT EDIT THIS FILE - edit the master and reinstall/d" \
           -e "/^# (\/[^ ]* installed/d" \
           -e "/^# (Cron version/d" >> $TMPFILE_COMM
      sed -n '4,$p' $TMPFILE >> $TMPFILE_COMM
      rm -f $TMPFILE
      mv $TMPFILE_COMM $TMPFILE
    fi
  fi

  if [ $CRONTAB_ST -eq 0 ]; then
    MESSAGE_LOG=`grep -n "$UTLIBDIR/utlog -c -d ${VAROPTDIR}/log/messages" \
      $TMPFILE 2> /dev/null | awk -F: '{ printf "-e \"%sd\" ", $1}'`

    ADMIN_LOG=`grep -n "$UTLIBDIR/utlog -c -d ${VAROPTDIR}/log/admin_log" \
      $TMPFILE 2> /dev/null | awk -F: '{ printf "-e \"%sd\" ", $1}'`

    if [ -n "$MESSAGE_LOG" -o -n "$ADMIN_LOG" ]; then
      NEWCRONTMP=${ROOT_VAROPTDIR}/tmp/crontab.new.tmp.$$
      cat /dev/null >$NEWCRONTMP
      eval "sed $MESSAGE_LOG $ADMIN_LOG $TMPFILE >> $NEWCRONTMP"

      crontab $NEWCRONTMP
      rm -f $NEWCRONTMP
    fi
  fi

  #
  # For Linux only.  We need to remove the links to init.d files using
  # chkconfig.  This is something else we can't do in an altroot install,
  # but luckily Linux doesn't support altroot installs anyway.
  #
  if [ $OS = "Linux" ]; then
    INITFILES="utsvc utacleanup utsyscfg"
    for F in $INITFILES
    do
	chkconfig --del $F > /dev/null
    done
    #
    # On SuSE/JDS, chkconfig (based on insserv) does not remove Kill links in
    # lower runlevels.  They must be removed manually.
    #
    if [ -f /etc/SuSE-release ]; then
	# we are installing on either SuSE or JDS
	ETCINIT=/etc/rc.d
    	for F in $INITFILES
    	do
	    rm -f $ETCINIT/rc4.d/K??$F
	    rm -f $ETCINIT/rc3.d/K??$F
	    rm -f $ETCINIT/rc2.d/K??$F
	    rm -f $ETCINIT/rc1.d/K??$F
    	done
    fi
  fi
  rm -f $TMPFILE
fi

umask $OLDMASK

if [ $OS = "Linux" ]; then
    # cleanup the log directory on Linux
    ROOT_VAR_LOG=$PKG_INSTALL_ROOT/var/log/$PKGID
    rm -f $ROOT_VAR_LOG/*
fi
exit 0
