#! /bin/bash
# Last edited on 2024-03-31 19:08:08 by stolfi

usage="$0 JOBNAME... > LIST.txt"

# Reads the names of one or more TeX/LaTeX jobs, and
# outputs all Postscript figures (".ps" or ".eps") included by them.
# Assumes the jobs generated ".dvi" output.
# 
# The Postscript files successfully included in the document are
# obtained from the file "{JOBNAME}.dvi" (specifically, from the
# report that "dvips" writes to "stderr"). Also scans through
# "{JOBNAME}.log" for Postscript files that are requested by the
# source document but were not found by.
# 
# Since it works by parsing the diagnostic output of other programs,
# it is not guaranteed to work in all cases.
# 
# Removes any initial "./" prefix from file names. Lists the files are
# in order of their appearance in the DVI file, then in the error log
# file; and each file is listed as many times as it is included. To
# remove such duplications, pipe the output through "sort | uniq".

jobs=( "$@" )

for job in ${jobs[@]} ; do
  if [[ -e ${job}.dvi ]]; then
    ( dvips -o /dev/null ${job}.dvi 2>&1 ) \
      |  sed -e 's/</ </g' -e 's/>/> /g' \
      | tr ' ' '\012' \
      | egrep -e '[.][eE]?[Pp][Ss][>]' \
      | tr -d '<>' \
      | sed -e 's:^[.][\/]::g' 
  else
    echo "${job}.dvi not found" 1>&2 
  fi
  if [[ -e ${job}.log ]]; then
    ( cat ${job}.log ; echo '|' ) \
      | tr '\012|' ' \012' \
      | sed -e 's:I[ ]*couldn.t[ ]*open[ ]*\([^ ]*[.][Ee]*[Pp][Ss]\): <\1> :g' \
      | tr ' ' '\012' \
      | egrep -e '^[<].*[.][eE]?[Pp][Ss][>]' \
      | tr -d '<>' \
      | sed -e 's:^[.][\/]::g' 
  else
    echo "${job}.log not found" 1>&2 
  fi
done

# echo "DONE"
