#!/usr/local/bin/execlineb -S2
# {{ ansible_managed }}

s6-envdir ./env
multisubstitute {
        importas -i -u NAME NAME
}

fdmove -c 1 2
ifelse { test "${1}" -eq 0 } {
	foreground { echo "bhyve-${NAME}: Guest requested a reboot." }
	bhyvectl --vm="${NAME}" --destroy
}
ifelse { test "${1}" -eq 1 } {
	foreground { echo "bhyve-${NAME}: Guest requested a power off." }
	foreground { bhyvectl --vm="${NAME}" --destroy }
	s6-svc -O .
}
ifelse { test "${1}" -eq 2 } {
	foreground { echo "bhyve-${NAME}: Guest requested a halt." }
	s6-svc -O .
}
ifelse { test "${1}" -eq 3 } {
	foreground { echo "bhyve-${NAME}: Guest crashed with a triple fault. Rebooting." }
	bhyvectl --vm="${NAME}" --destroy
}
foreground { echo "Guest ${NAME} caused bhyve to exit with (${1}, ${2})." }
bhyvectl --vm="${NAME}" --destroy