#!/bin/ksh -p
#
# ident "@(#)utpkg-utoctl.ksh	1.8 11/02/21 Oracle"
#
# Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
#

set -u

function disable {
    
    typeset SVCADM="/usr/sbin/svcadm"
    typeset SVCCFG="/usr/sbin/svccfg"
    typeset UTRCMD_FMRI="svc:/network/utrcmd/tcp6"
    typeset UTRCMD_MANIFEST_PATH="/var/svc/manifest/network/utrcmd-tcp6.xml"
    
   
# Remove files created as side-effects of 
# scripts delivered by this package.
#
    
    typeset FILES=""
    FILES="$FILES /etc/opt/SUNWut/terminals"
    FILES="$FILES /etc/opt/SUNWut/auth.props.bu"
    FILES="$FILES /etc/opt/SUNWut/policy/utpolicy"
    FILES="$FILES /etc/opt/SUNWut/policy/utpolicy.bu"
    
    rm -f $FILES
    
# Run utrepair to remove modifications from all CDE / Solaris files 
# for use with Sun Ray
#
    typeset SUNWUTLIB="/opt/SUNWut/lib"
    typeset PROTODIR="$SUNWUTLIB/prototype"
    
# List of files to be updated by utrepair
#
    typeset MODFILES="  /etc/X11/xdm/Xstartup /etc/X11/xdm/Xsetup /etc/X11/xdm/Xreset /etc/services"
    typeset XINETD_DIR=/etc/xinetd.d
    typeset INETD_CONF=/etc/inetd.conf
    if [ -d $XINETD_DIR ]; then
	# for RH server
	MODFILES="$MODFILES $XINETD_DIR/sunray_utrcmdd"
    fi
    if [ -f $INETD_CONF ]; then
	# for Solaris and JDS servers
	MODFILES="$MODFILES $INETD_CONF"
    fi
    
    if [ -x $SUNWUTLIB/utrepair ]; then
  # Build the argument list for utrepair
  #
	ARGS="-r $MODFILES"
	
	$SUNWUTLIB/utrepair $ARGS > /dev/null 2>&1
    fi
    if [ -f $SVCADM ]; then
	$SVCADM disable -s $UTRCMD_FMRI 1>/dev/null 2>&1
	$SVCCFG delete $UTRCMD_FMRI 1>/dev/null 2>&1
	rm -f $UTRCMD_MANIFEST_PATH
    fi

# Remove the xkb directory that we constructed
    rm -rf $SUNWUTLIB/xkb
        
# Always succeeds
    return 0
    
}

function enable {

# This script is to be run on linux only. It updates
# /opt/SUNWut/lib/xkb with a link to the xkbcomp command.

    if [ -d /usr/X11R6/lib/X11/xkb ]; then
	XKBDIR=/usr/X11R6/lib/X11/xkb
    elif [ -d /usr/share/X11/xkb ]; then
	XKBDIR=/usr/share/X11/xkb
    else 
	print -u2 "xkb directory not found"
	return 1
    fi
    
    typeset LIBDIR=/opt/SUNWut/lib
    
    typeset UTDSTXKBDIR=$LIBDIR/xkb
    
    cd $UTDSTXKBDIR
    
# make sure that compiled link exists
    if [ ! -e $UTDSTXKBDIR/compiled ]; then
	if [ -d /var/lib/xkb ]; then
	    rm -f $UTDSTXKBDIR/compiled; ln -s /var/lib/xkb $UTDSTXKBDIR/compiled
	else
	# this cannot not happen since SUNWuto will create the directory if it 
	# does not already exist unless instalation failed.
	    logit "**Missing /var/lib/xkb directory.  This could be an installation"
	    logit "problem.  This is required in order for Sun Ray Xnewt to support"
	    logit "the XKB extension."
	    return 1
	fi
    fi
    
# make sure that the xkbcomp exists
    if [ ! -e $UTDSTXKBDIR/xkbcomp ]; then
    # on SLES, it's located under the $XKBDIR.
    # on RH, xkbcomp is located under /usr/bin rather than the default $XKBDIR.
	for xdir in $XKBDIR /usr/bin
	  do
	  if [ -x $xdir/xkbcomp ]; then
	      rm -f $UTDSTXKBDIR/xkbcomp; ln -s $xdir/xkbcomp $UTDSTXKBDIR/xkbcomp
	      break
	  fi
	done
	if [ ! -L $UTDSTXKBDIR/xkbcomp ]; then
	# this error will cause the xkb to fail
	    logit "**Cannot locate xkbcomp utility on your system.  This is required in"
	    logit "order to enable XKB support.  Please find the xkbcomp utility on your"
	    logit "system and create a symbolic under $UTDSTXKBDIR."
	    return 1
	fi
    fi

    return 0
    
}


########################################################################
# Local Variables
# They must defined here to resolve some of the parameter resolution
# problem
########################################################################
local_variables() {
    return
}


########################################################################
#
# Configurable parameter
#
########################################################################
# NONE


########################################################################
#
# Variables used in the shared script utctl-shlib
#
########################################################################
DESCR=" Establishes and cleans some Sun Ray related files"


########################################################################
#
# Execution starts here
########################################################################
PROGPATH=`dirname $0`
. $PROGPATH/utctl-shlib
