#! /bin/sh
# Sun VirtualBox
# Linux Additions kernel module init script
#
# Copyright (C) 2006-2009 Sun Microsystems, Inc.
#
# This file is part of VirtualBox Open Source Edition (OSE), as
# available from http://www.virtualbox.org. This file is free software;
# you can redistribute it and/or modify it under the terms of the GNU
# General Public License (GPL) as published by the Free Software
# Foundation, in version 2 as it comes in the "COPYING" file of the
# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
#
# Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
# Clara, CA 95054 USA or visit http://www.sun.com if you need
# additional information or have any questions.
#


# chkconfig: 35 30 70
# description: VirtualBox Linux Additions kernel modules
#
### BEGIN INIT INFO
# Provides:       vboxadd
# Required-Start:
# Required-Stop:
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Description:    VirtualBox Linux Additions kernel modules
### END INIT INFO

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

LOCKFILE=/var/lock/subsys/virtualbox-addition
RETVAL=0

running_vboxadd()
{
    lsmod | grep -q "vboxadd[^_-]"
}

running_vboxvfs()
{
    lsmod | grep -q "vboxvfs[^_-]"
}

running_vboxvideo()
{
    lsmod | grep -q "vboxvideo[^_-]"
}

start()
{
    action "Loading VirtualBox module (vboxadd):" modprobe vboxadd 
    RETVAL=$?
    if [ $RETVAL = 0 ]; then
        action "Loading VirtualBox module (vboxvfs):" modprobe vboxvfs
        RETVAL=$?
        if [ $RETVAL = 0 ]; then
            action "Mount VirtualBox Share Folders:" mount -a -t vboxsf
            action "Loading VirtualBox module (vboxvideo):" modprobe vboxvideo
            RETVAL=$?
        fi
    fi
    [ $RETVAL = 0 ] && touch "$LOCKFILE" ||:
    return $RETVAL
}

stop()
{
    local RETVAL1 RETVAL2 RETVAL3
    action "Unmount VirtualBox Share Folders:" umount -a -t vboxsf
    action "Unloading VirtualBox module (vboxvideo):" modprobe -r vboxvideo || RETVAL3=$?
    action "Unloading VirtualBox module (vboxvfs):" modprobe -r vboxvfs || RETVAL2=$?
    action "Unloading VirtualBox module (vboxadd):" modprobe -r vboxadd || RETVAL1=$?
    [ $RETVAL3 = 0 ] || RETVAL=$RETVAL3
    [ $RETVAL2 = 0 ] || RETVAL=$RETVAL2
    [ $RETVAL1 = 0 ] || RETVAL=$RETVAL1
    [ $RETVAL = 0 ] && rm -f "$LOCKFILE" ||:
    return $RETVAL
}

restart()
{
    stop && start
}

status()
{
    if running_vboxadd; then
        echo "vboxadd module is loaded"
        running_vboxvfs && echo "vboxvfs module is loaded"
        running_vboxvideo && echo "vboxvideo module is loaded"
        echo "The VirtualBox Additions are currently running."
    elif [ -f "$LOCKFILE" ]; then
        echo "The VirtualBox Additions module is not loaded, but subsystem is locked"
        RETVAL=2
    else
        echo "The VirtualBox Additions are not currently running"
    fi
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    condstop)
        if [ -e "$LOCKFILE" ]; then
            stop
        fi
        ;;
    condrestart)
        # Do nothing on package upgrade
        ;;
    status)
        status
        ;;
    *)
        msg_usage "${0##*/} {start|stop|restart|condstop|condrestart|status}"
        RETVAL=1
esac

exit $RETVAL
