#!/bin/bash
#
# $Id: rc.autofs.in,v 1.63 2006/03/30 02:09:51 raven Exp $
#
# rc file for automount using a Sun-style "master map".
#
# chkconfig: 345 28 72
# processname: /usr/sbin/automount
# config: /etc/auto.master
# description: Automounts filesystems on demand

#
# Location of the automount daemon and the init directory
#
DAEMON=/usr/sbin/automount
prog=`basename $DAEMON`
MODULE="autofs4"
DEVICE="autofs"
initdir=/etc/init.d
confdir=/etc/sysconfig

test -e $DAEMON || exit 0

if [ -r $initdir/functions ]; then
    . $initdir/functions
fi

PATH=/sbin:/usr/sbin:/bin:/usr/bin
export PATH

#
# load customized configuation settings
#
if [ -r $confdir/autofs ]; then
    . $confdir/autofs
fi

function start() {
    # Make sure autofs4 module is loaded
    if ! grep -q autofs /proc/filesystems
    then
        echo -n "Loading $MODULE: "
        # Try load the autofs4 module fail if we can't
        modprobe $MODULE >/dev/null 2>&1
        RETVAL=$?
        if [ $RETVAL -eq 1 ]
        then
            failure "Load $MODULE"
            echo
            return $RETVAL
        else
            success "Load $MODULE"
            echo
        fi
    elif ([ -f /proc/modules ] && lsmod) | grep -q autofs[^4]
    then
        RETVAL=1
        failure "Load $MODULE"
        echo
        return $RETVAL
    fi

    # Check misc device
    if [ -n "$USE_MISC_DEVICE" -a "x$USE_MISC_DEVICE" = "xyes" ]; then
        sleep 1
        if [ -e "/proc/misc" ]; then
            MINOR=`awk "/$DEVICE/ {print \\$1}" /proc/misc`
            if [ -n "$MINOR" -a ! -c "/dev/$DEVICE" ]; then
                mknod -m 0600 /dev/$DEVICE c 10 $MINOR
            fi
        fi
        if [ -x /sbin/restorecon -a -c /dev/$DEVICE ]; then
            /sbin/restorecon /dev/$DEVICE
        fi
    else
        if [ -c /dev/$DEVICE ]; then
            rm /dev/$DEVICE
        fi
    fi

    echo -n $"Starting $prog: "
    $prog $OPTIONS 
    RETVAL=$?
    if [ $RETVAL -eq 0 ] ; then
        success "$prog startup"
    else
        failure "$prog startup"
    fi
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/autofs
    echo
    return $RETVAL
}

function stop() {
    echo -n $"Stopping $prog: "
    count=0
    while [ -n "`pidof $prog`" -a $count -lt 15 ] ; do
        killproc $prog -TERM >& /dev/null
        RETVAL=$?
        [ $RETVAL = 0 -a -z "`pidof $prog`" ] || sleep 3
        count=`expr $count + 1`
    done
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/autofs
    if [ -n "`pidof $prog`" ] ; then
        failure "$prog shutdown"
    else
        success "$prog shutdown"
    fi
    echo
    return $RETVAL
}

function restart() {
    stop
    start
}

function reload() {
    if [ ! -f /var/lock/subsys/autofs ]; then
        echo $"$prog not running"
        RETVAL=1
        return $RETVAL
    fi
    pid=`pidof $prog`
    if [ -z $pid ]; then
        echo $"$prog not running"
        RETVAL=1
    else
        kill -HUP $pid 2> /dev/null
        echo $"Reloading maps"
        RETVAL=0
    fi
    return $RETVAL
}

RETVAL=0

case "$1" in
    start)
        start
        ;;
    forcestart)
        OPTIONS="$OPTIONS --force"
        start
        ;;
    stop)
        stop
        ;;
    status)
        status $prog
        ;;
    restart)
        restart
        ;;
    forcerestart)
        OPTIONS="$OPTIONS --force"
        restart
        ;;
    reload)
        reload
        ;;
    condrestart)
        if [ -f /var/lock/subsys/autofs ]; then
            restart
        fi
        ;;
    *)
        echo $"Usage: $0 {start|forcestart|stop|status|restart|forcerestart|reload|condrestart}"
        exit 1;
        ;;
esac

exit $?