#!/bin/sh
#
# Linux Additions kernel module init script
#

# 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

MODPROBE=/sbin/modprobe
VBOXCLIENT=/usr/bin/VBoxClient

if $MODPROBE -c | grep -q '^allow_unsupported_modules  *0'; then
  MODPROBE="$MODPROBE --allow-unsupported-modules"
fi

# Check architecture
cpu=`uname -m`;
case "$cpu" in
  i[3456789]86|x86)
    cpu="x86"
    lib_path="/usr/lib"
    ;;
  x86_64|amd64)
    cpu="amd64"
    if test -d "/usr/lib64"; then
      lib_path="/usr/lib64"
    else
      lib_path="/usr/lib"
    fi
    ;;
esac

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

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

running_vboxguest()
{
    lsmod | grep -q "vboxguest[^_-]"
}

running_vboxsf()
{
    lsmod | grep -q "vboxsf[^_-]"
}

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

start()
{
    action "Loading VirtualBox module (vboxguest):" modprobe vboxguest
    RETVAL=$?
    if [ $RETVAL = 0 ]; then
        action "Loading VirtualBox module (vboxsf):" modprobe vboxsf
        RETVAL=$?
        if [ $RETVAL = 0 ]; then
            action "Mount VirtualBox Share Folders:" mount -a -t vboxsf
            [ ! -f "$VBOXCLIENT" ] ||
                action "Loading VirtualBox module (vboxvideo):" modprobe vboxvideo
            RETVAL=$?
        fi
    fi
    [ $RETVAL = 0 ] && touch "$LOCKFILE" ||:
    return $RETVAL
    # This isn't necessary anymore as the vboxsf module is autoloaded.
    # mount -a -t vboxsf
}

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

restart()
{
    stop && start
    # At least Fedora 11 and Fedora 12 require the correct security context when
    # executing this command from service scripts. Shouldn't hurt for other
    # distributions.
    #chcon -u system_u -t mount_exec_t "$lib_path/$PACKAGE/mount.vboxsf" > /dev/null 2>&1
    # And at least Fedora 15 needs this for the acceleration support check to
    # work
    #redhat_release=`cat /etc/redhat-release 2> /dev/null`
    #case "$redhat_release" in Fedora\ release\ 15* )
    #    for i in "$lib_path"/*.so
    #    do
    #        restorecon "$i" >/dev/null
    #    done
    #    ;;
    #esac

    return 0
}

status()
{
    if running_vboxguest; then
        echo "vboxguest module is loaded"
        running_vboxsf && echo "vboxsf 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
