#!/usr/bin/perl # wpdf -- view, watch and remake pdf based on Makefile use strict; use warnings; my %deptarget; # dependency => target # collect dependencies for given pdf files for my $target (@ARGV) { if ($target =~ /\.pdf$/) { # try to directly parse out dependencies open my $f, '<', 'Makefile' or die "could not open Makefile: $!\n"; while (<$f>) { next if not /^\Q$target\E:\s*(.*)/; $deptarget{$_} = $target for split /\s/, $1; goto found; } # try to get implicit dependencies (my $esc = $target) =~ s/'/'"'"'/g; open my $p, "make -ndv 2>&1 '$esc' |" or die "could not run make: $!\n"; while (<$p>) { next if not /^\Q$target\E:< = (.*)/; $deptarget{$_} = $target for split /\s/, $1; last; } close $p; found: close $f; exec('xpdf', '-remote', "wpdf-$target", $target) if fork == 0; next; } else { warn "skipping $_: not a pdf\n"; } } # make pdf on demand open my $p, '-|', 'watch', keys %deptarget or die "could not start watch: $!\n"; while (<$p>) { chomp; system('make', "$deptarget{$_}") == 0 && system('xpdf', '-remote', "wpdf-$deptarget{$_}", '-reload'); } close $p; exit($? != 0);