#!/usr/bin/env cbsd
#v11.1.19
CBSDMODULE="bhyve"
CIXARG="jname"
CIXOPTARG="mode"
MYDESC="Pause and resume bhyve domain (STOP/CONT signal)"
ADDHELP="
${H3_COLOR}Description${N0_COLOR}:

 Send STOP or CONT signal to a bhyve domain process to pause or resume it.

${H3_COLOR}Options${N0_COLOR}:

 ${N2_COLOR}jname=${N0_COLOR} - target bhyve domain name;
 ${N2_COLOR}mode=${N0_COLOR}  - action mode:
           * auto - toggle: if active then pause, if paused then resume (default);
           * on   - pause (send STOP signal);
           * off  - unpause/resume (send CONT signal);

${H3_COLOR}Examples${N0_COLOR}:

 # cbsd bpause jname=vm1
 # cbsd bpause jname=vm1 mode=on
 # cbsd bpause jname=vm1 mode=off

${H3_COLOR}See also${N0_COLOR}:

  cbsd bstart --help
  cbsd bstop --help
  cbsd bset --help

"

. ${subrdir}/nc.subr
. ${tools}
. ${strings}

cixinit

[ -z "${mode}" ] && mode="auto"

. ${subrdir}/rcconf.subr
if [ $? -eq 1 ]; then
	log_err 1 "${N1_COLOR}No such vm: ${N2_COLOR}${jname}${N0_COLOR}"
fi
[ "${emulator}" != "bhyve" ] && log_err 1 "${N1_COLOR}Not in bhyve emulator: ${N2_COLOR}${emulator}${N0_COLOR}"

capture _search_cmd echo "bhyve: ${jname}"

vm_pid=
pid_count=

capture _ps_output ${PS_CMD} axopid,state,ucomm,command -ww

while read _ps_line; do
	[ -z "${_ps_line}" ] && continue

	# fast filter: skip lines without "bhyve"
	case "${_ps_line}" in *bhyve*) ;; *) continue ;; esac

	# parse pid, state, ucomm, command in one awk call
	capture _pf ${AWK_CMD} '{cmd=""; for(i=4;i<=NF;i++) cmd=cmd (i>4?" ":"") $i; print $1,$2,$3,cmd}' <<< "${_ps_line}"
	read _ps_pid _ps_state _ps_ucomm _ps_cmd <<< "${_pf}"

	[ "${_ps_ucomm}" != "bhyve" ] && continue

	# check command prefix: "bhyve: ${jname}"
	_cmd_prefix=$( substr --pos=0 --len=${#_search_cmd} --str="${_ps_cmd}" )
	[ "${_cmd_prefix}" != "${_search_cmd}" ] && continue

	# found matching bhyve process
	vm_pid="${_ps_pid}"

	if [ "${_ps_state}" != "T" ]; then
		pid_count=$(( pid_count + 1 ))
	fi

	break
done <<< "${_ps_output}"

if [ -z "${vm_pid}" -o "${vm_pid}" = "0" ]; then
	cbsdlogger WARNING ${CIX_APP}: unable to determine pid for: ${jname}
	log_err 1 "${N1_COLOR}Unable to determine pid for: ${N2_COLOR}${jname}${N0_COLOR}"
fi

if [ -z "${pid_count}" ]; then
	cbsdlogger WARNING ${CIX_APP}: unable to determine pid_count for: ${jname}
	log_err 1 "${N1_COLOR}Unable to determine pid_count for: ${N2_COLOR}${jname}${N0_COLOR}"
fi

case "${mode}" in
	auto)
		if [ ${pid_count} -gt 0 ]; then
			cbsdlogger NOTICE ${CIX_APP}: bhyve domain is active, triggering to sleep: ${jname}
			${ECHO} "${N1_COLOR}bhyve domain is active, triggering to sleep${N0_COLOR}"
			SIGNAL="STOP"
		else
			cbsdlogger NOTICE ${CIX_APP}: bhyve domain is sleeping, triggering to continue: ${jname}
			${ECHO} "${N1_COLOR}bhyve domain is sleeping, triggering to continue${N0_COLOR}"
			SIGNAL="CONT"
		fi
		;;
	on)
		SIGNAL="STOP"
		;;
	off)
		SIGNAL="CONT"
		;;
esac

cbsdlogger NOTICE ${CIX_APP}: send ${SIGNAL} signal to pid \(${jname}\): ${vm_pid}
${KILL_CMD} -${SIGNAL} ${vm_pid}

exit 0
