#!/bin/sh

MODULE=$1
BASE_PACKAGE=$2

DPKG=/usr/bin/dpkg
DEBPATH=/var/lib/jolicloud-hardware/repo-cache
CONFIGPATH=/usr/lib/jolicloud-hardware/config

if [ $# -lt 2 ]; then
    exit 1
fi

err=0

if `$DPKG --get-selections $BASE_PACKAGE 2>/dev/null | grep -q \[^de\]install`; then
    echo "Already installed $MODULE package $BASE_PACKAGE.";
    #exit 0;
else
    if [ -d $DEBPATH/$MODULE ]; then
        echo "Installing $MODULE package $BASE_PACKAGE.";
        plymouth message --text="Please wait, initializing VirtualBox driver..."
        $DPKG -i -G $DEBPATH/$MODULE/*
        err=$?
        plymouth message --text="Continuing boot process..."
    fi
fi

[ $? != 0 ] && exit $?

if [ -x $CONFIGPATH/config-xorg-$MODULE.py ]; then
    echo "Running $MODULE configuration package.";
    $CONFIGPATH/config-xorg-$MODULE.py
    err=$?
fi

# Manually load vboxguest now, since the module was just built by dkms. All
# other modules are loaded automatically after jolicloud-hardware finishes.
# Vboxguest will also be autoloaded on next bootup by its hotplug alias.
modprobe vboxguest

exit $err
