#!/usr/bin/perl -w
use strict;
my %inc;
my @Inc = (".");
sub Usage() {
print "Usage:
depend_ly [-I
[:]...] ext ly-file(s)
Where
is where to find included ly-files
ext is the file extension which your lilypond run should generate
e.g.: pdf, ps, eps
";
exit 1;
}
sub file_exist($) {
my $file = shift;
my $inc;
foreach (@Inc) {
if ($_ eq ".") {
$inc = $file;
} else {
$inc = "$_/$file";
}
if (stat $inc ) {
return $inc;
}
}
return 0;
}
sub ly_inc($) {
my $file = shift;
my $fh;
my %lst = (); # which files to recursively check
# no file no dependancies
open($fh, "<", $file) || return;
while(<$fh>) {
s/%.*$//;
# this breaks for multiple \include per line
m/\\include[ \t]+\"([^\"]+)\"/ || next; #"
my $inc_file = $1;
# consider only existing files as dependancies
$inc_file = file_exist $inc_file || next;
# only add this file to %lst if we have not checked it for dependancies
if (!$inc{$inc_file}) { $lst{$inc_file} = 1; }
# add this as an dependancy
$inc{$inc_file} = 1;
}
foreach (keys(%lst)) {
&ly_inc($_);
}
}
if (@ARGV < 2) { Usage(); }
my $ext = shift @ARGV;
if ($ext =~ m/^-I(.*)$/) {
push @Inc, split(/:/, $1);
$ext = shift @ARGV;
}
my $file;
foreach $file (@ARGV) {
# reset found dependancies for each new file to check
%inc = ();
&ly_inc($file);
my $stem = $file;
$stem =~ s/\.ly$//;
my $prefix = "$stem.$ext";
if ($ext eq "eps") {
# more than one file is generated for the eps backend
$prefix .= " $stem-systems.tex";
}
my $lst = join(" ", sort(keys(%inc)));
print "$prefix:\t$file ", $lst, "\n";
}