#
#  Copyright Red Hat Inc., 2002
#  Copyright Mission Critical Linux, 2000
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version.
#
#  This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; see the file COPYING.  If not, write to the
#  Free Software Foundation, Inc.,  675 Mass Ave, Cambridge, 
#  MA 02139, USA.
#

# $Revision: 1.6 $
#
# Author: Gregory P. Myrdal <Myrdal@MissionCriticalLinux.Com>

#
# Shell library for device functions.
#
# Requires globals.bash to be sourced in.
#

SH_LIB=$(dirname $0)

LIBRARIES=

for library in $LIBRARIES
do
	if [ -f $library ]; then
	  . $library
	fi
done

#
# startDevice serviceID deviceID
#
startDevice ()
{

	if [ $# -ne 1 ]; then
	  logAndPrint $LOG_ERR "Usage: startDevice device"
	  return $FAIL
	fi

	typeset dev=$1

	#
	# Make sure the device special file exists
	#
	if [ ! -e $dev ]; then
	  logAndPrint $LOG_ERR "Device special file '$dev' does not exist"
	  return $FAIL
	fi

	return $SUCCESS
}

#
# stopDevice device
#
stopDevice ()
{

	if [ $# -ne 1 ]; then
	  logAndPrint $LOG_ERR "Usage: stopDevice device"
	  return $FAIL
	fi

	typeset dev=$1

	return $SUCCESS
}

#
# startDevices serviceID
#
startDevices()
{
	typeset dev
	typeset -i ret_val
	typeset tmpfile_str=/tmp/$MYNAME.startDevices.XXXXXX
	typeset tmpfile
	typeset tokenlist
	typeset token
	tmpfile=$(mktemp $tmpfile_str); ret_val=$?

	if [ $# -ne 1 ]; then
	  logAndPrint $LOG_ERR "Usage: startDevices serviceID"
	  return $FAIL
	fi

	typeset svcID=$1

	if [ -z "$tmpfile" ]; then
	  logAndPrint $LOG_ERR \
"Cannot create temporary file name with 'mktemp $tmpfile_str', err=$ret_val"
	  return $FAIL
	fi

	tokenlist=$(getSvcDeviceTokenList $DB $svcID)
	for token in $tokenlist; do
	  dev=$(getSvcDevice $DB $token)
	  case $? in
	    0) : ;;		# found it, let it fall through
	    2) break ;;		# done getting devices, no more left
	    *) logAndPrint $LOG_ERR "\
startDevices: Cannot get device $devN of service $SVC_NAME, err=$?"
	       rm -r $tmpfile
	       return $FAIL;;
	  esac

	  startDevice $dev
	  if [ $? -ne $SUCCESS ]; then
	    logAndPrint $LOG_ERR "Cannot start device $dev of service $SVC_NAME"
	    rm -r $tmpfile
	    return $FAIL
	  fi
	done

	rm -r $tmpfile
	return $SUCCESS
}

#
#
#
stopDevices()
{
	typeset dev
	typeset devN=$MIN_TOKEN_ID
	typeset -i ret_val
	typeset tmpfile_str=/tmp/$MYNAME.stopDevices.XXXXXX
	typeset tmpfile
	tmpfile=$(mktemp $tmpfile_str); ret_val=$?

	if [ -z "$tmpfile" ]; then
	  logAndPrint $LOG_ERR \
"Cannot create temporary file name with 'mktemp $tmpfile_str', err=$ret_val"
	  return $FAIL
	fi

	tokenlist=$(getSvcDeviceTokenList_r $DB $svcID)
	for token in $tokenlist; do
	  dev=$(getSvcDevice $DB $token)
	  case $? in
	    0) : ;;		# found it, let it fall through
	    2) break ;;		# done getting devices, no more left
	    *) logAndPrint $LOG_ERR "\
stopDevices: Cannot get device $devN of service $SVC_NAME, err=$?"
	       rm -r $tmpfile
	       return $FAIL;;
	  esac

	  stopDevice $dev
	  if [ $? -ne $SUCCESS ]; then
	    logAndPrint $LOG_ERR "Cannot stop device $dev of service $SVC_NAME"
	    rm -r $tmpfile
	    return $FAIL
	  fi
	done

	rm -r $tmpfile
	return $SUCCESS
}

#
# statusDevices
#
statusDevices()
{
	return $SUCCESS
}

#
# device
#
# Given an action and service ID number run that action for that service.
#
device()
{

	if [ $# -ne 2 ]; then
	  logAndPrint $LOG_ERR "Usage: device [start, stop, status] serviceID"
	  return $FAIL
	fi

	typeset action=$1
	typeset svcID=$2

	case "$SVC_ACTION" in
	'start')
	  startDevices $svcID
	  return $?
	  ;;

	'stop')
	  stopDevices $svcID
	  return $?
	  ;;

	'status')
	  statusDevices $svcID
	  return $?
	  ;;
	esac
}
