#!/bin/sh
#
# ident "@(#)utacleanup.sh	1.64	11/01/20 Oracle"
#
# Copyright (c) 1999, 2011, Oracle and/or its affiliates. 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.
#
###########################################
# 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.
#

PATH=/usr/bin:/bin:/usr/sbin:/sbin
export PATH

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
	/bin/chown $2:$3 $4
}

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

STOP=true
START=true

if [ "$1" = "stop" ]
then
	START=false
fi

KIOSKID=SUNWkio    
VARTMPDIR="/var/opt/SUNWut/tmp"
ETCSCDIR="/etc/opt/SUNWut/smartcard"
PROBE_ORDER_FILE="$ETCSCDIR/probe_order.conf"
TMPBASE="/tmp/SUNWut"
TMPDIR="$TMPBASE/config"
KIOSKRUNDIR="/var/run/opt/$KIOSKID"
XMGR_DIR="/etc/opt/SUNWut/xmgr"
XMGR_RESET="$XMGR_DIR/reset-dpy"
ADMINGID=/etc/opt/SUNWut/basedir/lib/utadmingid

#
# Clean out any junk that may be lying around.
#
/bin/rm -rf $TMPBASE
/bin/rm -rf $KIOSKRUNDIR
/bin/rm -f ${VARTMPDIR}/utconfig.*
/bin/rm -f ${VARTMPDIR}/utstart.log.*
/bin/rm -f ${VARTMPDIR}/utmntchan
if [ -d ${VARTMPDIR}/pipes ]; then
    /bin/rm -rf ${VARTMPDIR}/pipes/*
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,
    # and then kill the global-zone utaudiod proxy/helper process
    echo "killing all utaudio processes" 1>&2
    /usr/bin/pkill -x utaudio
    /usr/bin/pkill -x utaudiod

    # 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

if $START
then

    #
    # Discover the group ID for the web admin GUI.
    #
    if [ -x $ADMINGID ]
    then
	WEBGUI_GROUP=`$ADMINGID`
    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
fi

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

#
# Make top-level links from their old locations
#
for i in session_info session_proc kiosk user_session
do
	/bin/rm -rf /var/opt/SUNWut/$i
	$START && /bin/ln -s $TMPBASE/$i /var/opt/SUNWut/$i
done

#
# If S10, or if dtlogin is present and no ut_enable_gdm file has been
# created, use dtlogin. Otherwise use gdm. Therefore Linux and
# OpenSolaris will use gdm, S10 users will use dtlogin, and Nevada
# users will default to dtlogin but can override to use gdm. When
# dtlogin goes away on Nevada users will default to gdm.
#
#
if [ "`uname -s` `uname -r`" = "SunOS 5.10" ] \
   || [ -x /usr/dt/bin/dtlogin -a ! -f /etc/opt/SUNWut/ut_enable_gdm ]
then
	XMGR=dtlogin
else
	XMGR=gdm
fi

/bin/rm -f $XMGR_DIR
# resolve the symlink so we don't have to constantly de-reference it
XMGR_NEWDIR=`ls -l /etc/opt/SUNWut/basedir | awk '{print $NF}'`/lib/xmgr
ln -s $XMGR_NEWDIR/$XMGR $XMGR_DIR

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

if $START
then
    # nothing to do
    :
else
    /bin/rm -f $XMGR_DIR
    # reset-dpy recreates the xconfig structure, so we need to nuke
    # TMPBASE again
    /bin/rm -rf $TMPBASE
fi

exit 0
