#!/usr/bin/env cbsd
#v12.0.0
CIXARG="jname"
CIXOPTARG=""
MYDESC="bhyve process existence check"
CBSDMODULE="bhyve"
ADDHELP="
${H3_COLOR}Description${N0_COLOR}:

 Check whether a bhyve process for the specified virtual machine is running.
 Exits with 1 if the process exists (and outputs the PID to stdout),
 or 0 if no matching process is found.

 Used internally by bmigrate and other tools to verify VM state before
 performing operations such as live migration.

${H3_COLOR}Options${N0_COLOR}:

 ${N2_COLOR}jname=${N0_COLOR}     - target virtual machine name.

${H3_COLOR}Examples${N0_COLOR}:

 # cbsd bhyve-exist jname=myvm
 # cbsd bhyve-exist jname=myvm && echo \"VM is running\"

${H3_COLOR}See also${N0_COLOR}:

  cbsd bmigrate --help
  cbsd bstart --help
  cbsd bget --help

"

. ${subrdir}/nc.subr
cixinit
. ${subrdir}/bhyve.subr

check_for_bhyve_process -j ${jname}
ret=$?

if [ -n "${vm_pid}" ]; then
	echo "${vm_pid}"
fi

exit ${ret}
