#!/bin/bash
# start/stop script for mount NFS filesystems
#
# Startup script template for s2s simple service: mount via nfs - check for local mount before
# start  checks for local mount and does an nfs mount only if not already locally mounted.
# status checks for mounted anyway
# stop   checks for local mount and only umounts an nfs mount
#
# Environment variable(s):
#     CONFIG_FILE=/etc/s2s/sysconfig/$this[.$HOSTNAME]
#         use this variable to read local configuration
#
# (c) 2007..2007 Coware AG, Munich
#
# 2008-Aug-25 ahu  added $mountcmd for Solaris "mount -F"
# 2008-Apr-30 aka  status: changed to "checking for..."
# 2007-Jul-31 aka  initial version
#
# Note on runlevels:
# 0 - halt/poweroff 			6 - reboot
# 1 - single user			2 - multiuser without network exported
# 3 - multiuser w/ network (text mode)  5 - multiuser w/ network and X11 (xdm)
# 
# Return values acc. to LSB for all commands but status:
# 0	  - success
# 1       - generic or unspecified error
# 2       - invalid or excess argument(s)
# 3       - unimplemented feature (e.g. "reload")
# 4       - user had insufficient privileges
# 5       - program is not installed
# 6       - program is not configured
# 7       - program is not running
# 8--199  - reserved (8--99 LSB, 100--149 distrib, 150--199 appl)
# 
# Return value is slightly different for the status command:
# 0 	  - service up and running
# 1 	  - service dead, but /var/run/  pid  file exists
# 2 	  - service dead, but /var/lock/ lock file exists
# 3 	  - service not running (unused)
# 4 	  - service status unknown :-(
# 6       - program is not configured (NOT LSB: aka, Coware AG)
# 5--199  - reserved (5--99 LSB, 100--149 distro, 150--199 appl.)
#
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signaling is not supported) are
# considered a success.

progname="${0##*/}"					# cut path
progname=${progname#rc}
progname=${progname#[KS][0-9][0-9]}
progname="${progname%%.*}"				# cut filetype
test "$UID" -ne 0  && { echo >&2 "$progname: You must be root."; exit 1; }

action="$1"						# save action
ec=0							# default: success
 ret_other=(success error)				# return values
ret_status=(running dead_but_run dead_but_lock unused unknown reserved5 not_configured)
 ret_start=(started error inv_args not_implemented insuff_privileges not_installed not_configured not_running)
  ret_stop=(stopped error inv_args not_implemented insuff_privileges not_installed not_configured not_running)
rc_status () {						# inform user
    local s
    case "$action" in
	start)  s=${ret_start[$ec]};;
	stop)   s=${ret_stop[$ec]};;
	status) s=${ret_status[$ec]};;
        *)      test "$ec" = 0 && s=${ret_other[0]} || s=${ret_other[1]};;
    esac
    echo ": ${s:-(unknown exit code $ec)}"
}

# read configuration
ConfErr () { echo >&2 "$progname: $*"; exit 6; }
cfglist=""                                              # collect tried pathes, output on error for debugging
ChkCfgfile () {                                         # first try /etc/sysconfig then /etc/s2s/sysconfig
    local cf="$1"; test -z "$cf" && ConfErr "$FUNCNAME(): filename missing."
    CONFIG_FILE=/etc/sysconfig/$cf;     cfglist="$cfglist $CONFIG_FILE"; test -r "$CONFIG_FILE" && return 0
    CONFIG_FILE=/etc/s2s/sysconfig/$cf; cfglist="$cfglist $CONFIG_FILE"; test -r "$CONFIG_FILE" && return 0
    return 1                                            # failed
}
test -z "$CONFIG_FILE" && {                             # if no config file defined by environment, look for
    ChkCfgfile $progname.$HOSTNAME || {                 #     host specific one or
        ChkCfgfile $progname || {                       #     general one or error
            ConfErr "configuration missing, searched: '${cfglist# }'."; }; }
}                                                       # endif
test -s "$CONFIG_FILE" && . $CONFIG_FILE                # read config
# check for mandatory config vars
test -z "$MNTNFS_HOST"   && ConfErr "var 'MNTNFS_HOST' not configured."
test -z "$MNTNFS_SHARE"  && ConfErr "var 'MNTNFS_SHARE' not configured."
test -z "$MNTNFS_DEVICE" && ConfErr "var 'MNTNFS_DEVICE' not configured."
test -z "$MNTNFS_POINT"  && ConfErr "var 'MNTNFS_POINT' not configured."
test -n "${MNTNFS_POINT##/*}" && ConfErr "mount point '$MNTNFS_POINT' must start with a '/'."

test -n "$MNTNFS_OPTIONS" && MNTNFS_OPTIONS="-o $MNTNFS_OPTIONS"

sys_os="`uname`"
case "$sys_os" in
    SunOS)
        mounted () { mount | sed 's| on .*||' | grep "^$MNTNFS_POINT$" >&/dev/null; }
        loc_mnt () { mount | sed 's| on [^:]* .*||' | grep "^$MNTNFS_POINT$" >&/dev/null; }
        mountcmd="mount -F nfs"
	;;
    Linux)
        mounted () { mount | sed 's|^.* on \(.*\) type .*$|\1|' | grep "^$MNTNFS_POINT$" >&/dev/null; }
        loc_mnt () { mount | sed 's|^[^:]* on \(.*\) type .*$|\1|' | grep "^$MNTNFS_POINT$" >&/dev/null; }
        mountcmd="mount -t nfs"
	;;
    Darwin)						# MacOSX
        mounted () { mount | sed 's|^.* on \(.*\) (.*$|\1|' | grep "^$MNTNFS_POINT$" >&/dev/null; }
        loc_mnt () { mount | sed 's|^[^:]* on \(.*\) (.*$|\1|' | grep "^$MNTNFS_POINT$" >&/dev/null; }
        mountcmd="mount -t nfs"
	;;
    ForDocumentationOnly)
	# mounted() returns 0 if mount is active, != 0 else
	;;
    *)
        echo >&2 "Unknown operating system '$sys_os'."; exit 6;;
esac

# Check prerequisites, environment, binaries - avoid blocking, dont check if mounted
mounted || { test ! -d "$MNTNFS_POINT" && ConfErr "mount point '$MNTNFS_POINT' missing or not a dir."; }

# handle action
case "$action" in
    start)
	echo -n "    Starting $progname '$MNTNFS_POINT'"
	mounted || $mountcmd $MNTNFS_OPTIONS "$MNTNFS_DEVICE" "$MNTNFS_POINT" >&/dev/null || ec=1
	rc_status
	;;
    stop)
	echo -n "    Shutting down $progname '$MNTNFS_POINT'"
	# ..., set $ec
	mounted && ! loc_mnt && { umount "$MNTNFS_POINT" >&/dev/null || ec=1; }
	rc_status
	;;
    restart)
	## Stop the service and regardless of whether it was
	## running or not, start it again.
	$0 stop; $0 start; ec=$?
	;;
    status)
	echo -n "    Checking for $progname '$MNTNFS_POINT'"
	# ..., set $ec, typical 0 (running) or 3 (unused)
	mounted && ec=0 || ec=3
	rc_status
	;;
    *)
	echo "Usage: $0 {start|stop|status|restart}"
	ec=1
	;;
esac
exit $ec

# Local Variables:
# mode:                 shell-script
# mode:                 font-lock
# comment-column:       56
# tab-width:            8
# End:
