#!/usr/bin/env perl
#
# This script takes in a *.tex input file and a *.pdf output
# file and outputs a list of rules that force the output file
# to depend on any files that the input includes via a list of
# recognized TeX directives.

my @directives = qw(
  includegraphics
  lstinputlisting
);

sub usage
{
  die "Usage: texdep <input.tex> <output.pdf>\n";
}

my $tex = shift @ARGV or usage();
my $pdf = shift @ARGV or usage();

open(IN, "<", $tex) or die "Can't read '$tex': $!\n";

while (<IN>) {
  my $line = $_;
  chomp($line);
  
  foreach my $dir ( @directives ) {
    while ( $line =~ m|\\\Q$dir\E\[.*?\]{(.*?)}|g ) {
      print "$pdf: $1\n";
    }
  }
}

close(IN) or die "Can't read '$tex': $!\n";
