#!/bin/bash
#
#	Add vboxusers default virtualbox user group into our DB to protect it, if it isn't there
#	Create both the default vboxweb-service runtime user (vbox), if it isn't already there
#	Set/reset the vbox users password to a random one and store in the configuration database
#

    # Add the default vboxusers group to our config DB, if it's not already there (so we can't mess with it)
    if /sbin/e-smith/db accounts get vboxusers >/dev/null
    then
	echo "the vboxusers group already exists - we'll use this"
    else
	/sbin/e-smith/db accounts set vboxusers system Description "VBox users"
        /usr/sbin/usermod -a -G vboxusers admin >/dev/null
    fi

    # Create the default vboxweb-service runtime user account (vbox), if it doesn't exist and add to vboxusers group
    if /sbin/e-smith/db accounts get vbox >/dev/null
    then
	echo "the vbox user already exists - we'll use this"
    else
        /sbin/e-smith/db accounts set vbox system Description "VBoxWeb runtime user" PasswordSet yes
        /usr/sbin/usermod -a -G vboxusers vbox >/dev/null
    fi

    # Set/reset the vbox user password to a random one and store in the configuraion database
    if /sbin/e-smith/db configuration getprop vboxweb-service password >/dev/null
    then
        echo "we'll use the existing vbox user password"
	password="$(/sbin/e-smith/db configuration getprop vboxweb-service password)"
    else
        password="$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 32)"
	/sbin/e-smith/config setprop vboxweb-service password $password
    fi
    /usr/bin/echo $password | /usr/bin/passwd --stdin vbox
