#!/bin/ksh -p
#
# ident "@(#)utusbadm.ksh	1.5 05/03/09 SMI"
#
# Copyright 2004, 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# This script is used as the CLI to enable/disable USB services. 
# Currently it is like a "BIG" switch as in it either enables or
# disables all USB services for all users, with no fine-tuning provided.
# In the future this CLI can be extended to provide fine grained access
# control for USB devices.
#


#set -x

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

BASEDIR=/etc/opt/SUNWut/basedir
SUNWUT=`${BASEDIR}/lib/utprodinfo -r SUNWuto 2>/dev/null`/SUNWut
SUNWUTSBIN=$SUNWUT/sbin
SUNWUTLIB=$SUNWUT/lib
UTPROP=$SUNWUTLIB/utprop
SUNWUTETC=/etc/opt/SUNWut
UTADMIN_CONF=$SUNWUTETC/utadmin.conf

USBKEYNAME=USBEnable

ENABLE_USB=false
DISABLE_USB=false

function printDeprecateMsg {

	print ""
	print "Warning: utusbadm command is deprecated."
	print "         Please use $SUNWUTSBIN/utdevadm to enable/disable usb services."
	print ""

}

function printRestartMsg {

cat <<EOF
   Please restart services for the change to take effect. Use :

            $SUNWUTSBIN/utrestart -c

   Note that this will terminate all sessions.
EOF

}

#
# Enable the USB device services 
#

function enableUSB {

    $UTPROP -a -f $USBKEYNAME true

    if [[ $? -ne 0 ]]
    then
	print "Error: Unable to enable USB services"
	exit 1
    fi

    printRestartMsg
}


#
# Disable the USB device services 
#

function disableUSB {

    $UTPROP -a -f $USBKEYNAME false
    
    if [[ $? -ne 0 ]]
    then
	print "Error: Unable to disable USB services"
	exit 1
    fi

    printRestartMsg
}


# 
# Note : If there is no USBEnable property in the database, it is assumed as 
# enabled. This is the default behaviour. The property is only created the
# first time this command is used to change USB device behaviour.
#

function showCurrentStatus {

    typeset cur_stat=""

    cur_stat=`$UTPROP -k $USBKEYNAME 2> /dev/null`

    if [[ $? -ne 0 ]]
    then
	    # unable to get property, assuming it is enabled
	    cur_stat="true"
    fi

    if ($cur_stat == "true") 
    then
	print "ENABLED"	
    else
	print "DISABLED"	

    fi    
}

function mustBeRoot {
	case "$(id)" in
	'uid=0('*)
		;;
	*)
		print "Must be root to enable/disable USB services" 
		exit 1
		;;
	esac
}

#
# Usage - prints the usage message
# $1 - the exit code
function Usage {

    cat <<!

Usage:
utusbadm -e        # Enable USB services
utusbadm -d        # Disable USB services
utusbadm -h        # Display this usage message
utusbadm           # With no options, utusbadm displays the
                   # current state of the USB services
!
    printDeprecateMsg
    exit $1

}

function mustBeConfigured {
#
# Check to see if utconfig has been run first by checking for the
# existence of /etc/opt/SUNWut/utadmin.conf
#
    if [[ ! -f $UTADMIN_CONF ]]
    then
        print "Please run utconfig first before using utusbadm to change state."
	exit 1
    fi

}

###
# Main program
###

if [ $# -eq 0 ]
then
	showCurrentStatus
        printDeprecateMsg
        exit 0
fi

while getopts "hed" c
do
	case $c in
	h)	Usage 0;;
	e)	if [[ "$DISABLE_USB" == "true" ]] then
			Usage 1
		fi
		ENABLE_USB=true
		;;
	d)	if [[ "$ENABLE_USB" == "true" ]] then
			Usage 1
		fi
		DISABLE_USB=true
		;;
	*)	Usage 1;;
	esac
done

mustBeRoot
mustBeConfigured

if [[ "$ENABLE_USB" == "true" ]] then
	enableUSB
elif [[ "$DISABLE_USB" == "true" ]] then
	disableUSB
fi
printDeprecateMsg

