#!/bin/sh
#
# ident "@(#)utacleanup.sh	1.41	05/03/29 SMI"
#
# Copyright 1999-2002,2004,2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Return the system to a pristine state with respect to Sun Ray
# X11 sessions.
#
# This should only be run with Sun Ray services stopped.
#
###########################################
# chkconfig header info for RH
#
# chkconfig: 5 15 32
# description: Reset Sun Ray related X11 session files
#
###########################################
# insserv header info for JDS/SuSE
### BEGIN INIT INFO
# Provides:			utacleanup
# Required-Start:		$remote_fs xdm inetd
# X-UnitedLinux-Should-Start:
# Required-Stop:
# X-UnitedLinux-Should-Stop:
# Default-Start:                5
# Default-Stop:			1 2 3 4
# Short-Description:
# Description:			Reset Sun Ray related X11 session files
### END INIT INFO

#
# This command can be used out of init.d or standalone.  If we're
# invoked with no arguments we treat that the same as a
# "start" invocation.
#

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

umask 022

if [ \( $# -ne 0 \) -a \
     \( \( $# -ne 1 \) -o \
        \( \( "$1" != "start" \) -a \( "$1" != "stop" \) \) \) \
   ]
then
        usage
fi

PKGID=SUNWut
OPTDIR=/var/opt/$PKGID
TMPBASE=/tmp/$PKGID
TMPDIR=$TMPBASE/config
XMGR_RESET=/etc/opt/${PKGID}/xmgr/reset-dpy

#
# Clean out any junk that may be lying around.
#
/bin/rm -rf $TMPBASE
if [ -d ${OPTDIR}/tmp/pipes ]; then
    /bin/rm -r ${OPTDIR}/tmp/pipes/* 2> /dev/null
fi

#
# Discover the group ID for the web admin GUI.
#
if [ -x /opt/SUNWut/lib/utadmingid ]
then
    WEBGUI_GROUP=`/opt/SUNWut/lib/utadmingid`
fi

#
# 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
if [ -n "$WEBGUI_GROUP" ]
then
    /bin/chgrp $WEBGUI_GROUP $TMPDIR/displays $TMPDIR/itokens $TMPDIR/ctokens
fi
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 xconfig
do
	/bin/rm -rf $OPTDIR/$i
	/bin/ln -s $TMPDIR/$i $OPTDIR/$i
done

#
# Make top-level links from their old locations
#
for i in session_proc
do
	/bin/rm -rf $OPTDIR/$i
	/bin/ln -s $TMPBASE/$i $OPTDIR/$i
done

if [ -x ${XMGR_RESET} ]
then
	${XMGR_RESET} "$TMPDIR"
fi

exit 0
