iditacards/configure.sh
2024-10-14 17:38:03 -04:00

142 lines
3.3 KiB
Bash
Executable file

#!/bin/bash
# Outputs a build.ninja script for placing into $1 (or output/ if omitted)
scriptdir="$(dirname -- "${BASH_SOURCE[0]}")"
builddir="${1-output}"
cue_sources=( "$scriptdir"/*.cue "$scriptdir"/*.yaml )
everything="$(cue export -- "${cue_sources[@]}" | jq -c)"
ASSET_TO_JSON="| to_entries[] | .value + { name: .key }"
mapfile -t assets < <(jq -c ".assets $ASSET_TO_JSON" <<<"$everything")
mapfile -t pseudos < <(jq -c ".pseudoassets $ASSET_TO_JSON" <<<"$everything")
# base is the directory that contains the script
base="$(realpath -s --relative-to="$builddir" "$scriptdir")"
# root is the directory that the assets are generated into
root=.
relative_cue_sources=( )
for cue_source in "${cue_sources[@]}"; do
relative_cue_sources+=( "$base/$cue_source" )
done
cat <<EOF
# Generated from configure.sh, do not edit this file!
base = $base
EOF
cat "$scriptdir/rules.ninja"
cat <<EOF
rule configure
description = recreate build.ninja using $base/configure.sh
command = $base/configure.sh $root
generator = 1
restat = 1
build ./build.ninja: configure | $base/configure.sh ${relative_cue_sources[*]}
build $root/.everything.json: cuegen ${relative_cue_sources[*]}
filter = .
EOF
for asset in "${assets[@]}" "${pseudos[@]}"; do
# reset asset variables, also quiets shellcheck
name=
print=
kind=
source=
template=
size=
data=
contents=
eval "$(jq -r 'to_entries[] | "\(.key)=\(.value|@text|@sh)"' <<<"$asset")"
rawsuffix="$(echo -n "$print" | sed 's/.*/raw./')"
case $kind in
template)
cat <<EOF
build $root/.$name.updated | $root/.$name.json: extract $root/.everything.json
filter = --arg asset '$name' '.assets[\$\$asset].data'
target = $root/.$name.json
build $root/tex/$name.tex: template2tex $root/.$name.json | $base/templates/$template
template = $base/templates/$template
build $root/$name.pdf: tex2pdf $root/tex/$name.tex || $root/.$name.pdf.dd
dyndep = $root/.$name.pdf.dd
build $root/.$name.pdf.dd: scantex $root/tex/$name.tex
target = $root/$name.pdf
build $root/$name.${rawsuffix}png: pdf2png $root/$name.pdf
w = ${size%x*}
h = ${size#*x}
EOF
;;
image)
cat <<EOF
build $root/$name.${rawsuffix}png: copy $base/$source
EOF
;;
tex)
cat <<EOF
build $root/$name.pdf: tex2pdf $base/$source || $root/.$name.pdf.dd
dyndep = $root/.$name.pdf.dd
build $root/.$name.pdf.dd: scantex $base/$source
target = $root/$name.pdf
build $root/$name.${rawsuffix}png: pdf2png $root/$name.pdf
w = ${size%x*}
h = ${size#*x}
EOF
;;
texdoc)
cat <<EOF
build $root/$name.pdf: tex2pdf2x $base/$source || $root/.$name.pdf.dd
dyndep = $root/.$name.pdf.dd
build $root/.$name.pdf.dd: scantex $base/$source
target = $root/$name.pdf
EOF
;;
cat)
mapfile -t ins < <(jq -r ".[]" <<<"$contents")
echo -n "build $root/$name.pdf: pdfunite"
for i in "${ins[@]}"; do
echo -n " $root/$i.pdf"
done
echo
;;
*)
echo "ERROR! UNKNOWN ASSET TYPE $kind!" >&2
;;
esac
# always run convert if print is non-empty
if [ -n "$print" ]; then
cat <<EOF
build $root/$name.png: convert $root/$name.raw.png
args = $print
EOF
fi
done