#!/bin/sh
#
# PROVIDE: clawdie_firstboot
# REQUIRE: NETWORKING LOGIN
# BEFORE: clawdie
# KEYWORD: firstboot
#
# clawdie-firstboot — runs once on first HDD boot to complete Clawdie-AI setup.
# Self-disables on completion.

. /etc/rc.subr

name="clawdie_firstboot"
rcvar="${name}_enable"
start_cmd="${name}_start"
stop_cmd=":"

SHARE="/usr/local/share/clawdie-iso"
LOG="/var/log/clawdie-firstboot.log"
HANDOFF_FILE="/var/db/clawdie-installer/clawdie-handoff.sealed"

clawdie_firstboot_start()
{
    echo "Starting Clawdie first-boot setup..."

    # Run on ttyv0 so bsddialog has a real TTY for the wizard
    /usr/bin/script -q -a "$LOG" \
        /bin/sh "${SHARE}/firstboot/firstboot.sh"

    RC=$?

    if [ "$RC" -eq 0 ]; then
        echo "Clawdie first-boot setup complete. Disabling service."
        sysrc -x clawdie_firstboot_enable
        rm -f "$HANDOFF_FILE"
        rmdir /var/db/clawdie-installer 2>/dev/null || true
        rm -rf "$SHARE"
    else
        echo "Clawdie first-boot setup failed (exit $RC). Check $LOG"
    fi
}

load_rc_config "$name"
: "${clawdie_firstboot_enable:=NO}"
run_rc_command "$1"
