#!/bin/sh
#
. /opt/HAC/bin/rsf.sh

service=${RSF_SERVICE:-"service_name"}
script="`basename $0`"
mdir="${PRODUCT_ETC}/mounts/"

#
# $1 - operation [mount|unmount]
check_for_mounts()
{
    #
    # If the mounts directory exists then process it.
    # Each file contained within is named <pool>.<fs type>.
    # Each file is then processed by extracting the FS type (lowercase it first)
    # then pass to do_operation to read the contents and mount the file systems.
    # do_operation does both mount and unmount.
    if [ -d ${mdir} ] ; then
	for f in `ls ${mdir}${service}.*|sed 's^"${mdir}"^^'`
	do
	    TYP=`echo $f | awk -F. '{print $2}' | tr "[:upper:]" "[:lower:]"`
	    MFILE="${mdir}${service}.${TYP}"
	    do_operation ${1} ${MFILE} ${TYP}
	done
    fi
}

#
# $1 - operation [mount|unmount]
# $2 - filesystems path
# $3 - FS type
do_operation()
{
    OPERATION=$1
    FSP=$2
    TYPE=$3

    if [ -f ${FSP} ] ; then
	while IFS=: read vol mp options; do
	    if [ "${OPERATION}" = "mount" ] ; then
		mkdir -p ${mp}
		if [ -z "${options}" ] ; then
		    dated_echo "Mounting ${vol} of type ${TYPE} on ${mp}"
		    mount -t ${TYPE} ${vol} ${mp}
		else
		    dated_echo "Mounting ${vol} of type ${TYPE} on ${mp} with options: ${options}"
		    mount -t ${TYPE} -o ${options} ${vol} ${mp}
		fi
	    else
		dated_echo "Unmounting ${vol} from ${mp}"
		umount -f ${mp}
		if [ $? != 0 ] ; then
		    dated_echo "Warning: non zero exit code from umount of ${mp} - $?"
		fi
	    fi
	done < ${FSP}
    fi
}

case "${1}" in

'start')
    notify_watchers "${LOG_INFO}" "RSF_SERVICE_STARTUP" "${service} importing additional filesystems"
    check_for_mounts mount
    rc_exit ${service} ${RSF_OK}
    ;;

'stop')
    notify_watchers "${LOG_INFO}" "RSF_SERVICE_SHUTDOWN" "${service} exporting additional filesystems"
    check_for_mounts unmount
    rc_exit ${service} ${RSF_OK}
    ;;

'check')
	exit ${RSF_CHECK_NORESRC}
	;;

*)
	rc_exit ${service} ${RSF_WARN} "usage: $0 <start|stop|check>"
	;;

esac
