#!/bin/bash
# start/stop script for mount local filesystems
#
# Environment variable(s):
#     CONFIG_FILE=/etc/s2s/sysconfig/$this[.$HOSTNAME]
#         use this variable to read local configuration
#
# (c) 2007..2009 Censhare AG, Munich
#
# 2009-Apr-30 pju  Check for mount points instead of devices on Linux,
#                  making sure this script is working for device names like
#                  /dev/disk/by-id/scsi-[0-9a-z]+-part1 
#                  which are symlinks to the actual device
# 2008-Apr-30 aka  status: changed to "checking for..."
# 2007-Feb-26 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; }
test -n "$CONFIG_FILE" -a -s "$CONFIG_FILE" && {
    . $CONFIG_FILE
}
# check for mandatory config vars
test -z "$MOUNT_DEVICE" && ConfErr "var 'MOUNT_DEVICE' not configured."
test -z "$MOUNT_POINT"  && ConfErr "var 'MOUNT_POINT' not configured."
test -n "${MOUNT_POINT##/*}" && ConfErr "mount point '$MOUNT_POINT' must start with a '/'."

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

# Check prerequisites, environment, binaries
test ! -d "$MOUNT_POINT" && ConfErr "mount point '$MOUNT_POINT' missing or not a dir."

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

# handle action
case "$action" in
    start)
	echo -n "    Starting $progname '$MOUNT_POINT'"
	mounted || mount $MOUNT_FS $MOUNT_OPTIONS $MOUNT_DEVICE $MOUNT_POINT >&/dev/null || ec=1
	rc_status
	;;
    stop)
	echo -n "    Shutting down $progname '$MOUNT_POINT'"
	# ..., set $ec
	mounted && { umount $MOUNT_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 '$MOUNT_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:
