#!/bin/ksh -p
#
# ident "uttsc.ksh $Date: 2006-09-25 15:47:18 -0700 (Mon, 25 Sep 2006) $ $Revision: 54 $ SMI"
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

PATH=/bin:/usr/bin
SUNWUTETC=/etc/opt/SUNWut
SRSS_PKG_LOC="`$SUNWUTETC/basedir/lib/utprodinfo -r SUNWuto`/SUNWut"
SRSS_BIN=$SRSS_PKG_LOC/bin
UTADMIN_CONF=$SUNWUTETC/utadmin.conf
SRSS_DYNAMIC_ADMIN=$SRSS_PKG_LOC/lib/libutadmin.so.1

SRCBINARY=/opt/SUNWuttsc/lib/uttsc-bin
OS=`/bin/uname -s`

function cleanup {
        AUDIO_PROC=`ps -ef | grep utaudioid-$$ | grep -v grep | awk '{print $2}'`
	if [[ -n $AUDIO_PROC ]]; then
		kill -KILL $AUDIO_PROC
	fi
        exit $1
}

function checkForPatch {

        if [[ ! -f $SRSS_DYNAMIC_ADMIN ]]; then
	      print -u2 "Unable to launch the Sun Ray Connector. Please ensure that the latest SRSS patch is installed."
	      exit 1
	fi	  
}

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 -u2 "Please run utconfig first before launching Sun Ray Connector for Windows Operating Systems."
	exit 1
    fi
}


function validateSRSSversion {

    majorVersion=`$SUNWUTETC/basedir/lib/utprodinfo -p SUNWuto PRODVERS | awk -F. '{print $1}'`
    minorVersion=`$SUNWUTETC/basedir/lib/utprodinfo -p SUNWuto PRODVERS | awk -F. '{print $2}'`
    subminorVersion=`$SUNWUTETC/basedir/lib/utprodinfo -p SUNWuto PRODVERS | awk -F. '{print $3}'`
    
    case "$OS" in # {
      SunOS)
	     if [[ $majorVersion -lt 3 || $majorVersion -eq 3 && $minorVersion -lt 1 ]]; then
	       print "Only SRSS 3.1 and above are supported"
	       exit 1
	     fi
	     #2. Check for Patch (on solaris)
	     checkForPatch
	     ;;
      Linux)
	     if [[ $majorVersion -lt 3 || $majorVersion -eq 3 && $minorVersion -lt 1 || 
	       $majorVersion -eq 3 && $minorVersion -eq 1 && $subminorVersion -lt 1 ]]; then
		 print "Only SRSS 3.1.1 and above are supported"
		 exit 1
	     fi
	     ;;
      *)
	 print "Unsupported platform: " $OS
	 exit 1
	 ;;
    esac # }
}

#
#MAIN
# 

# 1. Check for SRSS version.. as only 3.1 and above are supported on solaris and 3.1.1
# and above on Linux. 
# 2. Check for patch version on solaris
validateSRSSversion
   
#3. Check for SRSS configured status
mustBeConfigured


# 4. Execute the audio launcher

# need to keep default if utaudio fails.

OLD_AUDIODEV=$AUDIODEV

AUDIODEV="$($SRSS_BIN/utaudio utaudioid-$$ &)"

if [[ -z $AUDIODEV ]]; then
	# Check if it's Trusted Solaris Environment by checking
	# the existence of trusted network remote-host database
        if [[ -f /etc/security/tsol/tnrhdb ]]; then

                if [[ -n $OLD_AUDIODEV ]]; then
                        # Audio device is available only if
                        # the device is allocated to the user at the label
                        # in which uttsc is executed. 
                        device_owner=`ls -l $OLD_AUDIODEV | awk '{print $3}'`
                        userid=$USER
                        if [[ $device_owner = $userid ]]; then
				# If getlabel succeeds then device is not allocated at
				# a higher label
                                getlabel $OLD_AUDIODEV >/dev/null 2>&1
                                if [[ $? -eq 0 ]]; then
				    # Check if device is allocated at same label.
                                    audiodev_label=`getlabel $OLD_AUDIODEV | awk -F"	" '{print $2}'`
                                    process_label=`plabel $$ | awk -F"	" '{print $2}'`
                                    if [[ "$audiodev_label" = "$process_label" ]]; then
                                            AUDIODEV=$OLD_AUDIODEV
                                    fi
                                fi
                        fi 
		fi
        else
                print "Unable to launch utaudio. Using default audio device."
                AUDIODEV=$OLD_AUDIODEV
        fi
fi

export AUDIODEV

trap "cleanup 1" HUP INT TERM KILL QUIT 

export UTAUDIODEV=$AUDIODEV

# 5. Start the uttsc binary. On exit, cleanup and exit with return status from binary.

$SRCBINARY "$@" 
return_status=$?

cleanup $return_status

