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

#
# Shell API for access to the cluster database.  Since the Service
# Manager does not directly modify services there are no setSvc*()
# functions in this file.  The Service Manager only needs to read the
# information and use it for service configuration.
#

LIBRARIES=

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

#
# Get the log level of the Service Manager
#
getSvcMgrLogLevel ()
{
	typeset DB=$1

	# clusvcmgrd%logLevel
	$GETCONFIG -f $DB $SVCMGR_STR$SEP$SVCMGR_LOGLEVEL_STR
	return $?
}


#
# Get the NFS Lock-workaround option of the Service Manager
#
getSvcMgrNFSLock ()
{
	typeset DB=$1

	# clusvcmgrd%nfs_lock_workaround
	$GETCONFIG -f $DB $SVCMGR_STR$SEP$SVCMGR_NFS_LOCK
	return $?
}


#
# Given a service ID get its associated service name from the given database.
#
getSvcName ()
{
	typeset DB=$1
	typeset svcID=$2

	# services%service0%name
	$GETCONFIG -f $DB \
	$SVC_SERVICES_LIST_STR$SEP$SVC_SERVICE_STR$svcID$SEP$SVC_NAME_STR
	return $?
}


#
# Retrievs the export client.
#
getTokenList()
{
        typeset DB=$1
	typeset tokenregex=$2
	typeset chop=$3

        tlist=$(/usr/sbin/cludb -f $DB -m "^$tokenregex$SEP$chop\$" | \
		awk '{if(($3!="")&&($3!~/^[Nn][Oo][Nn][Ee]$/)){print $1}}' | \
        	sed -e "s/$SEP$chop\$//i" )
        rv=$?
        [ -n "$tlist" ] || return $NOT_FOUND
        echo $tlist
        return $rv
}


#
# Given a service ID get its associated user script from the given database.
#
getSvcUserScript ()
{
	typeset DB=$1
	typeset svcID=$2
	typeset N=$3

	# services%service0%userScript
	$GETCONFIG -f $DB \
	$SVC_SERVICES_LIST_STR$SEP$SVC_SERVICE_STR$svcID$SEP$SVC_USER_SCRIPT_STR
	return $?
}

#
# Get all ip address tokens for a service
#
getSvcIPaddressTokenList()
{
	typeset DB=$1
	typeset svcID=$2
	typeset rv=0
	typeset tlist=""

	# services%service0%service_ipaddresses%...[0-9]+%ipAddress
	tlist=$(getTokenList $DB \
	        "$SVC_SERVICES_LIST_STR$SEP$SVC_SERVICE_STR$svcID$SEP$SVC_NETWORK_LIST_STR$SEP$SVC_NETWORK_STR[0-9]+" \
		"$SVC_IP_ADDR_STR")
        rv=$?
        [ -n "$tlist" ] || return $NOT_FOUND
        echo $tlist
        return $rv
}

#
# Given a service ID get the Nth IP address from the given database.
#
getSvcIPaddress ()
{
	typeset DB=$1
	typeset token=$2

	# services%service0%network0 + %ipAddress
	$GETCONFIG -f $DB \
		"$token$SEP$SVC_IP_ADDR_STR"
	return $?
}


#
# Given a token, get our link-monitor config opt from the given database.
#
getSvcIPmonitorlink ()
{
	typeset DB=$1
	typeset token=$2

	# services%service0%network0 + %ipAddress
	$GETCONFIG -f $DB \
		"$token$SEP$SVC_LINKMON_STR"
	return $?
}

#
# Given a service ID get the Nth IP address from the given database.
#
getSvcNetmask ()
{
	typeset DB=$1
	typeset token=$2

	# services%service0%network0%netmask
	$GETCONFIG -f $DB \
		"$token$SEP$SVC_NETMASK_STR"
	return $?
}

#
# Given a service ID get the Nth IP address from the given database.
#
getSvcBroadcast ()
{
	typeset DB=$1
	typeset	token=$2

	# services%service0%network0%broadcast
	$GETCONFIG -f $DB \
		"$token$SEP$SVC_BROADCAST_STR"
	return $?
}

#
# Get all device tokens for a service
#
getSvcDeviceTokenList()
{
	typeset DB=$1
	typeset svcID=$2
	typeset rv=0
	typeset tlist=""
	typeset tmp=""

	# services%service0%device0%name
	tmp=$(getTokenList $DB \
		"$SVC_SERVICES_LIST_STR$SEP$SVC_SERVICE_STR$svcID$SEP$SVC_DEVICE_STR[0-9]+" \
		"$SVC_DEVICE_NAME_STR")
        rv=$?

	#
	# Enforce by-id mounting of file systems.
	#

	# Add extra 0 before sorting
	tlist=$(echo $tmp | sed -e 's/\(device\)\([0-9]\)\(\s\|$\)/\10\2\3/g')

	# Sort 
	tlist=$(sort < <(for t in $tlist; do echo $t; done))

	# Remove extra 0
	tlist=$(echo $tlist | sed -e 's/\(device0\)\([0-9]\)\(\s\|$\)/device\2\3/g')
	
        [ -n "$tlist" ] || return $NOT_FOUND
        echo $tlist
        return $rv
}


#
# Get all device tokens for a service in reverse order
#
getSvcDeviceTokenList_r()
{
	typeset DB=$1
	typeset svcID=$2
	typeset rv=0
	typeset tlist=""
	typeset tmp=""

	# services%service0%device0%name
	tmp=$(getTokenList $DB \
		"$SVC_SERVICES_LIST_STR$SEP$SVC_SERVICE_STR$svcID$SEP$SVC_DEVICE_STR[0-9]+" \
		"$SVC_DEVICE_NAME_STR")
        rv=$?

	#
	# Enforce by-id unmounting of file systems.
	#
	# Add extra 0 before sorting
	tlist=$(echo $tmp | sed -e 's/\(device\)\([0-9]\)\(\s\|$\)/\10\2\3/g')

	# Sort 
	tlist=$(sort -r < <(for t in $tlist; do echo $t; done))

	# Remove extra 0
	tlist=$(echo $tlist | sed -e 's/\(device0\)\([0-9]\)\(\s\|$\)/device\2\3/g')
	
        [ -n "$tlist" ] || return $NOT_FOUND
        echo $tlist
        return $rv
}


