#!/bin/sh
#
# gandi-mount     Start/Stop the Gandi mounting script.
#
### rpm based distribution
# chkconfig: - 11 89
# description: initscript which mount additional drive Gandi hosting.
# processname: gandi-mount
# config: /etc/sysconfig/gandi
# pidfile: /var/run/gandi-mount.pid
#
### BEGIN INIT INFO
# Provides:             gandi-mount
# Required-Start:       $local_fs udev
# Required-Stop:        $remote_fs
# Default-Start:        S
# Default-Stop:         0 1 6
# X-Start-Before:       
# X-Start-After:        $local_fs udev
# Short-Description:    Gandi script to mount attached disk to the system.
# Description:          Gandi-mount use udev command to mount the additional
#                       disk attached to the system to allow user to find
#                       their data.
#                       The script is designed to delay the boot process until
#                       the disk are fully available and mounted. To avoid
#                       starting application without the data.
### END INIT INFO

# Author: Nicolas Chipaux <aegiap@gandi.net>

# "-e" option is not welcome here.

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="mounting additional disk"
NAME=gandi-mount
CONFIG_ALLOW_MOUNT=1
DEFAULTSFILE=/etc/default/gandi

# Mount attached datadisk to the filesystem tree
[ -r $DEFAULTSFILE ] && . $DEFAULTSFILE

# Some distribution does not use LSB by default. We provide some simple 
# function to do the job and let the LSB functions overrides them if
# present
log_success_msg() {
            echo -e "\t\t\t [$1]"
}

log_failure_msg() {
            echo -e "\t\t\t [$1]"
}

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
[ -f /lib/lsb/init-functions ] && . /lib/lsb/init-functions

log_gandi_msg() {
        [ $1 = 0 ] && log_success_msg "ok" || log_failure_msg "fail"
}

retval=1

# restarting udev detection of disk using the available command 
# first, we try udevadm for newer system and then udevtrigger 
# in its both binary version
udev_call() {
    udevcmd="$(which udevadm 2> /dev/null)"
    if [ -n "$udevcmd" -a -f "$udevcmd" ]; then
        "$udevcmd" trigger --subsystem-match="block"
        retval=$?
	"$udevcmd" settle
    else
        udevcmd="$(which udevtrigger 2> /dev/null)"
        if [ -n "$udevcmd" -a -f "$udevcmd" ]; then
            if "$udevcmd" --help 2>&1 | grep -q -o subsystem-match; then
                "$udevcmd" --subsystem-match="block"
                retval=$?
            else
                "$udevcmd" > /dev/null
                retval=$?
            fi
        fi
        udevcmd="$(which udevsettle 2> /dev/null)"
        if [ -n "$udevcmd" -a -f "$udevcmd" ]; then
            "$udevcmd" > /dev/null
            retval=$?
        fi
    fi

    return $retval
}

case "$1" in
  start)
    if [ "o$CONFIG_ALLOW_MOUNT" = "o0"  ]; then
        echo -n "$NAME disabled in conf: "
        retval=$?
    else
        echo -n "Starting $NAME: "
        udev_call
    fi
    log_gandi_msg $retval
    ;;
  stop|status)
    # nothing to do
    ;;
  restart|force-reload)
    echo -n "Restarting $NAME: "
    udev_call
    log_gandi_msg $retval
    ;;
  *)
    echo "Usage : $NAME {start|restart|force-reload}" >&2
    exit 3
    ;;

esac

exit 0
