#!/usr/bin/perl -w

use strict;

sub run($) {
    my $inp = shift;

    my $stem = $inp;
    $stem =~ s/\.ly//;

    my $fh;
    my $outp = "${stem}-systems.tex";
    open($fh, ">", $outp) || die("cannot open $outp for writing");
    for (my $ix = 1; -f "${stem}-$ix.eps"; $ix++) {
	my $name = "${stem}-$ix";
	print $fh
"\\includegraphics{$name}%
\\ifx\\betweenLilyPondSystem \\undefined
  \\linebreak
\\else
  \\expandafter\\betweenLilyPondSystem{$ix}%
\\fi
";
    }
    close($fh);
}

sub main() {
    for (@ARGV) {
	run($_);
    }
}

main();
