#!/bin/sh

###
# Use rc.local to start virtualbox at boot.
###

# if rc.vboxdrv is executable, run it on startup
run=`grep "sh /etc/rc.d/rc.vboxdrv" etc/rc.d/rc.local`
if [[ "${run}" == "" ]]; then
cat << EOF >> etc/rc.d/rc.local

#VirtualBox driver
# To disable VirtualBox driver, chmod rc.vboxdrv to 644
if [ -x /etc/rc.d/rc.vboxdrv ]; then
        sh /etc/rc.d/rc.vboxdrv start
fi
EOF
fi

# if rc.vboxnet is executable, run it on startup
run=`grep "sh /etc/rc.d/rc.vboxnet" etc/rc.d/rc.local`
if [[ "${run}" == "" ]]; then
cat << EOF >> etc/rc.d/rc.local

#VirtualBox network
# To disable VirtualBox network, chmod rc.vboxnet to 644
if [ -x /etc/rc.d/rc.vboxnet ]; then
        sh /etc/rc.d/rc.vboxnet start
fi
EOF
fi

# if rc.vboxbridge is executable, run it on startup
run=`grep "sh /etc/rc.d/rc.vboxbridge" etc/rc.d/rc.local`
if [[ "${run}" == "" ]]; then
cat << EOF >> etc/rc.d/rc.local

#VirtualBox bridge
# To enable VirtualBox network, chmod rc.vboxbridge to 755
if [ -x /etc/rc.d/rc.vboxbridge ]; then
        sh /etc/rc.d/rc.vboxbridge start 1 user
fi
EOF
fi

# Chroot to insert the group in the possible --root target
chroot . << EOR 2>/dev/null
/usr/sbin/groupadd -g215 vboxusers 2> /dev/null
EOR

echo "********************************************************************************"
echo "* Remember to run '/etc/rc.d/rc.vboxdrv setup' after install                   *"
echo "*                                                                              *"
echo "* To enable bridged networking change the execution permissions of             *"
echo "* /etc/rc.d/rc.vboxbridge and modify rc.local line to match your number of     *"
echo "* machines and the user running virtualbox.                                    *"
echo "* rc.vboxbridge {start [number of virtual interfaces] [user] | stop}           *"
echo "*                                                                              *"
echo "* From version 2.2.0 there is only a qt4 gui, so no additional packages are    *"
echo "* available anymore.                                                           *"
echo "*                                                                              *"
echo "* Legal notice: the guest additions included in this package are licensed      *"
echo "* under the PUEL license, so you can't use these in commercial enviroments.    *"
echo "********************************************************************************"
( cd usr/bin ; rm -rf vboxsdl )
( cd usr/bin ; ln -sf ../lib/virtualbox/VBox.sh vboxsdl )
( cd usr/bin ; rm -rf vboxheadless )
( cd usr/bin ; ln -sf ../lib/virtualbox/VBox.sh vboxheadless )
( cd usr/bin ; rm -rf virtualbox )
( cd usr/bin ; ln -sf ../lib/virtualbox/VBox.sh virtualbox )
( cd usr/bin ; rm -rf vboxmanage )
( cd usr/bin ; ln -sf ../lib/virtualbox/VBox.sh vboxmanage )
