#!/bin/ksh -p
#
# ident "@(#)utaudioctl.ksh	1.4	12/11/28 Oracle"
#
# Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
#

set -u

function rmLink  {
	typeset LINK=$1
	if [ -L $LINK ] ; then
		rm -rf $LINK
		if $VERB; then
			print "$LINK is removed"
		fi
	fi
}

function mkLink  {

	typeset LINK_TO=$1
	typeset LINK=$2

	rm -rf $LINK
	ln -fs $LINK_TO $LINK
	if [ $? -ne 0 ]; then
		logit "Error: [${PROG}] Failed to create $LINK "
		return 1
	elif $VERB; then
		print "$LINK to $LINK_TO link made"
	fi
	return 0
}

function smf_is_system_labeled {

	[ ! -x /bin/plabel ] && return 1
	/bin/plabel > /dev/null 2>&1
	return $?
}


########################################################################
#
# Operation entry points, one for each keyword.	 This is where the
# specific work for this feature happens.
#
########################################################################


#
# disable()
#
function disable {
	typeset status=0

	rm $UTAUDIO_BIN
	if [ -f "$PULSEAUDIO_BIN" ]; then
		if [ -f  "$PULSEAUDIO_DAEMONCONF_BAK" ]; then
			cp $PULSEAUDIO_DAEMONCONF_BAK  $PULSEAUDIO_DAEMONCONF
		fi
	fi

	return $status
}

#
# enable ()
#
function enable {

	if [ -f "$PULSEAUDIO_BIN" ]; then
		cp $UTAUDIO_PULSEAUDIO $UTAUDIO_BIN
		cp $PULSEAUDIO_DAEMONCONF $PULSEAUDIO_DAEMONCONF_BAK
		mkLink $UTAUDIO_PULSEAUDIO_DAEMONCONF $PULSEAUDIO_DAEMONCONF
	else
		if [ "$OS" = "SunOS" ]; then
			cp $UTAUDIO_SADA $UTAUDIO_BIN
		else
			cp $UTAUDIO_OSS $UTAUDIO_BIN
		fi
	fi

	if ! ( smf_is_system_labeled ); then
		chmod u+s $UTAUDIO_BIN
	fi
		
	return 0
}

#########################################
# LOCAL VARIABLES
# They must defined here to resolve the some of parameter resolution
# problem
#########################################
local_variables() {
	OS=`uname -s`
	PULSEAUDIO_BIN="/usr/bin/pulseaudio"
	PULSEAUDIO_DAEMONCONF="/etc/pulse/daemon.conf"
	PULSEAUDIO_DAEMONCONF_BAK="/etc/pulse/daemon.conf.BAK"

	UTAUDIO_BIN="/etc/opt/SUNWut/utaudio"
	UTAUDIO_PULSEAUDIO="/opt/SUNWut/lib/pulse/utaudio"
	UTAUDIO_SADA="/opt/SUNWut/lib/sada/utaudio"
	UTAUDIO_OSS="/opt/SUNWut/lib/oss/utaudio"
	UTAUDIO_PULSEAUDIO_DAEMONCONF="/etc/opt/SUNWut/pulse/daemon.conf"
}

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

#########################################
#
# variables used in the shared script utctl-shlib
#
#########################################
DESCR="sets up utaudio configuration"

########################################################################
#
# Execution starts here
#

PROGPATH=`dirname $0`
. $PROGPATH/utctl-shlib
