#!/bin/bash
#
#  vboxtoolinit: Frontend for vboxtool for auto start sessions when booting and save 
#                sessions when host is stopped
#
#  This is a wrapper for vboxtool. It is to be placed in /etc/init.d to provide auto
#  start at boot time and stop when the host is halted. Because it's a wrapper, the 
#  original functions of vboxtool can be executed as usual, without cd'ing to 
#  /etc/init.d.
#
#  Usage: Should be placed in /etc/init.d
#
#  Copyright (C) 2008 Mark Baaijens <mark.baaijens@gmail.com>
#
#  This file is part of VBoxTool.
#
#  VBoxTool is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  VBoxTool is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

### BEGIN INIT INFO
# Provides:          vboxtool
# Required-Start:    $syslog $local_fs $remote_fs
# Required-Stop:     $syslog $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       Controls VirtualBox sessions
### END INIT INFO

# Command mapping between vboxtoolinit and vbox. Because the init-system requires specific 
# commands like 'start' and 'stop' and we don't want to execute these commands literally, 
# vboxtoolinit maps these to the desired command in vboxtool.

start()
{
  # 'vboxtoolinit start' maps to 'vboxtool autostart'; when the host boots, all sessions in 
  # the config file /etc/vboxtool/machines.conf are started
  nohup $su_command "vboxtool autostart" > /dev/null
}

stop()
{
  # 'vboxtoolinit stop' maps to 'vboxtool save'; when the host halts, all running sessions 
  # are saved, instead of stopped.
  nohup $su_command "vboxtool save" > /dev/null
}

restart()
{
  stop
  start
}

# Some constants
config_file='/etc/vboxtool/vboxtool.conf'

# Retrieve settings from config file, just by executing the config file.
# Config file $config_file should look like this:
# vbox_user='<user name>'
if [ -f $config_file ]
then
  . $config_file
else
  echo "Error: $config_file does not exist. Exiting."
  exit 1
fi

if [ ! -n "$vbox_user" ]
then
  echo "Error: vbox_user not defined in $config_file. Exiting."
  exit 1  
fi

# Implementation of user control, execute several commands as another (predefined) user, 
# thus freeing the main script vboxtool from any user related issues.
su_command="su - $vbox_user -c"

#
# Check for a commandline option
#
case "$1" in
start)
  start
  ;;
stop)
  stop
  ;;
restart)
  restart
  ;;  
reload)
        echo -n "Reloading service is not implemented yet "
        ## Like force-reload, but if daemon does not support
        ## signaling, do nothing (!)

        # If it supports signaling:
        #echo -n "Reload service FOO "
        #/sbin/killproc -HUP $FOO_BIN
        #touch /var/run/FOO.pid
        #rc_status -v

        ## Otherwise if it does not support reload:
        #rc_failed 3
        #rc_status -v
        ;;
status)
        echo -n "Checking for service status is not implemented yet "
        #echo -n "Checking for service FOO "
        ## Check status with checkproc(8), if process is running
        ## checkproc will return with exit status 0.

        # Return value is slightly different for the status command:
        # 0 - service up and running
        # 1 - service dead, but /var/run/  pid  file exists
        # 2 - service dead, but /var/lock/ lock file exists
        # 3 - service not running (unused)
        # 4 - service status unknown :-(
        # 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)

        # NOTE: checkproc returns LSB compliant status values.
        #/sbin/checkproc $FOO_BIN
        # NOTE: rc_status knows that we called this init script with
        # "status" option and adapts its messages accordingly.
        #rc_status -v
        ;;
*)
  ;;  
esac

exit 0

