#!/sbin/sh -p
#
# Copyright (c) 09/17/99 Sun Microsystems, Inc. All Rights Reserved
# @(#)utacleanup.sh	1.12 08/21/00
#
# Return the system to it's pristine state with respect to Sun Ray
# X11 sessions
#
# This should only be run with ut-services stopped.
#

#
# This command can be used out of init.d or standalone. So, we allow
# the single arg "start" or no args.
#
# If no args are used, then the cleanup is done unconditionally; the check
# file /tmp/.utacleanup.once is not consulted or changed
# 
#

usage() {
	echo "Usage: $0 [start] [stop]" 1>&2
	exit 1
}

umask 022

if [ $# -eq 1 ]
then
	chkfile=/tmp/.utacleanup.once
	case "$1" in
	'start')
		#
		# Only do this once per boot from startup scripts
		#
		if [ -f $chkfile ]
		then
			exit 0
		fi
		/bin/touch $chkfile
		;;
	'stop')
		# we don't do anything on stop except remove the check file
		/bin/rm -f $chkfile 2>/dev/null
		exit 0
		;;
	*)
		usage
		;;
	esac
elif [ $# != 0 ]
then
	usage
fi

XDIR=/etc/dt/config
UDIR=/usr/dt/config
PKGID=SUNWut
OPTDIR=/var/opt/$PKGID
TMPBASE=/tmp/$PKGID
TMPDIR=$TMPBASE/config
TMPXDIR=$TMPDIR/xconfig

#DEBUG
#XDIR=$HOME/uttest/$XDIR
#UDIR=$HOME/uttest/$UDIR
#OPTDIR=$HOME/uttest/$OPTDIR
#TMPBASE=/tmp/$LOGNAME
#TMPDIR=$TMPBASE/config
#TMPXDIR=$TMPDIR/xconfig
#DEBUG

cleanup() {
  /bin/mkdir -p $XDIR
  /bin/mkdir -p $TMPXDIR

  for src 
  do
    tsrc=$TMPXDIR/$src
    sproto=$XDIR/$src.$PKGID.prototype

    #
    # If the $XDIR file exists and is not a link, back it up to use as
    # the prototype
    #
    if [ -f $XDIR/$src -a ! -h $XDIR/$src ]
    then
      if /bin/cmp -s $XDIR/$src $UDIR/$src
      then
	# No need to retain - just a copy of /usr/dt
        :
      else
	/bin/mv -f $XDIR/$src $sproto
      fi
    fi

    #
    # Make the link
    #
    /bin/rm -f $XDIR/$src
    /bin/ln -s $tsrc $XDIR/$src

    #
    # Use the prototype if it exists, or get a vanilla one from
    # /usr/dt/config
    #
    if [ -f $sproto ]
    then
      sf=$sproto
    else
      sf=$UDIR/$src
    fi

    if [ "$src" = "Xconfig" ]
    then
      #
      # Make sure we have the correct values for what we care about
      #
      sed								\
	-e '/^[	 ]*Dtlogin\.pidFile:/d'					\
	-e '/^[	 ]*Dtlogin\.servers:/d'					\
	-e '/^[	 ]*Dtlogin\*setup:/d'					\
	-e '/^[	 ]*Dtlogin\*startup:/d'					\
	-e '/^[	 ]*Dtlogin.*startAttempts:/d'				\
	-e '/^[	 ]*Dtlogin.*serverAttempts:/d'				\
      	$sf > $tsrc.new

      echo '#' >> $tsrc.new
      echo "# $PKGID required parameters:" >> $tsrc.new
      echo '#' >> $tsrc.new
      echo 'Dtlogin.pidFile:	/var/dt/Xpid' >> $tsrc.new
      echo 'Dtlogin.servers:	Xservers' >> $tsrc.new
      echo 'Dtlogin*startup:	Xstartup' >> $tsrc.new
      echo 'Dtlogin*setup:	Xsetup' >> $tsrc.new
      echo 'Dtlogin*startAttempts:	1000' >> $tsrc.new
      echo 'Dtlogin*serverAttempts:	4' >> $tsrc.new
    else
      /bin/cat $sf > $tsrc.new
    fi
  done

  #
  # Create a copy and then move into place so there is never a broken
  # or partial version installed
  #
  # Here's where the files are replaced
  #
  for src 
  do
    tsrc=$TMPXDIR/$src
    /bin/mv -f $tsrc.new $tsrc
  done

  return 0
}

#
# Clean out any junk that may be lying around
#
/bin/rm -rf $TMPBASE

#
# Make our new directories
#
umask 022
/bin/mkdir -p $TMPBASE
/bin/mkdir -p $TMPDIR
umask 077
/bin/mkdir -p $TMPDIR/tokens
umask 022
/bin/mkdir -p $TMPDIR/displays
umask 027
/bin/chgrp root $TMPDIR/displays
umask 022
/bin/mkdir -p $TMPDIR/idle

#
# Make links from their old locations
#
for i in displays idle tokens
do
	/bin/rm -rf $OPTDIR/$i
	/bin/ln -s $TMPDIR/$i $OPTDIR/$i
done

#
# Reinitialize all dtlogin setup (create the new Xservers last)
#
cleanup Xconfig Xservers

exit 0
