#!/bin/sh

DB=/etc/vbox/db
CONFIG=/etc/vbox/autostart.cfg

if [ $(id -u) != 0 ]; then
    cat >&2 <<EOT
Usage:

VBoxManage setproperty autostartdbpath $DB

VBoxManage modifyvm <VM> --autostart-enabled on

Hint: $0 must be run as root too!
EOT
    exit 1
fi

mkdir -p "$DB"
chown root:vboxusers "$DB"
chmod 1770 "$DB"

mkdir -p "$(dirname $CONFIG)"
cat >$CONFIG <<EOT
# Default policy is to deny starting a VM, the other option is "allow".
default_policy = deny
EOT

IFS=,
for user in $(getent group vboxusers|cut -d: -f4-); do
    cat >>$CONFIG <<EOT

$user = {
    allow = true
    startup_delay = 0
}
EOT
done
unset IFS

cat >/etc/default/virtualbox <<EOT
VBOXAUTOSTART_DB=$DB
VBOXAUTOSTART_CONFIG=$CONFIG
EOT
