#!/bin/sh
#
# PROVIDE: bhyveopil 
# REQUIRE: networking
# KEYWORD:

. /etc/rc.subr

name="bhyveopil"
rcvar="bhyveopil_enable"


stop_cmd="${name}_stop"
bhyveopil_stop()
{
# Loop running vms to shutdown
# For each one, send the ACPI shutdown command
# Allow 2 mins for shutdown
# If still has not shut down, kill the Bhyve vm process
# Then destroy the vm

# Check if directory /dev/vmm exists.
vminstancedir="/dev/vmm"
if  [ ! -d "$vminstancedir" ]
then
        echo "Directory $vminstancedir not found.  "
        echo "No Bhyve vms to shutdown.  "
else
        echo "Directory $vminstancedir exists.  "
        for FILE in /dev/vmm/*
do
        vmname=$(echo "$FILE" | cut -d/ -f4 )
        echo "$vmname"
sh /usr/local/bhyveopil/stopvmgracefully.sh $vmname &
                done
                fi
}




bhyveopil_user="root"
bhyveopil_command="/usr/local/bhyveopil/startbhyvevms.sh"
pidfile="/var/run/bhyveopil/${name}.pid"
command="/usr/sbin/daemon"
command_args="-P ${pidfile} -o /usr/local/bhyveopil/bhyveopil.log -f ${bhyveopil_command}"

load_rc_config $name
: ${bhyveopil_enable:=no}

run_rc_command "$1"

