#
# ident "@(#)postinstall.src	1.10 04/11/10 SMI"
#
# Copyright 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 -u

set -e

# Run utacleanup to initialize the symlinks.  Do this only if we're not
# doing an altroot install.  In the altroot case it'll all get taken care
# of when the client gets booted.
#
if [ -z "$PKG_INSTALL_ROOT" ]; then
  /etc/init.d/utacleanup
fi

# Clean up /var/opt/
#
# VAROPTDIR, the /var/opt path without the prepended PKG_INSTALL_ROOT
# path. ROOT_VAROPTDIR includes the PKG_INSTALL_ROOT path so that it
# does the right thing in an altroot install.

VAROPTDIR=/var/opt/SUNWut
ROOT_VAROPTDIR=$PKG_INSTALL_ROOT$VAROPTDIR

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

#
# create the syslog entries
# NOTE: this is just temporary.  This will eventually move into
# the activation framework.
#
# no need to prepend the PKG_INSTALL_ROOT to UTLIBDIR because it's
# only used within the crontab.
UTLIBDIR=/opt/SUNWut/lib
#
# 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

TMPFILE=$ROOT_VAROPTDIR/tmp/syslog.conf.$$
OLDMASK=`umask`
umask 077
cat /dev/null >$TMPFILE

ENTRY="`sed -n \"s:user\.info.*$VAROPTDIR/log/messages:yes:p\" $ROOT_SYSLOG_CONF`"
if [ -z "$ENTRY" ]; then
  echo "user.info		$VAROPTDIR/log/messages" >>$TMPFILE
fi

ENTRY="`sed -n \"s:local1\.info.*$VAROPTDIR/log/admin_log:yes:p\" $ROOT_SYSLOG_CONF`"
if [ -z "$ENTRY" ]; then
  echo "local1.info		$VAROPTDIR/log/admin_log" >>$TMPFILE
fi

if [ -s $TMPFILE ]; then
  cat $TMPFILE >>$ROOT_SYSLOG_CONF
fi

touch $ROOT_VAROPTDIR/log/messages
touch $ROOT_VAROPTDIR/log/admin_log
#
# HUP the syslod
# NOTE: this should not be done when installing on an altnernate root.
if [ -z "$PKG_INSTALL_ROOT" ] ; then
  pkill -HUP syslogd || true
fi

rm -f $TMPFILE

#
# create crontab entries
# NOTE: this is just temporary.  This will eventually be moved to
# the activation framework .
#
umask 077

#
# NOTE: we cannot use crontab to list the current entries or to install new
# crontabs when installing on an alternate root.  We have no solution for
# this at the moment, it needs to be handled by the activation framework when
# that materialises.  I suppose we could dink with root's crontab file
# directly but that'd be pretty gross.
#
UTLOG_CMD="$UTLIBDIR/utlog -c -d $VAROPTDIR/log"

if [ -n "$PKG_INSTALL_ROOT" ] ; then

  echo "*********************************************************************"
  echo "You are installing this software into an alternative root filesystem."
  echo "This script is unable to establish cron jobs in the alternative root."
  echo "After booting from the alternative root filesystem please create the"
  echo "following crontab entries for the 'root' user so that the Sun Ray log"
  echo "files are rotated periodically:"
  echo
  echo '3 3 * * *  ' "$UTLOG_CMD/messages  1>/dev/null 2>&1"
  echo '33 3 * * * ' "$UTLOG_CMD/admin_log 1>/dev/null 2>&1"
  echo
  echo "*********************************************************************"
  sleep 4

else
  ENTRY_ADDED=false
  TMPFILE=$ROOT_VAROPTDIR/tmp/crontab.tmp.$$
  cat /dev/null >$TMPFILE

  OS=`uname -s`
  if [ $OS = "SunOS" ]; then
    (set +e; crontab -l root 2>/dev/null >>$TMPFILE 2> /dev/null)
  else
    (set +e; crontab -u root -l 2>/dev/null >>$TMPFILE 2> /dev/null)
    if [[ $? -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)
      if [ $? -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
  fi

  ENTRY="`sed -n \"s:$UTLOG_CMD/messages:yes:p\" $TMPFILE`"
  if [ -z "$ENTRY" ]; then
    echo "3 3 * * * $UTLOG_CMD/messages 1>/dev/null 2>&1" >>$TMPFILE
    ENTRY_ADDED=true
  fi

  ENTRY="`sed -n \"s:$UTLOG_CMD/admin_log:yes:p\" $TMPFILE`"
  if [ -z "$ENTRY" ]; then
    echo "33 3 * * * $UTLOG_CMD/admin_log 1>/dev/null 2>&1" >>$TMPFILE
    ENTRY_ADDED=true
  fi

  if $ENTRY_ADDED; then
    crontab $TMPFILE
  fi

  rm -f $TMPFILE

  #
  # For Linux only.  We need to create 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="utsyscfg utacleanup utsvc"
    for F in $INITFILES
    do
	chkconfig --add $F > /dev/null
    done
    #
    # On SuSE/JDS, chkconfig (based on insserv) does not create Kill links in
    # lower runlevels.  They must be created manually.
    #
    if [ -f /etc/SuSE-release ]; then
	# we are installing on either SuSE or JDS
	ETCINIT=/etc/rc.d
	KILLFILES="K31utsyscfg K15utacleanup K02utsvc"
    	for F in $KILLFILES
    	do
	    INITF=`echo $F | sed -e "s/^K..//"`
	    ln -s ../$INITF $ETCINIT/rc4.d/$F
	    ln -s ../$INITF $ETCINIT/rc3.d/$F
	    ln -s ../$INITF $ETCINIT/rc2.d/$F
	    ln -s ../$INITF $ETCINIT/rc1.d/$F
    	done
    fi
  fi
fi

# if gmSignature file exists, remove it!!  We don't want to use a
# leftover file.
#
rm -f $PKG_INSTALL_ROOT/etc/opt/SUNWut/gmSignature
umask 177
touch $PKG_INSTALL_ROOT/etc/opt/SUNWut/gmSignature
chown root $PKG_INSTALL_ROOT/etc/opt/SUNWut/gmSignature
umask $OLDMASK

exit 0
