#!/sbin/sh -p
#
# ident "@(#)utacleanup.sh	1.30 02/12/06 SMI"
#
# Copyright 1999-2002 Sun Microsystems, Inc.  All rights reserved.
#
# Return the system to a pristine state with respect to Sun Ray
# X11 sessions.
#
# This should only be run with Sun Ray services stopped.
#

#
# This command can be used out of init.d or standalone.  If we're
# invoked with no arguments we treat that basically the same as a
# "start" invocation.  The difference is that if no argument is given
# 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
LASTLANGFILE=/var/dt/sdtlogin/lastlang_*

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

clean_prototype() {
        /bin/nawk '
                BEGIN {
			remove_lines=0
		}
                /# BEGIN SUNRAY CONFIGURATION/ {
                        remove_lines=1
			print $0
                }
                /# END SUNRAY CONFIGURATION/ {
			remove_lines=0
		}
                remove_lines == 1 {
			next
		}
                {
			print $0
		}
        ' -

}


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.
      # The class ID 'SunRay' must match the line that is dynamically
      # edited into the Xservers file to launch a Sun Ray X server.
      #
      sed								\
	-e '/^[	 ]*Dtlogin\.pidFile:/d'					\
	-e '/^[	 ]*Dtlogin\.servers:/d'					\
	-e '/^[	 ]*Dtlogin\.SunRay\.setup:/d'				\
	-e '/^[	 ]*Dtlogin\.SunRay\.startup:/d'				\
	-e '/^[	 ]*Dtlogin\.SunRay\.reset:/d'				\
	-e '/^[	 ]*Dtlogin\.SunRay\.startAttempts:/d'			\
	-e '/^[	 ]*Dtlogin\.SunRay\.serverAttempts:/d'			\
      	$sf | clean_prototype > $tsrc.new

      echo '#########################################################################' >> $tsrc.new
      echo '#' >> $tsrc.new
      echo "# The following entries are dynamically created by Sun Ray." >> $tsrc.new
      echo "# It is both dangerous and pointless to edit these lines." >> $tsrc.new
      echo "# Any changes made here will be discarded at the next reboot." >> $tsrc.new
      echo '#' >> $tsrc.new
      echo "# Sun Ray required parameters (DO NOT EDIT):" >> $tsrc.new
      echo '#' >> $tsrc.new
      echo 'Dtlogin.pidFile:	/var/dt/Xpid' >> $tsrc.new
      echo 'Dtlogin.servers:	Xservers' >> $tsrc.new
      echo 'Dtlogin.SunRay.startup:		Xstartup' >> $tsrc.new
      echo 'Dtlogin.SunRay.setup:		Xsetup' >> $tsrc.new
      echo 'Dtlogin.SunRay.reset:		Xreset' >> $tsrc.new
      echo 'Dtlogin.SunRay.startAttempts:	1000' >> $tsrc.new
      echo 'Dtlogin.SunRay.serverAttempts:	4' >> $tsrc.new
      echo '#' >> $tsrc.new
      echo "# Sun Ray dynamically created parameters (DO NOT EDIT):" >> $tsrc.new
      echo '#' >> $tsrc.new
    else
      /bin/cat $sf | clean_prototype > $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 $LASTLANGFILE

#
# Discover the group ID for the web admin GUI.
#
WEBGUI_GROUP=`/opt/SUNWut/lib/utadmingid`

#
# Make our new directories
#
umask 022
/bin/mkdir -p $TMPBASE
/bin/mkdir -p $TMPDIR
umask 077
/bin/mkdir -p $TMPBASE/session_info
/bin/mkdir -p $TMPDIR/tokens
umask 022
/bin/mkdir -p $TMPBASE/session_proc
umask 022
/bin/mkdir -p $TMPDIR/displays
umask 027
/bin/mkdir -p $TMPDIR/ctokens
/bin/mkdir -p $TMPDIR/itokens
/bin/chgrp $WEBGUI_GROUP $TMPDIR/displays $TMPDIR/itokens $TMPDIR/ctokens
umask 022
/bin/mkdir -p $TMPDIR/idle
umask 022
/bin/mkdir -p $TMPDIR/dispinfo
umask 022
/bin/mkdir -p $TMPDIR/rescache

#
# Make links from their old locations
#
for i in displays idle tokens itokens ctokens dispinfo rescache
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
