#!/bin/sh
set -e

echo "--- DEBUG: SCRIPT STARTED ---"
pwd
mkdir -p web_fonts

# Try to locate woff2_compress
if ! command -v woff2_compress >/dev/null 2>&1; then
    echo "FATAL ERROR: woff2_compress it is not installed in the chroot."
    exit 1
fi

for font in NotoSans-Bold.ttf NotoSans-BoldItalic.ttf NotoSans-Italic.ttf NotoSans-Regular.ttf NotoSansMono-Regular.ttf; do
    echo "Searching $font..."
    file_path=$(find . -name "$font" -print -quit)
    if [ -n "$file_path" ]; then
        echo "Converting $file_path..."
        woff2_compress "$file_path"
        # Move generated woff2 to directory that dh_install waits
        mv "${file_path%.ttf}.woff2" web_fonts/ 2>/dev/null || echo "Fail to move ${font}.woff2"
    else
        echo "Not found $font"
    fi
done

echo "--- FINAL CONTENT OF web_fonts/ ---"
ls -la web_fonts/
