#!/usr/bin/env bash
# Unified start script for Maven services.
# Usage: ./start-maven.sh [build]
#   - Pass "build" as first arg to rebuild everything before starting.
#
# This assumes ROOT=/home/kami/apps/Maven and the local Go toolchain at
# $ROOT/deps/go/go/bin/go.

set -euo pipefail

ROOT="/home/kami/apps/Maven"

# ---------------------------------------------------------------------------
# Environment
# ---------------------------------------------------------------------------
export ROOT
export CGO_CFLAGS="-I$ROOT/deps/include -I$ROOT/deps/whisper.cpp/ggml/include"
export CGO_LDFLAGS="-L$ROOT/deps/lib -Wl,-rpath,$ROOT/deps/lib"
export LD_LIBRARY_PATH="$ROOT/deps/lib"
export PATH="$ROOT/deps/go/go/bin:$PATH"

# ---------------------------------------------------------------------------
# Optional: build
# ---------------------------------------------------------------------------
if [[ "${1:-}" == "build" ]]; then
    echo "--- Building all commands ---"
    cd "$ROOT"
    go build ./cmd/mavend/
    go build ./cmd/mavsttd/   # needs CGO (whisper.cpp)
    go build ./cmd/mavttsd/   # pure Go
    go build ./cmd/mavweb/
    go build ./cmd/mavpoll/
    echo "--- Build complete ---"
fi

# ---------------------------------------------------------------------------
# Ensure directories exist
# ---------------------------------------------------------------------------
mkdir -p /run/user/1000/maven
mkdir -p /home/kami/.local/share/maven

# ---------------------------------------------------------------------------
# Start services
# ---------------------------------------------------------------------------
echo "--- Starting Maven services ---"

cd "$ROOT"

# 1. Core daemon
./mavend > /tmp/mavend.log 2>&1 &
MAVEND_PID=$!
echo "mavend pid=$MAVEND_PID"

# Wait for mavend to create its socket (it loads models at startup)
for i in $(seq 1 30); do
    if [ -S /run/user/1000/maven/mavend.sock ]; then
        break
    fi
    sleep 1
done

# 2. STT worker (needs LD_LIBRARY_PATH for libwhisper.so, libggml-vulkan.so)
export LD_LIBRARY_PATH="$ROOT/deps/lib"
./mavsttd -socket /run/user/1000/maven/stt.sock -model models/stt/ggml-small.bin > /tmp/mavsttd.log 2>&1 &
MAVSTTD_PID=$!
echo "mavsttd pid=$MAVSTTD_PID"

# 3. TTS worker (needs LD_LIBRARY_PATH for Piper's espeak-ng)
export LD_LIBRARY_PATH="$ROOT/deps/piper"
./mavttsd -socket /run/user/1000/maven/tts.sock -piper deps/piper/piper -model models/tts/ru_RU-irina-medium.onnx -espeak_data deps/piper/espeak-ng-data > /tmp/mavttsd.log 2>&1 &
MAVTTSD_PID=$!
echo "mavttsd pid=$MAVTTSD_PID"

# 4. Web bridge (pure Go, no deps)
./mavweb -addr :9201 -voice 127.0.0.1:9100 -core /run/user/1000/maven/mavend.sock > /tmp/mavweb.log 2>&1 &
MAVWEB_PID=$!
echo "mavweb pid=$MAVWEB_PID"

# 5. Env poller
./mavpoll -socket /run/user/1000/maven/mavend.sock -netdata http://127.0.0.1:19999 > /tmp/mavpoll.log 2>&1 &
MAVPOLL_PID=$!
echo "mavpoll pid=$MAVPOLL_PID"

# ---------------------------------------------------------------------------
# Trap SIGINT to forward it to children, then wait for everything
# ---------------------------------------------------------------------------
cleanup() {
    echo ""
    echo "--- Stopping Maven services ---"
    kill $MAVEND_PID  2>/dev/null || true
    kill $MAVSTTD_PID 2>/dev/null || true
    kill $MAVTTSD_PID 2>/dev/null || true
    kill $MAVWEB_PID  2>/dev/null || true
    kill $MAVPOLL_PID 2>/dev/null || true
}
trap cleanup INT TERM

echo ""
echo "All services started. Press Ctrl-C to stop."
wait
