#!/bin/sh
#
#
# Written by Leo Eraly <leo@unstable.be>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#
# This RA can be used in a Novell Linux environment if you have NCP volumes
# on top of traditional Linux filesystems (e.g ext3,reiserfs) and you want to make
# them high-available without the use of Novell Cluster Services.
# ! This is not intented to be used with NCP volumes on top of NSS !
#
# The NCP server also requires a virtual IP (for its resource) so next to this script
# you'll also need to configure an IPaddr resource which has a dependency on this resource/script
#######################################################################
# Initialization:

. /usr/lib/heartbeat/ocf-shellfuncs
#. ${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs

#######################################################################
HOSTOS=`uname`
MOUNT=/bin/mount

usage() {
    echo "usage: $0 {start|stop|status|monitor|validate-all|meta-data}"
    return $1
}


meta_data() {
    cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="NcpMount">
<version>1.0</version>

<longdesc lang="en">
Resource script for NCP Volumes. It manages an NCP volume an a shared medium. 
</longdesc>
<shortdesc lang="en">NCP volume resource agent</shortdesc>

<parameters>
<parameter name="directory" required="1">
<longdesc lang="en">
The mount point for the filesystem.
</longdesc>
<shortdesc lang="en">mount point</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="ip" unique="1" required="1">
<longdesc lang="en">
The IPv4 address for the cluster resource
"192.168.1.1".
</longdesc>
<shortdesc lang="en">IPv4 address</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="ncpvolume" required="1">
<longdesc lang="en">
The name of the (virtual) NCP volume to be mounted
</longdesc>
<shortdesc lang="en">NCP volume name</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="ncpserver" required="1">
<longdesc lang="en">
The name of the (virtual) NCP server
</longdesc>
<shortdesc lang="en">NCP server name</shortdesc>
<content type="string" default="" />
</parameter>
</parameters>

<actions>
<action name="start" timeout="60" />
<action name="stop" timeout="60" />
<action name="notify" timeout="60" />
<action name="monitor" depth="0" timeout="40" interval="20" start-delay="10" />
<action name="validate-all" timeout="5" />
<action name="meta-data" timeout="5" />
</actions>
</resource-agent>
END
    exit $OCF_SUCCESS
}

##################################################################
ncp_usage() {
    cat <<END
usage: $0 {start|stop|status|monitor|validate-all|meta-data}

Expects to have a fully populated OCF RA-compliant environment set.
END
}

list_mounts() {
    if [ -f "/etc/mtab" -a -r "/etc/mtab" ]; then
        cut -d' ' -f1,2,3 </etc/mtab
    else
        $MOUNT | cut -d' ' -f1,3,5
    fi
}

ncp_validate() {
    # ncpcon must be available
    #check_binary $NCPCON

    # does the mountpoint exist
    if [ -n $MOUNTPOINT -a ! -d $MOUNTPOINT ]; then
        ocf_log warn "Mountpoint $MOUNTPOINT does not exist"
    fi
}

ncp_status() {
    #check is volume is mounted by ncpserv
    if `$NCPCON volumes | grep -q "$NCP_VOLUME"`
    then
        rc=$OCF_SUCCESS
        msg="$NCP_VOLUME is mounted under ncp"
    else
        rc=$OCF_NOT_RUNNING
        msg="$NCP_VOLUME is not mounted under ncp"
    fi
 
    return $rc
}

ncp_start() {
    # was/is it still mounted ?
    if ncp_status > /dev/null 2>&1 ; then
        ocf_log info "Filesystem $MOUNTPOINT is already mounted."
        return $OCF_SUCCESS
    fi

    #ncpcon mount
    $NCPCON mount "$NCP_VOLUME,PATH=$MOUNTPOINT"
    if [ $? -ne 0 ]; then
        ocf_log err "Couldn't mount volume $NCP_VOLUME on $MOUNTPOINT"
        return $OCF_ERR_GENERIC
    fi

    #ncpcon bind
    $NCPCON bind --ncpservername=$NCP_SERVER --ipaddress=$RESOURCE_IP
    if [ $? -ne 0 ]; then
        ocf_log err "Couldn't bind $NCP_SERVER with ip $RESOURCE_IP"
        return $OCF_ERR_GENERIC
    fi

    return $OCF_SUCCESS
}

ncp_stop() {
    ncp_status >/dev/null 2>&1
	if [ $? -eq $OCF_NOT_RUNNING ]; then
		# Already unmounted, wonderful.
		return $OCF_SUCCESS
	else
		# ncpcon unbind
		$NCPCON unbind --ncpservername=$NCP_SERVER --ipaddress=$RESOURCE_IP
		if [ $? -ne 0 ]; then
        		ocf_log err "Couldn't unbind $NCP_SERVER with ip $RESOURCE_IP"
	        	return $OCF_ERR_GENERIC
		fi
        
		# unmount volume
        	$NCPCON dismount $NCP_VOLUME
		if [ $? -ne 0 ]; then
        		ocf_log err "Couldn't umount $NCP_VOLUME"
	        	return $OCF_ERR_GENERIC
		fi

    fi
    
    return $OCF_SUCCESS
}

#ocf_log debug "$0 started with arguments \"$@\""

NCPBASE=/opt/novell/ncpserv
NCPCON=$NCPBASE/sbin/ncpcon
MOUNTPOINT=$(echo $OCF_RESKEY_directory | sed 's/\/*$//')
: ${MOUNTPOINT:=/}
NCP_VOLUME=$OCF_RESKEY_ncpvolume
NCP_SERVER=$OCF_RESKEY_ncpserver
RESOURCE_IP=$OCF_RESKEY_ip

case $__OCF_ACTION in
meta-data)  meta_data
        ;;
start)  ncp_start
        ;;
stop)   ncp_stop 
        ;;
status|monitor) ncp_status
        exit $?
        ;;
validate-all)   ncp_validate
        ;;
usage|help) ncp_usage
        exit $OCF_SUCCESS
        ;;
*)      ncp_usage
        exit $OCF_ERR_UNIMPLEMENTED
        ;;
esac

exit $?

