#!/bin/sh

### BEGIN INIT INFO
# Provides:                 open-vm-tools
# Required-Start:           $local_fs $remote_fs
# Required-Stop:            $local_fs $remote_fs
# X-Start-Before:
# X-Stop-After:
# Default-Start:            2 3 4 5
# Default-Stop:             0 1 6
# Description:              Runs the open-vm-tools services
# Short-Description:        Runs the open-vm-tools services
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC=`basename $0`
NAME=vmtoolsd
DAEMON=/usr/bin/$NAME
PIDPATH=/var/run
PIDFILE=$PIDPATH/$NAME.pid
DAEMON_ARGS="--background ${PIDFILE}"
SCRIPTNAME=/etc/init.d/${DESC}

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions




exit_if_not_in_vm () {
    if [ ! -x /usr/bin/vmware-checkvm ] || ! /usr/bin/vmware-checkvm > /dev/null 2>&1
    then
        [ "$VERBOSE" != no ] && log_daemon_msg "Not starting as we're not running in a VM." "$NAME"
        [ "$VERBOSE" != no ] && log_end_msg 0
        exit 0
    fi
}

#
# Function that starts the daemon/service
#
do_start()
{
    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running
    #   2 if daemon could not be started
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
        || return 1
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
        $DAEMON_ARGS \
        || return 2
}

#
# Function that stops the daemon/service
#
do_stop()
{
    # Return
    #   0 if daemon has been stopped
    #   1 if daemon was already stopped
    #   2 if daemon could not be stopped
    #   other if a failure occurred
    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --exec $DAEMON
    RETVAL="$?"
    [ "$RETVAL" = 2 ] && return 2
    # Many daemons don't delete their pidfiles when they exit.
    rm -f $PIDFILE
    return "$RETVAL"
}

do_load_modules() {
    # the daemon works well without the modules, so ignore
    # if they fail to load.
    [ "$VERBOSE" != no ] && log_daemon_msg "Loading $DESC modules"
    [ "$VERBOSE" != no ] && log_progress_msg "vmhgfs"
    modprobe vmhgfs
    [ "$VERBOSE" != no ] && log_progress_msg "vmsync"
    modprobe vmsync
    log_end_msg 0
}

do_remove_modules()
{
    [ "$VERBOSE" != no ] && log_daemon_msg "Removing $DESC modules"
    [ "$VERBOSE" != no ] && log_progress_msg "vmhgfs"
    modprobe -r vmhgfs
    [ "$VERBOSE" != no ] && log_progress_msg "vmsync";
    modprobe -r vmsync
    log_end_msg 0
}

case "${1}" in
    start)
        # Check if we're running inside VMWare
        exit_if_not_in_vm

        # load modules
        do_load_modules

        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
        do_start
        case "$?" in
            0|1)
                [ "$VERBOSE" != no ] && log_end_msg 0
                RETVAL=0
                ;;
            2)
                [ "$VERBOSE" != no ] && log_end_msg 1
                RETVAL=1
                ;;
        esac

        ;;

    stop)
        # Check if we're running inside VMWare
        exit_if_not_in_vm

        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
        do_stop
        case "$?" in
            0|1)
                [ "$VERBOSE" != no ] && log_end_msg 0
                RETVAL=0
                ;;
            2)
                [ "$VERBOSE" != no ] && log_end_msg 1
                RETVAL=1
                ;;
        esac

        do_remove_modules

        ;;

    force-reload|restart)
        exit_if_not_in_vm
        #
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop
        case "$?" in
            0|1)
                do_remove_modules
                do_load_modules

                do_start
                case "$?" in
                    0)
                        log_end_msg 0
                        RETVAL=0
                        ;;
                    1) # Old process is still running
                        log_end_msg 1
                        RETVAL=1
                        ;; 

                    *) # Failed to start
                        log_end_msg 1
                        RETVAL=1
                        ;;
                esac
                ;;
            *)
                # Failed to stop
                log_end_msg 1
                RETVAL=1
                ;;
        esac
        ;;
    *)
        log_success_msg "Usage: ${0} {start|stop|restart|force-reload}"
        exit 1
        ;;
esac

exit $RETVAL
