#!/bin/bash
# system_update.sh - Runs after git checkout in the Python system_update endpoint.
# Called with the current working directory = project root.
# All output is also written to /tmp/system_update.log for debugging.

exec > >(tee -a /tmp/system_update.log) 2>&1

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"
POETRY="$HOME/.local/bin/poetry"
OPENOCD_SRC="$HOME/sw_openocd"
OPENOCD_REPO="ssh://git@gitea.summitwave.work:222/auracaster/sw_openocd.git"
OPENOCD_BRANCH="change-8818"
OPENOCD_MARKER="$OPENOCD_SRC/.last_built_commit"
OPENOCD_DIR="$PROJECT_ROOT/src/openocd"

echo "[system_update] Starting post-checkout update. project_root=$PROJECT_ROOT"

# 1. poetry install
echo "[system_update] Running poetry install..."
(cd "$PROJECT_ROOT" && "$POETRY" install)
if [ $? -ne 0 ]; then
    echo "[system_update] ERROR: poetry install failed"
    exit 1
fi

# 2. Clone/update and build sw_openocd if needed
if [ ! -d "$OPENOCD_SRC" ]; then
    echo "[system_update] Installing sw_openocd build dependencies..."
    sudo apt install -y git build-essential libtool autoconf texinfo \
        libusb-1.0-0-dev libftdi1-dev libhidapi-dev pkg-config || \
        echo "[system_update] WARNING: apt install deps had errors, continuing"
    sudo apt-get install -y pkg-config libjim-dev || \
        echo "[system_update] WARNING: apt-get install libjim-dev had errors, continuing"

    echo "[system_update] Cloning sw_openocd branch $OPENOCD_BRANCH..."
    git clone --branch "$OPENOCD_BRANCH" --single-branch "$OPENOCD_REPO" "$OPENOCD_SRC"
    if [ $? -ne 0 ]; then
        echo "[system_update] ERROR: git clone sw_openocd failed"
        exit 1
    fi
else
    echo "[system_update] Updating sw_openocd..."
    git -C "$OPENOCD_SRC" fetch origin "$OPENOCD_BRANCH"
    git -C "$OPENOCD_SRC" checkout "$OPENOCD_BRANCH"
    git -C "$OPENOCD_SRC" pull
fi

OPENOCD_COMMIT=$(git -C "$OPENOCD_SRC" rev-parse HEAD)
LAST_BUILT=""
[ -f "$OPENOCD_MARKER" ] && LAST_BUILT=$(cat "$OPENOCD_MARKER")

if [ "$OPENOCD_COMMIT" != "$LAST_BUILT" ]; then
    echo "[system_update] Building sw_openocd (commit $OPENOCD_COMMIT)..."

    (cd "$OPENOCD_SRC" && ./bootstrap)
    if [ $? -ne 0 ]; then echo "[system_update] ERROR: openocd bootstrap failed"; exit 1; fi

    (cd "$OPENOCD_SRC" && ./configure --enable-bcm2835gpio --enable-sysfsgpio)
    if [ $? -ne 0 ]; then echo "[system_update] ERROR: openocd configure failed"; exit 1; fi

    (cd "$OPENOCD_SRC" && make)
    if [ $? -ne 0 ]; then echo "[system_update] ERROR: openocd make failed"; exit 1; fi

    (cd "$OPENOCD_SRC" && sudo make install)
    if [ $? -ne 0 ]; then echo "[system_update] ERROR: openocd make install failed"; exit 1; fi

    echo "$OPENOCD_COMMIT" > "$OPENOCD_MARKER"
    echo "[system_update] sw_openocd built and installed (commit $OPENOCD_COMMIT)"
else
    echo "[system_update] sw_openocd up to date (commit $OPENOCD_COMMIT), skipping build"
fi

# 3. Flash firmware to both SWD interfaces
FLASH_SCRIPT="$OPENOCD_DIR/flash.sh"
HEX_FILE="$OPENOCD_DIR/merged.hex"

for IFACE in swd0 swd1; do
    echo "[system_update] Flashing $IFACE..."
    (cd "$OPENOCD_DIR" && bash "$FLASH_SCRIPT" -i "$IFACE" -f "$HEX_FILE")
    if [ $? -ne 0 ]; then
        echo "[system_update] ERROR: flash $IFACE failed"
        exit 1
    fi
    echo "[system_update] Flash $IFACE complete"
done

# 4. Restart services (this will kill this process too)
echo "[system_update] Restarting services..."
bash "$PROJECT_ROOT/src/service/update_and_run_server_and_frontend.sh"