#
# Given a service ID get the Nth device name from the given database.
#
# lhh - added use of /sbin/findfs to determine file system device.
#
getSvcDevice()
{
	typeset DB=$1
	typeset token=$2
	typeset rv=0
	typeset dev=""

	# services%service0%device0%name
	dev=$($GETCONFIG -f $DB \
		"$token$SEP$SVC_DEVICE_NAME_STR")
	rv=$?

	if [ $rv -eq 0 ]; then
		if echo $dev | grep -q "LABEL="; then
			dev=`/sbin/findfs $dev`
			rv=$?
		fi
	fi

	if [ -n "$dev" ]; then
		echo $dev
	fi
	return $rv
}

#
# Given a service ID get the Nth mount point from the given database.
#
getSvcMountPoint()
{
	typeset DB=$1
	typeset token=$2

	# services%service0%device0 + %mount%name
	$GETCONFIG -f $DB \
		"$token$SEP$SVC_MOUNT_STR$SEP$SVC_MOUNT_NAME_STR"
	return $?
}

#
# Given a service ID get the Nth mount point fstype from the given database.
#
getSvcMountFstype ()
{
	typeset DB=$1
	typeset token=$2

	# services%service0%device0 + %mount%fstype
	$GETCONFIG -f $DB \
		"$token$SEP$SVC_MOUNT_STR$SEP$SVC_MOUNT_FSTYPE_STR"
	return $?
}

#
# Get user quota 
#
getSvcMountUserQuota()
{
	typeset DB=$1
	typeset token=$2

	# services%service0%device0 + %mount%user_quota
	$GETCONFIG -f $DB \
		"$token$SEP$SVC_MOUNT_STR$SEP$SVC_MOUNT_UQUOTA_STR"
	return $?
}

#
# Get group quota 
#
getSvcMountGroupQuota()
{
	typeset DB=$1
	typeset token=$2

	# services%service0%device0 + %mount%group_quota
	$GETCONFIG -f $DB \
		"$token$SEP$SVC_MOUNT_STR$SEP$SVC_MOUNT_GQUOTA_STR"
	return $?
}

#
# Given a service ID get the Nth mount point options from the given database.
#
getSvcMountOptions ()
{
	typeset DB=$1
	typeset token=$2

	# services%service0%device0 + %mount%options
	$GETCONFIG -f $DB \
		"$token$SEP$SVC_MOUNT_STR$SEP$SVC_MOUNT_OPTIONS_STR"
	return $?
}

#
# Given a service ID get the Nth forced unmount setting from the given database.
#
getSvcForceUnmount ()
{
	typeset DB=$1
	typeset token=$2

	# services%service0%device0 + %mount%force_unmount
	$GETCONFIG -f $DB \
		"$token$SEP$SVC_MOUNT_STR$SEP$SVC_FORCE_UNMOUNT_STR"
	return $?
}


#
# Retrievs the export directory for a given mount.
#
getSvcExportDirTokenList()
{
	typeset DB=$1
	typeset token=$2
	typeset rv=0
	typeset tlist=""

	# services%service0%device0 + %nfsexport0%name
	tlist=$(getTokenList $DB \
		"$token$SEP$SVC_EXPORT_DIR_STR[0-9]+" \
		"$SVC_EXPORT_NAME_STR")
        rv=$?
        [ -n "$tlist" ] || return $NOT_FOUND
        echo $tlist
        return $rv
}


#
# Retrievs the export directory for a given mount.
#
getSvcExportDir()
{
	typeset DB=$1
	typeset token=$2

	# services%service0%device0%nfsexport0 + %name
	$GETCONFIG -f $DB \
		$token$SEP$SVC_EXPORT_NAME_STR
	return $?
}


#
# Retrievs the export client.
#
getSvcExportClient()
{
	typeset DB=$1
	typeset token=$2

	# services%service0%device0%nfsexport0%client0 + %name
	$GETCONFIG -f $DB \
		$token$SEP$SVC_EXPORT_CLIENT_NAME_STR
	return $?
}

#
# Retrievs the export client.
#
getSvcExportOptions()
{
	typeset DB=$1
	typeset token=$2

	# services%service0%device0%nfsexport0%client0 + %options
	$GETCONFIG -f $DB \
		$token$SEP$SVC_EXPORT_CLIENT_OPTIONS_STR
	return $?
}

#
# Retrievs the export client.
#
getSvcExportClientTokenList()
{
        typeset DB=$1
	typeset token=$2
        typeset tlist=""
        typeset -i rv=0
                                                                                 
        # services%service0%device0%nfsexport0 + %client[0-9]+%name
        tlist=$(getTokenList $DB \
		"$token$SEP$SVC_EXPORT_CLIENT_STR[0-9]+" \
		"$SVC_EXPORT_CLIENT_NAME_STR")
        rv=$?
        [ -n "$tlist" ] || return $NOT_FOUND
        echo $tlist
        return $rv
}


#
# Given a service ID get the Nth mount point fstype from the given database.
#
getSvcShareName ()
{
	typeset DB=$1
	typeset token=$2

	# services%service0%device0 + %sharename
	$GETCONFIG -f $DB \
		"$token$SEP$SVC_DEVICE_SHARE_NAME"
	return $?
}
