#!/bin/sh
# Render the xfwm4 window decorations from their recoloured SVG sources.
#
# Upstream renders these through Inkscape's shell mode against one big
# baseplate file; the per-button SVGs in xfwm4/assets are 1:1 with their PNGs,
# so rsvg-convert is enough and much faster. Nine frame pieces have no SVG at
# all -- src/recolor-xfwm4-png.py handles those separately.
#
# Run from the theme root:  sh src/render-xfwm4.sh

set -e
command -v rsvg-convert >/dev/null || {
    echo "rsvg-convert not found: doas apk add librsvg" >&2
    exit 1
}
[ -d xfwm4/assets ] || { echo "run me from the theme root" >&2; exit 1; }

render() {   # render <scale> <destination dir>
    scale=$1
    dest=$2
    mkdir -p "$dest"
    n=0
    for svg in xfwm4/assets/*.svg; do
        name=$(basename "$svg" .svg)
        # match the source's own dimensions so each piece keeps its geometry
        w=$(sed -n 's/.*[^-]width="\([0-9.]*\)".*/\1/p' "$svg" | head -1)
        h=$(sed -n 's/.*[^-]height="\([0-9.]*\)".*/\1/p' "$svg" | head -1)
        rsvg-convert -w "$(awk "BEGIN{printf \"%d\", $w * $scale + 0.5}")" \
                     -h "$(awk "BEGIN{printf \"%d\", $h * $scale + 0.5}")" \
                     "$svg" -o "$dest/$name.png"
        n=$((n + 1))
    done
    echo "  $dest: $n"
}

echo "rendering xfwm4 decorations"
render 1   xfwm4
render 1.5 xfwm4/Darkmatter-hdpi/xfwm4
render 2   xfwm4/Darkmatter-xhdpi/xfwm4

python3 src/recolor-xfwm4-png.py
