#!/usr/bin/perl -w
#
# This musixtex Perl script automates the three-pass TeX compilation
# of a MusiXTeX music score file.  It basically runs the command:
#   $ tex filename.tex ; musixflx filename ; tex filename.tex
#
# Adapted by Anthony Fok <foka@debian.org> for Debian GNU/Linux
# from the DOS batch file in the MusiXTeX User Manual by MusiXTeX's authors.
# Improvements copyright 2003, Julian Gilbey <jdg@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# Initial version:  Mon, 14 Jul 1997 07:15:00 -0600
#   Last modified:  Fri, 8 Aug 2003

use strict;
use File::Basename;

my ($progname, $package, $pkg_name, $flex, $texprog);

$progname = basename($0);
$package = "musixtex";
$flex = "musixflx";
$texprog = "tex";

sub usage($) {
    my $status = $_[0];
    my $fh = ($status == 0 ? *STDOUT : *STDERR);
    print $fh <<"EOF";
This script automates the three-pass TeX compilation of MusiXTeX
music score files.  Note: This script may not work for certain files.
You may run tex/latex and $flex manually in such cases.

Usage: $progname file.tex [TeX options]

For more information, please refer to /usr/share/doc/$package/README.Debian.
EOF
    exit($status);
}

sub version() {
    print <<"EOF";
This is $progname, from the Debian $package package.
Adapted by Anthony Fok <foka\@debian.org> for Debian GNU/Linux                 
from the DOS batch file in the MusiXTeX User Manual by MusiXTeX\'s authors.    
Improvements copyright 2003, Julian Gilbey <jdg\@debian.org>                   

This program comes with ABSOLUTELY NO WARRANTY.
You are free to redistribute this code under the terms of the
GNU General Public License, version 2 or later.
EOF
    exit(0);
}

# Main program

if ($#ARGV == -1) { usage(1) };

my ($filename, $dirname, $basename, $suffix);

$filename = shift @ARGV;
if ($filename =~ /^--?help$/) { usage(0) };
if ($filename =~ /^--?version$/) { version() };

if (! -e $filename) {
    if (-e "$filename.tex") { $filename .= '.tex'; }
    else { die "TeX file $filename not found"; }
}
-r $filename || die "TeX file $filename not readable";

open(FILE, $filename) || die "Error while opening config file $filename" ;
while ( <FILE> ) {
  if ( /\\documentclass/ ) {
    $texprog = "latex" ;
    last;
  }
}
close(FILE) || die "Error while closing file $filename" ;

($basename, $dirname, $suffix) = fileparse($filename, '\.tex');

# Note: TeX writes its output to the current directory.
#       Therefore, we use musixflex to process the file in the
#       current directory.

# remove them if they exist
unlink("$basename.mx1", "$basename.mx2");

system($texprog, @ARGV, $filename) == 0
    or die "First pass of TeX failed on $filename: $!";

system($flex, $basename) == 0
    or die "$flex failed to process $basename.mx1: $!";

system($texprog, @ARGV, $filename) == 0
    or die "Second pass of TeX failed on $filename: $!";

exit(0);
