#! /usr/bin/env nix-shell
#! nix-shell -i bash --pure
#! nix-shell -p bash harfbuzz soupault woff2 jotdown python3 --pure

if ! [[ -d pgvv/ ]]; then
	python3 -m venv pgvv
	source ./pgvv/bin/activate
	python3 -m pip install --upgrade pip
	pip install --upgrade pygments
	deactivate
fi

function soup_config {
    rm soupault.toml
    cp ../soupault.toml soupault.toml
    cat soupault.toml.frag* >>soupault.toml
}

source ./pgvv/bin/activate

find acl.cool/site/ ytheleus.org/site/ -type f \( -name '*.dj' -o -name '*.html' \) -exec cat {} + >all_chars.txt
cat common_chars.txt >>all_chars.txt

for font in fonts/JuliaMono/*{-Light,-Regular,-SemiBold}{,Italic}.woff2; do
    woff2_decompress "$font"
    ttf_font="${font%.woff2}.ttf"

    subset_ttf="${ttf_font%.ttf}-Subset.ttf"
    hb-subset "$ttf_font" \
        --output-file="$subset_ttf" \
        --text-file=all_chars.txt \
        --layout-features='*' \
        --passthrough-tables

    woff2_compress "$subset_ttf"

    rm "$subset_ttf" "$ttf_font"
done

for font in \
    fonts/Alegreya/static/Alegreya-{Regular,Italic,Bold,BoldItalic}.ttf \
    fonts/Alegreya_Sans/AlegreyaSans-{Regular,Italic,Bold,BoldItalic}.ttf; do
    subset_ttf="${font%.ttf}-Subset.ttf"
    hb-subset "$font" \
        --output-file="$subset_ttf" \
        --text-file=all_chars.txt \
        --layout-features='*' \
        --passthrough-tables

    woff2_compress "$subset_ttf"

    rm "$subset_ttf"
done

rm css/code.css
pygmentize -f html -S algol_nu | grep -v 'line-height' >css/code.css

builtin pushd acl.cool
soup_config
soupault

NEXT_DIR="serve_$(date +%s)"
cp -a serve "$NEXT_DIR"
ln -sfn "$NEXT_DIR" serve_

popd
builtin pushd ytheleus.org
soup_config
soupault

NEXT_DIR="serve_$(date +%s)"
cp -a serve "$NEXT_DIR"
ln -sfn "$NEXT_DIR" serve_

popd

deactivate
