#!/usr/bin/env bash
set -euo pipefail

script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
source_root="$(cd -- "$script_dir/.." && pwd)"
if [[ -f "$source_root/experiments/hexon-lab/shell.qml" ]]; then
  app_root="$source_root"
else
  app_root="$(cd -- "$script_dir/../share/learn-omarchy" && pwd)"
fi

if [[ $# -gt 1 || "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
  echo "usage: hexon-lab [character-id]"
  echo "Preview bundled and user character packs with the production renderer (default: ohm-1)."
  exit 0
fi
for tool in qs node; do
  command -v "$tool" >/dev/null 2>&1 || {
    echo "hexon-lab: required command not found: $tool" >&2
    exit 1
  }
done
character="${1:-${CHARACTER_LAB_CHARACTER:-ohm-1}}"
if [[ ${#character} -gt 64 || ! "$character" =~ ^[a-z][a-z0-9]*(-[a-z0-9]+)*$ ]]; then
  echo "hexon-lab: invalid character ID: $character" >&2
  exit 2
fi
export CHARACTER_LAB_APP_ROOT="$app_root"
export CHARACTER_LAB_ROOT="${CHARACTER_LAB_ROOT:-$app_root/assets/characters}"
export CHARACTER_LAB_CHARACTER="$character"
# The entry point is at the repository root so Quickshell can resolve the
# lab's ../../app import without escaping its QML configuration root.
exec qs --no-duplicate --path "$app_root/character-lab.qml"
