#!/bin/bash
# ==========================================================================
# Convert TTF fonts to WOFF2 for better compression
# Requires: woff2 (brew install woff2)
# ==========================================================================

set -e

FONT_DIR="$(dirname "$0")/../Resources/Public/Fonts"

# Check if woff2 is installed
if ! command -v woff2_compress &> /dev/null; then
    echo "Error: woff2 is not installed."
    echo "Install with: brew install woff2"
    exit 1
fi

cd "$FONT_DIR"

# Only convert the fonts we actually use (based on _typo.scss)
FONTS=(
    "OpenSans-Regular"
    "OpenSans-Italic"
    "OpenSans-SemiBold"
    "OpenSans-SemiBoldItalic"
    "OpenSans-Bold"
    "OpenSans-BoldItalic"
    "OpenSans-ExtraBold"
    "OpenSans-ExtraBoldItalic"
)

echo "Converting fonts to WOFF2..."
echo "Directory: $FONT_DIR"
echo ""

for font in "${FONTS[@]}"; do
    if [ -f "${font}.ttf" ]; then
        if [ -f "${font}.woff2" ]; then
            echo "  ✓ ${font}.woff2 already exists, skipping"
        else
            echo "  → Converting ${font}.ttf..."
            woff2_compress "${font}.ttf"
            echo "  ✓ ${font}.woff2 created"
        fi
    else
        echo "  ✗ ${font}.ttf not found"
    fi
done

echo ""
echo "Done! Now update _typo.scss to use WOFF2:"
echo ""
echo '  src:'
echo '      local("Open Sans Regular"),'
echo '      url("../Fonts/OpenSans-Regular.woff2") format("woff2"),'
echo '      url("../Fonts/OpenSans-Regular.ttf") format("truetype");'
