#!/bin/sh
# S60alkane-web: serve the Alkane console and HTTP API.
#
# Starts after the network (S40). `alkane-os --serve` brings up the web console
# (gated on a login) and the read-only JSON API on port 80, falling back to 8080
# without privilege. The board HAL/mining backend is started separately; this
# only needs the box to be reachable and observable, so every page renders and
# the console works before any hashboard is driven.
#
# This file is part of the alkane-os image overlay (image/overlay), dropped into
# the SDK rootfs at build time. It is intentionally not in the SDK's own init,
# which owns board-detect / network / ssh and leaves the console to alkane-os.

PIDFILE=/run/alkane-web.pid
BIN=/usr/bin/alkane-os

start() {
    [ -x "$BIN" ] || { echo "alkane-web: $BIN missing"; exit 1; }
    echo "alkane-web: starting console on :80 (falls back to :8080)"
    start-stop-daemon -S -b -m -p "$PIDFILE" -x "$BIN" -- --serve
}

stop() {
    echo "alkane-web: stopping console"
    start-stop-daemon -K -q -p "$PIDFILE" 2>/dev/null
    rm -f "$PIDFILE"
}

case "$1" in
    start) start ;;
    stop) stop ;;
    restart) stop; start ;;
    *) echo "usage: $0 {start|stop|restart}"; exit 1 ;;
esac
