#
#  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.2 $
#
# Author: Gregory P. Myrdal <Myrdal@MissionCriticalLinux.Com>

#
# Shell library for user script functions.
#

SH_LIB=$(dirname $0)

LIBRARIES=

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

#
# startUser serviceID
#
startUser()
{
	typeset user_script
	typeset ret_val

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

	typeset svcID=$1

	user_script=$(getSvcUserScript $DB $svcID)
	ret_val=$?
	case $ret_val in
	  0) : ;;			# found it
	  2) return $SUCCESS ;;	# a user script does not exist
	  *) logAndPrint $LOG_ERR "\
Cannot find user script for service $SVC_NAME, err=$ret_val"
	    return $FAIL ;;
	esac

	if [ -z "$user_script" ]; then	# it must not exist
	  return $SUCCESS
	fi
	
	if [ ! -f $user_script ]; then
	  logAndPrint $LOG_ERR "\
User script '$user_script' is defined for service $SVC_NAME, but does not exist"
	  return $FAIL
	fi

	if [ ! -x $user_script ]; then
	  logAndPrint $LOG_WARNING "\
User script '$user_script' is not executable, turning execute bit on"
	  chmod +x $user_script
	fi

	logAndPrint $LOG_NOTICE "Running user script '$user_script start'"

	$user_script start
	ret_val=$?
	if [ $ret_val -ne 0 ]; then
	  logAndPrint $LOG_ERR "\
User script '$user_script start' returned error $ret_val"
	  return $FAIL
	fi

	return $SUCCESS
}

#
# stopUser serviceID
#
stopUser()
{
	typeset user_script
	typeset ret_val

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

	typeset svcID=$1

	user_script=$(getSvcUserScript $DB $svcID)
	ret_val=$?
	case $ret_val in
	  0) : ;;			# found it
	  2) return $SUCCESS ;;	# a user script does not exist
	  *) logAndPrint $LOG_ERR "\
Cannot find user script for service $SVC_NAME, err=$ret_val"
	    return $FAIL ;;
	esac

	if [ -z "$user_script" ]; then	# it must not exist
	  return $SUCCESS
	fi
	
	#
	# Log only a warning if the stop script does not exist.  The
	# stop script may be on the shared device, thus, will not be
	# there during service stops at boot time.
	#
	if [ ! -f $user_script ]; then
	  logAndPrint $LOG_WARNING "\
User script '$user_script' is defined for service $SVC_NAME, but does not exist"
	  return $SUCCESS
	fi

	if [ ! -x $user_script ]; then
	  logAndPrint $LOG_WARNING "\
User script '$user_script' is not executable, turning execute bit on"
	  chmod +x $user_script
	fi

	logAndPrint $LOG_NOTICE "Running user script '$user_script stop'"

	#
	# Try to stop it.  If we get a '0' (stopped) or '3'
	# (which generally means "all daemons stopped", but only in the
	# context of a status check), then we're golden.
	#
	$user_script stop
	ret_val=$?
	if [ $ret_val -eq 0 -o $ret_val -eq 3 ]; then
	  return $SUCCESS
	fi

	#
	# It returned an error code.  Some user scripts return error codes
	# if we try to stop it while it isn't running.  So, we now check
	# the status of the script.
	#
	# If we get '3' (all daemons stopped), we're golden - otherwise,
	# we've failed to stop the service.
	#
	$user_script status
	if [ $? -eq 3 ]; then
	  return $SUCCESS
	fi
	logAndPrint $LOG_ERR "\
User script '$user_script stop' returned error $ret_val"
	return $FAIL
}

#
# statusUser serviceID
#
statusUser()
{
	typeset user_script
	typeset ret_val

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

	typeset svcID=$1

	user_script=$(getSvcUserScript $DB $svcID)
	ret_val=$?
	case $ret_val in
	  0) : ;;			# found it
	  2) return $SUCCESS ;;	# a user script does not exist
	  *) logAndPrint $LOG_ERR "\
Cannot find user script for service $SVC_NAME, err=$ret_val"
	    return $FAIL ;;
	esac

	if [ -z "$user_script" ]; then	# it must not exist
	  return $SUCCESS
	fi
	
	if [ ! -f $user_script ]; then
	  logAndPrint $LOG_WARNING "\
User script '$user_script' is defined for service $SVC_NAME, but does not exist"
	  return $FAIL
	fi

	if [ ! -x $user_script ]; then
	  logAndPrint $LOG_WARNING "\
User script '$user_script' is not executable, turning execute bit on"
	  chmod +x $user_script
	fi

	logAndPrint $LOG_DEBUG "Running user script '$user_script status'"

	$user_script status
	ret_val=$?
	if [ $ret_val -ne 0 ]; then
	  logAndPrint $LOG_ERR "\
User script '$user_script status' returned error $ret_val"
	  return $FAIL
	fi

	return $SUCCESS
}

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

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

	typeset action=$1
	typeset svcID=$2

	case "$action" in
	'start')
	  startUser $svcID
	  return $?
	  ;;

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

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