#!/bin/bash

# ideone boilerplate - we can't write files in the home directory;
# so create a temporary directory for our files instead
t=$(mktemp -d -t ideone.XXXXXXXXXXXX) || exit
trap 'rm -rf "$t"' ERR EXIT
cd "$t"

svg_name=template
pdf_base_name=pdf
image_width=1024
cat <<\: >template.svg
<svg>
  <title>Sorry, I don't remember what SVG looks like</title>
  <box>Certificate for $name_placeholder</box>
  <circle>* E pluribus unum *</circle>
  <triangle>Join the Illuminati! <eye/> </triangle>
  <box>This is to certify that
     <center><cursive><large>$name_placeholder</large></cursive></center>
     has completed their studies at
     <center><bombastic>$cole_placeholder</bombastic></center>
     with a degree of $dni_placeholder.
   </box>
  <text>Pamplona, $date_placeholder</text>
  <signature>Bugs Bunny, Esq.</signature>
</svg>
:

inkscape () {
   # just a placeholder to show that it was run
   tr A-Za-z a-zA-Z <"$1" >"${3#--export-filename=}"
}

tmp=$(mktemp -t svg2pdf.XXXXXXXXXX) || exit
#trap 'rm -f "$tmp"' EXIT   # don't override the ideone trap above

while IFS=":" read -r name cole dni date; do
    # write diagnostics to standard error; include name of script
    echo "$0: $name" >&2
    # run sed just once, replaces cp too
    sed -e "s/\$name_placeholder/${name// /\\ }/" \
        -e "#s/\$cole_placeholder/$cole/" \
        -e "s/\$dni_placeholder/${dni// /\\ }/" \
        -e "s/\$date_placeholder/$date/" "$svg_name.svg" >"$tmp"
    inkscape "$tmp" --export-area-page \
        --export-filename="$pdf_base_name\ ${cole// /_}\ ${name// /_}.pdf" \
        --export-width=$image_width --export-type="pdf"
done

ls -la
nl *.pdf
rm -f "$tmp"