#!/bin/sh

. /opt/etc/functions.openrc
. /opt/etc/functions.noopenrc

vmpool="z???/VM"
offending_vms="win1 win2"

update_me

some_vm_active() {
	local _vm
	# 'virsh list --name' can output several EOLs, 'wc -l' is useless.
	for _vm in `virsh list --name`; do
		return 0
	done
	return 1
}

for ovm in ${offending_vms}; do
	if virsh list | grep $ovm > /dev/null 2>&1; then
		echo "${ME}: your must manually stop VM $ovm"
		exit 1
	else
		echo "${ME}: problematic VM $ovm is not running, Ok..."
	fi
done

update_me

echo "${ME} no offending VMs running; stopping others..."

service vmman stop
service winbindd stop
sleep 1
service pdc stop

update_me

if some_vm_active; then
	echo "${ME}: some VMs are still active, this should not happen!"
fi

service libvirtd stop

snapname="${vmpool}@bs$(date "+%Y-%m-%d")"

zfs snapshot -r ${snapname}

service libvirtd start
service wbwait start
service smbd start
service vmman start
