#!/bin/sh
#
# ident "@(#)utacleanup.sh	1.57	08/07/23 SMI"
#
# Copyright 2008 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
# 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

#
# Return 0 if system is labeled (running TX)
#
smf_is_system_labeled() {
	[ ! -x /bin/plabel ] && return 1
	/bin/plabel > /dev/null 2>&1
	return $?
}

#
# Make a directory with explicit mode/owner/group
# usage: mkdir_explicit mode uid gid dirname
#
mkdir_explicit() {
	/bin/mkdir -m $1 $4
	chown $2:$3 $4
}

# Is GDM available for SRSS use?
isGDMOK2Use() {
	[ -f ${ETCOPTDIR}/ut_enable_gdm ]
}

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

PKGID=SUNWut
KIOSKID=SUNWkio    
OPTDIR="/opt/$PKGID"
ETCOPTDIR="/etc/opt/$PKGID"
VAROPTDIR="/var/opt/$PKGID"
VARTMPDIR="$VAROPTDIR/tmp"
ETCSCDIR="$ETCOPTDIR/smartcard"
PROBE_ORDER_FILE="$ETCSCDIR/probe_order.conf"
TMPBASE="/tmp/$PKGID"
TMPDIR="$TMPBASE/config"
KIOSKRUNDIR="/var/run/opt/$KIOSKID"
XMGR_DIR="$ETCOPTDIR/xmgr"
XMGR_RESET="$XMGR_DIR/reset-dpy"

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

#
# If we're on a TX systen, clean up all device-related stuff
#
if smf_is_system_labeled ; then

    echo "Cleaning up Sun Ray TX Devices" 1>&2

    # kill any utaudio process that users in local zones have started
    # this will kill both the local zone utaudio process and the global
    # zone utaudio helper process
    echo "killing all utaudio processes" 1>&2
    /bin/pkill utaudio

    # deallocate all Sun Ray devices (devices of class "sunray")
    echo "deallocating all Sun Ray devices" 1>&2
    /usr/sbin/deallocate -F -c sunray

    # get a list of all Sun Ray devices and remove them from the
    # allocatable devices database
    devlist="`/usr/sbin/list_devices -c sunray  | awk '{print $2}'`"
    for dev in $devlist
      do
        echo "removing device: $dev" 1>&2
        /usr/sbin/remove_allocatable -f -n $dev
      done

    # clean up the global and local zones' /dev/SUNWut
    for zone in `/usr/sbin/zoneadm list -i`
      do
        if [ $zone = "global" ] ; then
	  zonePrefix=""
	else
	  zonePrefix="/zone/$zone"
	fi
	echo "removing Sun Ray device directories in $zone zone" 1>&2
	/bin/rm -rf $zonePrefix/dev/SUNWut
      done
fi

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

#
# Fix up admingui ownership and permissions for /var/opt/SUNWut/tmp
# because they can get clobbered by a patch install.  Everything else
# under /var/opt/SUNWut that needs to be admin-accessible is created
# dynamically by 'utconfig', not delivered by a package, and therefore
# can't get clobbered by a patch delivery.
# Same thing applies to /etc/opt/SUNWut/smartcard directory
# as well as /etc/opt/SUNWut/smartcard/probe_order.conf
#
if [ -z "$WEBGUI_GROUP" ] ; then
    WEBGUI_GROUP=root
else
    /bin/chgrp "$WEBGUI_GROUP" "$VARTMPDIR"
    /bin/chmod g=rwx "$VARTMPDIR"
    /bin/chgrp "$WEBGUI_GROUP" "$ETCSCDIR"
    /bin/chmod g=rwx "$ETCSCDIR"
    /bin/chgrp "$WEBGUI_GROUP" "$PROBE_ORDER_FILE"
    /bin/chmod u=rw,g=rw,o=r "$PROBE_ORDER_FILE"
fi

#
# Make our new directories
#

mkdir_explicit 755 root root $TMPBASE
mkdir_explicit 755 root root $TMPDIR
mkdir_explicit 700 root root $TMPBASE/session_info
mkdir_explicit 700 root root $TMPDIR/tokens
mkdir_explicit 755 root root $TMPBASE/session_proc

mkdir_explicit 755 root root $TMPBASE/user_session

mkdir_explicit 755 root $WEBGUI_GROUP $TMPDIR/displays
mkdir_explicit 750 root $WEBGUI_GROUP $TMPDIR/ctokens
mkdir_explicit 750 root $WEBGUI_GROUP $TMPDIR/itokens

mkdir_explicit 755 root root $TMPDIR/idle
mkdir_explicit 755 root root $TMPDIR/dispinfo
mkdir_explicit 755 root root $TMPDIR/rescache
mkdir_explicit 755 root root $TMPBASE/kiosk
umask 022

#
# Make links from their old locations
#
for i in displays idle tokens itokens ctokens dispinfo rescache xconfig
do
	/bin/rm -rf $VAROPTDIR/$i
	/bin/ln -s $TMPDIR/$i $VAROPTDIR/$i
done

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

#
# Set platform dependancies
#
OS=`uname -s`
if [ "$OS" = "SunOS" ]; then
	if isGDMOK2Use && [ "`/bin/svcs -o STATE -H gdm 2>/dev/null`" = "online" ]
	then
		XMGR=gdm
	else
		XMGR=dtlogin
	fi
else
	XMGR=gdm
fi

rm -f $XMGR_DIR
ln -s $OPTDIR/lib/xmgr/$XMGR $XMGR_DIR

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

exit 0
