#!/bin/sh
#
# REQUIRE: LOGIN
# PROVIDE: windows
# KEYWORD: shutdown

. /etc/rc.subr
name=windows
rcvar=${name}_enable
start_cmd="${name}_start"
stop_cmd="${name}_stop"
status_cmd="${name}_status"
load_rc_config $name

tmux_name="windows"

windows_start() {
   /usr/local/bin/tmux new-session -d -s "$tmux_name" "/usr/bin/env VNC_ENABLE=YES VNC_LISTEN=0.0.0.0:5900 /usr/local/bin/bash /usr/local/bin/launch_windows start windows zroot/freebsd/current/vm/windows /vm/windows /vm/.iso/Win11_23H2_English_x64v2.iso"
}

windows_status() {
    if /usr/local/bin/tmux has-session -t $tmux_name 2>/dev/null; then
        echo "$tmux_name is running."
    else
        echo "$tmux_name is not running."
        return 1
    fi
}

windows_stop() {
    /usr/local/bin/tmux has-session -t $tmux_name 2>/dev/null && (
        /usr/local/bin/tmux kill-session -t $tmux_name
        sleep 10
        bhyvectl --vm=windows --destroy
        # kill `cat /var/run/windows.pid`
    )
    windows_wait_for_end
}

windows_wait_for_end() {
    while /usr/local/bin/tmux has-session -t $tmux_name 2>dev/null; do
        sleep 1
    done
}

run_rc_command "$1"
