#!/bin/sh
# Clawdie Operator USB — session bootstrap
#
# Open one calm starting surface on login: a local START-HERE note in Mousepad.
# Operators can then save notes/edits and explicitly launch Colibri, hw-report,
# or the deeper HTML reference from the desktop.

set -eu

PAGE="${1:-/usr/local/share/clawdie-iso/START-HERE.txt}"
LOG_DIR="${XDG_CACHE_HOME:-${HOME}/.cache}/clawdie"
LOG_FILE="${LOG_FILE:-${LOG_DIR}/bootstrap.log}"

log_msg() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') $1" >> "$LOG_FILE"
}

mkdir -p "$(dirname "$LOG_FILE")"
touch "$LOG_FILE" 2>/dev/null || LOG_FILE=/dev/null

if [ ! -f "$PAGE" ]; then
    log_msg "Start note missing: $PAGE"
    exit 1
fi

for editor in /usr/local/bin/mousepad /usr/local/bin/xdg-open; do
    if [ -x "$editor" ]; then
        log_msg "Launching ${editor} ${PAGE}"
        exec "$editor" "$PAGE" >> "$LOG_FILE" 2>&1
    fi
done

log_msg "No supported editor found for ${PAGE}"
exit 1
