#!/bin/sh
#
# Check that the installed fonts are visible to fontconfig and that each
# family resolves to the file we ship for it.
#
# Modelled on the autopkgtests of fonts-unfonts-core and fonts-nanum. The
# idiom (assert fc-match family -> expected file) is theirs; the code here
# was written for this package.

set -eu

FONTDIR=/usr/share/fonts/truetype/cascadia-code

FILES="CascadiaCode.ttf CascadiaCodeItalic.ttf \
CascadiaCodeNF.ttf CascadiaCodeNFItalic.ttf \
CascadiaCodePL.ttf CascadiaCodePLItalic.ttf \
CascadiaMono.ttf CascadiaMonoItalic.ttf \
CascadiaMonoNF.ttf CascadiaMonoNFItalic.ttf \
CascadiaMonoPL.ttf CascadiaMonoPLItalic.ttf"

# family name -> file fc-match is expected to return for it
FAMILIES="Cascadia Code:CascadiaCode.ttf
Cascadia Code NF:CascadiaCodeNF.ttf
Cascadia Code PL:CascadiaCodePL.ttf
Cascadia Mono:CascadiaMono.ttf
Cascadia Mono NF:CascadiaMonoNF.ttf
Cascadia Mono PL:CascadiaMonoPL.ttf"

rc=0

for file in $FILES; do
    if [ ! -s "$FONTDIR/$file" ]; then
        echo "missing or empty: $FONTDIR/$file" >&2
        rc=1
        continue
    fi
    if ! fc-list -f '%{file}\n' | grep -qxF "$FONTDIR/$file"; then
        echo "not known to fontconfig: $FONTDIR/$file" >&2
        rc=1
    fi
done

echo "$FAMILIES" | while IFS=: read -r family file; do
    actual=$(fc-match -f '%{file}' "$family")
    expected="$FONTDIR/$file"
    if [ "$actual" != "$expected" ]; then
        echo "fc-match '$family' returned '$actual', expected '$expected'" >&2
        exit 1
    fi
done || rc=1

if [ "$rc" -ne 0 ]; then
    exit 1
fi

echo "fontconfig resolves all Cascadia families as expected"
