#!/bin/ksh -p
#
# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#
# ident "@(#)ut_audio_clean.ksh 1.9     07/06/08 SMI"
#
#
# This is the clean script for the Sun Ray DTU internal audio device.
# 
# Following is the syntax for calling the script:
#	scriptname [-s|-f|-i|-I] devicename [-A|-D] [username] [zonename]
#           [zonepath]
#
# $1:	-s for standard cleanup by a user
#	-f for forced cleanup by an administrator
#	-i for boot-time initialization (when the system is booted with -r)
#	-I to suppress error/warning messages; the script is run in the '-i'
#	mode
#
# $2:	devicename - device to be allocated/deallocated, e.g., sr0
#
# $3:	-A if cleanup is for allocation, or -D if cleanup is for deallocation.
#
# $4:	username - run the script as this user, rather than as the caller.
#
# $5:	zonename - zone in which device to be allocated/deallocated
#
# $6:	zonepath - root path of zonename
#
# Unless the clean script is being called for boot-time
# initialization, it may communicate with the user via stdin and
# stdout.  To communicate with the user via CDE dialogs, create a
# script or link with the same name, but with ".windowing" appended.
# For example, if the clean script specified in device_allocate is
# /etc/security/xyz_clean, that script must use stdin/stdout.  If a
# script named /etc/security/xyz_clean.windowing exists, it must use
# dialogs.  To present dialogs to the user, the dtksh script
# /etc/security/lib/wdwmsg may be used.
#
# This particular script, ut_audio_clean, will work using stdin/stdout, or
# using dialogs.  A symbolic link ut_audio_clean.windowing points to
# ut_audio_clean.
#
#  EXIT VALUES
#     The following exit values are returned:
#
#     0             Successful completion.
#     1             Any error.
#     2             A system error.
#     3             Operation canceled.
#

USAGE="usage: $0 [-s|-f|-i|-I] devicename [-A|-D][username][zonename][zonepath]"
PATH="/usr/bin:/usr/sbin"
CLEAN_PROG="/etc/security/lib/audio_clean"
WDWMSG="/etc/security/lib/wdwmsg"
MODE="allocate"

# define script exit return values
exit_SUCCESS=0
exit_ERROR=1
exit_SYSERROR=2
exit_CANCEL=3

trap "exit ${exit_CANCEL}" INT TERM QUIT TSTP ABRT

if [ `basename $0` != `basename $0 .windowing` ]; then
  WINDOWING="yes"
else
  WINDOWING="no"
fi

#
# 		*** Shell Function Declarations ***
#

# msg - display a message to the user
# call with '-c' as first arg to putup a <CANCEL> button
# in a windowing environment
msg() {

	CANCEL=""
	if [ "$1" = "-c" ] ; then
	    CANCEL="Cancel"
	    shift
	fi

  	if [ "$WINDOWING" = "yes" ]; then
	  if [ $MODE = "allocate" ]; then
	    TITLE="Audio Device Allocation"
	    else
	    TITLE="Audio Device Deallocation"
	  fi
	  $WDWMSG "$*" "$TITLE" OK ${CANCEL}
	  msg_type="$?"
	  case $msg_type in
	  	0) return ;;			# <OK> pressed
		1) exit ${exit_CANCEL} ;;	# <CANCEL> pressed
	       \?) exit ${exit_CANCEL} ;;	# user dismisses window
	  esac

	else  
	  echo "$*"
	fi
}

alloc_msg() {
	msg "${DEVICE} : Audio device allocated at label $ZONELABEL." \
	"\nTurn on microphone if audio recording is to be performed." \
	"\nTurn microphone off when not recording."
}

dealloc_msg() {
	msg "${DEVICE} : Audio device deallocated from label $ZONELABEL." \
	"\nPlease make sure the microphone is turned off."
}

fail_msg() {
	if [ "$MODE" = "allocate" ]; then
		msg "Allocate of $DEVICE failed."
	else
		msg "Deallocate of $DEVICE failed."
	fi
}

#
# 	Main program
#

# Check syntax, parse arguments.

while getopts ifsI c
do
	case $c in
	i)
		FLAG=$c;;
	f)
		FLAG=$c;;
	s)
		FLAG=$c;;
	I)
		FLAG=i
		silent=y;;
	\?)	msg $USAGE
	     	exit ${exit_ERROR};;
	esac
done

shift `expr $OPTIND - 1`

DEVICE="$1"
if [ "$2" = "-A" ]; then
	MODE="allocate"
elif [ "$2" = "-D" ]; then
	MODE="deallocate"
fi
if [ "$MODE" != "allocate" -a "$MODE" != "deallocate" ]; then
	msg $USAGE
	exit ${exit_ERROR}
fi
ZONENAME=$4
ZONEPATH=$5
ZONELABEL="`getlabel -S $ZONEPATH | nawk -F ':\t' '{print $2}'`"

$CLEAN_PROG -$FLAG $DEVICE

if [ $? -ne 0 ]; then
	fail_msg
	exit ${exit_ERROR}
fi

if [ "$MODE" = "allocate" ]; then
	alloc_msg
else
	dealloc_msg
fi

exit ${exit_SUCCESS}

