#!/bin/sh

# PROVIDE: flan
# REQUIRE: NETWORKING rpcbind syslogd
# KEYWORD: shutdown
#
# Service for Flan
# https://www.antonmcclure.com/software/flan

. /etc/rc.subr

name="flan"
rcvar="flan_enable"

load_rc_config $name

: ${flan_enable:="NO"}
: ${flan_user:="root"}
: ${flan_tty:="/dev/ttyv6"}
: ${flan_config:="/usr/local/etc/flan.cfg"}

start_cmd="flan_start"
stop_cmd="flan_stop"

flan_start()
{
    echo "Starting Flan."
    mkdir -p /run/flan
    pkill -t ${flan_tty#/dev/}
    export XDG_RUNTIME_DIR="/run/flan"
    /usr/sbin/daemon -u "${flan_user}" -p /var/run/flan.pid -S -t "flan" /usr/local/bin/xinit /usr/local/bin/flan "${flan_config}" -- :0 vt7
}

flan_stop()
{
    echo "Stopping Flan."
    if [ -f /var/run/flan.pid ]; then
        kill $(cat /var/run/flan.pid)
        rm -f /var/run/flan.pid
    fi
    pkill -t ${flan_tty#/dev/}
}

run_rc_command "$1"
