#!/usr/bin/bash
VSTORAGE_NFS_LIB=/usr/libexec/vstorage-nfs/nfs_functions
ROOT=""
SHARE_NAME=""

ARGS="${@}"

log=/var/log/vstorage/vstorage-nfsd.log

source $VSTORAGE_NFS_LIB

[ -f $NFS_ETC/config ] && source $NFS_ETC/config

pcs_ha_init_host >>$log 2>&1

function for_each_share {
	local action=$1
	local retval=0

        host_id=`get_host_id`
        [ $? -ne 0 ] && return 1

	ls $NFS_ROOT/shares 2>/dev/null | while read share
	do
		[ ! -d  "$NFS_ROOT/shares/$share/control" ] && continue

		pcs_ha_is_registered_local $share $host_id >/dev/null 2>&1
		rc=$?
		if [ $rc -eq 0 ] ; then
			pcs_ha_lock_exec $action $share
			[ $? -ne 0 ] && retval=1
		else
			[ $rc -ne 1 ] && retval=$rc
		fi
	done

	return $retval
}

function nfs_start_share {
	local share="$1"

	if [ -z "$share" ]; then
		echo "Share name was not specified" >>$log
		return 1
	fi

	if [ ! -f "$NFS_ROOT/shares/$share/control/.auto" ] ; then
		echo "Autostart disabled for share '$share', skip it" >>$log
		return 2
	fi

	# do start share
	echo "Starting share '$share'" >>$log
	pcs_nfs_start_share $share 1 >>$log 2>&1
	if [ $? -ne 0 ]; then
		echo "Unable to start share '$share'" >>$log
		return 3
	fi

	echo "Share $share was started succesfully" >>$log

        return 0
}

function nfs_stop_share {
	local share="$1"

	if [ -z "$share" ]; then
		echo "Share name was not specified" >>$log
		return 1
	fi

	# do stop share
	echo "Stopping share '$share'" >>$log
	pcs_nfs_stop_share $share  >>$log 2>&1
	if [ $? -ne 0 ]; then
		echo "Unable stop share '$share'" 2>&1
		return 2
	fi

	echo "Share $share was stopped succesfully" >>$log

        return 0
}

function nfs_fence_host {
	echo "Fencing host" >>$log
	pcs_ha_fence_host >>$log 2>&1
	if [ $? -ne 0 ]; then
		echo "Failed to fence host" >>$log
		return 1
	fi
	echo "Host is succesfully fenced" >>$log
	return 0
}

function nfs_add_share_address {
	local share="$1"

	if [ -z "$share" ]; then
		echo "Share name was not specified" >>$log
		return 1
	fi

	echo "Unfencing share '$share'" >>$log

	local status=`pcs_nfs_get_status $share`
	if [ "$status" != "running" ]; then
		echo "Share '$share' is not running, skip it" >>$log
		return 0
	fi

	local addr=`cat "$NFS_ROOT/shares/$share/control/address" 2>>$log`
        if [ -z "$addr" ] ; then
                echo "Unable to read address for share '$share'" >>$log
                return 2
        fi

	pcs_ha_add_addr $addr >>$log 2>&1
	if [ $? -ne 0 ] ; then
		echo "Failed to add address '$addr'" >>$log
		return 3
	fi

	echo "Share '$share' was unfenced successfully" >>$log
	return 0
}

function nfs_unfence_host {
	echo "Unfencing host" >>$log
	for_each_share nfs_add_share_address
	if [ $? -ne 0 ]; then
		echo "Failed to unfence host" >>$log
		return 1
	fi
	echo "Host is succesfully unfenced" >>$log
	return 0
}

if [ ! -x $VSTORAGE_NFS_LIB ] ; then
        echo "Unable find executable $VSTORAGE_NFS_LIB" 1>&2
        exit 1
fi

CMD="${1}"
shift
ARGS="${@}"

if [ -z "$NFS_ROOT" ]; then
	echo "Unable to find directory with NFS shares, please check 'NFS_ROOT' in $NFS_ETC/config" >>$log
	exit 1
fi

case "${CMD}" in
  start)
	pcs_nfs_bind_state
	pcs_nfs_copy_ds_list
	pcs_nfs_start_ganesha
	pcs_nfs_check_ganesha
	for_each_share nfs_start_share
	exit $?
	;;
  stop)
	for_each_share nfs_stop_share
	ganesha_pid=$(cat $NFS_GANESHA_PID)
	pcs_nfs_stop_ganesha
	pcs_nfs_unbind_state
	pcs_nfs_delete_ds_list
	pcs_nfs_wait_until_ganesha_stopped "$ganesha_pid"
	exit $?
	;;
  fence)
	nfs_fence_host
	exit $?
	;;
  unfence)
	nfs_unfence_host
	exit $?
	;;
  *)
	echo $"Usage: $0 {start|stop}"
	exit 2
esac
