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

cd "$(dirname "$0")/.."

MODE="flash"
FONT=""

usage() {
  cat <<'EOF'
Usage:
  scripts/flash-font.sh [--build-only|--generate-only] <font>
  scripts/flash-font.sh --list

Fonts:
  verdana   /System/Library/Fonts/Supplemental/Verdana.ttf
  tahoma    /System/Library/Fonts/Supplemental/Tahoma.ttf
  georgia   /System/Library/Fonts/Supplemental/Georgia.ttf
  arial     /System/Library/Fonts/Supplemental/Arial.ttf

<font> may also be an explicit .ttf/.otf path.

Modes:
  default          Generate font tables, then cargo run/flash using .cargo runner.
  --build-only    Generate font tables, then build embedded target without flashing.
  --generate-only Generate font tables only.
EOF
}

list_fonts() {
  cat <<'EOF'
verdana=/System/Library/Fonts/Supplemental/Verdana.ttf
tahoma=/System/Library/Fonts/Supplemental/Tahoma.ttf
georgia=/System/Library/Fonts/Supplemental/Georgia.ttf
arial=/System/Library/Fonts/Supplemental/Arial.ttf
EOF
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --build-only)
      MODE="build"
      shift
      ;;
    --generate-only)
      MODE="generate"
      shift
      ;;
    --list)
      list_fonts
      exit 0
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      if [[ -n "$FONT" ]]; then
        echo "error: multiple fonts specified: '$FONT' and '$1'" >&2
        usage >&2
        exit 2
      fi
      FONT="$1"
      shift
      ;;
  esac
done

if [[ -z "$FONT" ]]; then
  usage >&2
  exit 2
fi

font_path_for() {
  case "$1" in
    verdana) echo "/System/Library/Fonts/Supplemental/Verdana.ttf" ;;
    tahoma) echo "/System/Library/Fonts/Supplemental/Tahoma.ttf" ;;
    georgia) echo "/System/Library/Fonts/Supplemental/Georgia.ttf" ;;
    arial) echo "/System/Library/Fonts/Supplemental/Arial.ttf" ;;
    *) echo "$1" ;;
  esac
}

FONT_PATH="$(font_path_for "$FONT")"
if [[ ! -f "$FONT_PATH" ]]; then
  echo "error: font not found: $FONT_PATH" >&2
  exit 1
fi

if ! command -v cc >/dev/null 2>&1; then
  echo "error: cc not found" >&2
  exit 1
fi

if ! command -v freetype-config >/dev/null 2>&1; then
  echo "error: freetype-config not found" >&2
  exit 1
fi

GENERATOR="/tmp/kinda-fontgen-ftmono"
GENERATOR_SOURCE="scripts/generate-fonts-ftmono.c"

if [[ ! -x "$GENERATOR" || "$GENERATOR_SOURCE" -nt "$GENERATOR" ]]; then
  echo "Building FreeType mono font generator"
  cc "$GENERATOR_SOURCE" $(freetype-config --cflags --libs) -o "$GENERATOR"
fi

echo "Generating font tables from: $FONT_PATH"
"$GENERATOR" "$FONT_PATH" src/ui/generated_fonts.rs

if command -v rg >/dev/null 2>&1; then
  rg "Source font|Rasterizer|px; cell height" src/ui/generated_fonts.rs || true
else
  grep -E "Source font|Rasterizer|px; cell height" src/ui/generated_fonts.rs || true
fi

case "$MODE" in
  generate)
    echo "Generated src/ui/generated_fonts.rs"
    ;;
  build)
    cargo build --bin kinda --target riscv32imac-unknown-none-elf
    ;;
  flash)
    cargo run --bin kinda --target riscv32imac-unknown-none-elf
    ;;
  *)
    echo "error: unknown mode: $MODE" >&2
    exit 2
    ;;
esac
