#!/bin/sh

#
# Clean garbage generated by texi2pdf. Note that --build=clean does not work
# properly, as it breaks relative links to the images in .texinfo file.
#

INPUT=$1
shift

clean_garbage() {
  retcode=$?
  for ext in aux cp cps fn ky log pg toc tp vr; do
    rm -f ${INPUT%.texinfo}.$ext
  done
  if [ $retcode -ne 0 ]; then
    rm -f ${INPUT%.texinfo}.pdf
  fi
  exit $retcode
}

trap clean_garbage 0 INT QUIT

"$@" "$INPUT"
