#!/bin/sh

# The App Store Lite product retains Signal's icon font for generic messaging
# controls, but it must not carry usable Signal logo or "official" badge
# glyphs. Subset only the copied framework resources so upstream source fonts
# and non-App-Store builds remain unchanged.

set -eu

[ "${CONFIGURATION:-}" = "App Store Release" ] || exit 0

expected_harfbuzz_version="hb-subset (HarfBuzz) 14.2.0"

if [ -n "${KIVO_HB_SUBSET:-}" ]; then
  hb_subset="$KIVO_HB_SUBSET"
elif command -v hb-subset >/dev/null 2>&1; then
  hb_subset="$(command -v hb-subset)"
elif [ -x /opt/homebrew/bin/hb-subset ]; then
  hb_subset=/opt/homebrew/bin/hb-subset
elif [ -x /usr/local/bin/hb-subset ]; then
  hb_subset=/usr/local/bin/hb-subset
else
  echo "error: App Store Lite requires HarfBuzz 14.2.0 (hb-subset). Install the pinned tool before archiving." >&2
  exit 1
fi

if [ -n "${KIVO_HB_SHAPE:-}" ]; then
  hb_shape="$KIVO_HB_SHAPE"
elif [ -x "$(dirname "$hb_subset")/hb-shape" ]; then
  hb_shape="$(dirname "$hb_subset")/hb-shape"
elif command -v hb-shape >/dev/null 2>&1; then
  hb_shape="$(command -v hb-shape)"
else
  echo "error: App Store Lite requires hb-shape from the same pinned HarfBuzz toolchain." >&2
  exit 1
fi

actual_harfbuzz_version="$($hb_subset --version | sed -n '1p')"
if [ "$actual_harfbuzz_version" != "$expected_harfbuzz_version" ]; then
  echo "error: expected $expected_harfbuzz_version, found $actual_harfbuzz_version" >&2
  exit 1
fi

actual_hb_shape_version="$($hb_shape --version | sed -n '1p')"
if [ "$actual_hb_shape_version" != "hb-shape (HarfBuzz) 14.2.0" ]; then
  echo "error: expected hb-shape (HarfBuzz) 14.2.0, found $actual_hb_shape_version" >&2
  exit 1
fi

font_dir="${TARGET_BUILD_DIR:?}/${UNLOCALIZED_RESOURCES_FOLDER_PATH:?}"
temp_dir="${TARGET_TEMP_DIR:?}/KivoSignalSymbolSubset"
mkdir -p "$temp_dir"

# UTF-8 for U+E000 (Signal logo), U+E086 (upstream official badge), and
# three generic sentinel glyphs that must remain usable after subsetting.
signal_logo_scalar="$(printf '\356\200\200')"
official_badge_scalar="$(printf '\356\202\206')"
sentinel_scalars="$(printf '\356\200\201\356\201\243\356\201\260')"

for font_name in \
  SignalSymbols-Regular.otf \
  SignalSymbols-Light.otf \
  SignalSymbols-Bold.otf
do
  font_path="$font_dir/$font_name"
  subset_path="$temp_dir/$font_name.subset"
  repeat_path="$temp_dir/$font_name.repeat"

  if [ ! -f "$font_path" ]; then
    echo "error: expected App Store Lite symbol font is missing: $font_path" >&2
    exit 1
  fi

  source_logo_shape="$($hb_shape "$font_path" "$signal_logo_scalar")"
  source_official_shape="$($hb_shape "$font_path" "$official_badge_scalar")"
  if printf '%s\n%s\n' "$source_logo_shape" "$source_official_shape" | grep -q '\.notdef='; then
    echo "error: $font_name no longer matches the reviewed source-font glyph contract" >&2
    exit 1
  fi

  "$hb_subset" "$font_path" \
    '--unicodes=*' \
    '--unicodes-=E000,E086' \
    '--name-IDs=*' \
    '--name-languages=*' \
    '--layout-features=*' \
    --no-layout-closure \
    --output-file="$subset_path"
  "$hb_subset" "$font_path" \
    '--unicodes=*' \
    '--unicodes-=E000,E086' \
    '--name-IDs=*' \
    '--name-languages=*' \
    '--layout-features=*' \
    --no-layout-closure \
    --output-file="$repeat_path"

  if [ ! -s "$subset_path" ] || ! cmp -s "$subset_path" "$repeat_path"; then
    echo "error: deterministic symbol-font subsetting failed for $font_name" >&2
    exit 1
  fi

  subset_logo_shape="$($hb_shape "$subset_path" "$signal_logo_scalar")"
  subset_official_shape="$($hb_shape "$subset_path" "$official_badge_scalar")"
  subset_sentinel_shape="$($hb_shape "$subset_path" "$sentinel_scalars")"
  if ! printf '%s\n' "$subset_logo_shape" | grep -q '\.notdef=' || \
    ! printf '%s\n' "$subset_official_shape" | grep -q '\.notdef=' || \
    printf '%s\n' "$subset_sentinel_shape" | grep -q '\.notdef='
  then
    echo "error: $font_name retained a blocked glyph or lost a generic sentinel glyph" >&2
    exit 1
  fi

  cp "$subset_path" "$font_path"
done
