#!/usr/bin/perl

# gprocess
# a script for 'compiling' gregorio gabc music files
# v0.1
# Copyright (C) 2008 Richard Chonak <chonak@yahoo.com>
# 

# 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 3 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.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


use strict;

## USER-MODIFIABLE COMMAND PATHS

# search for a PDF viewer among these file paths; take the first
my @PDFVIEWERS = grep { -e $_ && -x $_ } 
   ("/usr/bin/kpdf","/usr/bin/okular","/usr/bin/acroread");
my $PDFVIEWER = $PDFVIEWERS[0];

my $TEXCOMPILER = "/usr/bin/lamed";
my $DVIPDFCONVERTER = "/usr/bin/dvipdfm";
my $PDFTK = "/usr/bin/pdftk";

## END OF USER-MODIFIABLE COMMAND PATHS


# print usage string if no command-line arg specified

if ($#ARGV < 0) {
  die "usage: $0 <gabc-file-name>\n";
}


my $GABCFILE = $ARGV[0];
my $SCORENAME; 
my $ANNOTATION;
my $REFERENCE;
my $OFFICEPART;
my $NAMEFOUND = 0;
my $KEEPSEARCHING = 1;
my $INLINE = "";


my $SCORETEXFILE = $GABCFILE;
$SCORETEXFILE =~ s/gabc$/tex/;

my $SCOREWRAPTEX = $SCORETEXFILE;
$SCOREWRAPTEX =~ s/.tex$/-main.tex/;

####  GET THE SCORE TITLE FROM THE GABC FILE ####

if ($GABCFILE =~ m#/#) {
   die "File spec must not contain slashes: $GABCFILE\n";
};


open (GABC,"<".$GABCFILE) or die "No such file: $GABCFILE\n";
while ($KEEPSEARCHING == 1 && ($INLINE = <GABC>)) {
  if ($INLINE =~ /^name/) {
    $SCORENAME = $INLINE;
    $SCORENAME =~ s/^.*:\s*//;
    $SCORENAME =~ s/\s*;\s*$//;
  };
  if ($INLINE =~ /^anotation/) {
    $ANNOTATION = $INLINE;
    $ANNOTATION =~ s/^.*:\s*//;
    $ANNOTATION =~ s/\s*;\s*$//;
  };
  if ($INLINE =~ /^reference/) {
    $REFERENCE = $INLINE;
    $REFERENCE =~ s/^.*:\s*//;
    $REFERENCE =~ s/\s*;\s*$//;
  };
  if ($INLINE =~ /^office-part/) {
    $OFFICEPART = $INLINE;
    $OFFICEPART =~ s/^.*:\s*//;
    $OFFICEPART =~ s/\s*;\s*$//;
  };
  if ($INLINE =~ /^%%/) { $KEEPSEARCHING = 0; }
}

close (GABC);



####  READ IN THE TEMPLATE  ####

my @TEMPLATELINES;


@TEMPLATELINES = <DATA>;



####  MAKE SUBSTITUTIONS ####

&do_subst ("XXXX-SCORENAME-XXXX", $SCORENAME);
&do_subst ("XXXX-SCORETEXFILE-XXXX", $SCORETEXFILE);
&do_subst ("XXXX-ANNOTATION-XXXX", $ANNOTATION);
&do_subst ("XXXX-OFFICEPART-XXXX", $OFFICEPART);
&do_subst ("XXXX-REFERENCE-XXXX", $REFERENCE);

foreach (@TEMPLATELINES){
  s/^.*XXXX.*$//;
};

####  WRITE TEX WRAPPER FILE ####

open (TEXWRAP,">".$SCOREWRAPTEX) or die "Cannot write file $SCOREWRAPTEX\n";
print TEXWRAP @TEMPLATELINES; 
close (TEXWRAP);

#### RUN GREGORIO, LATEX, AND KPDF ####
my $DVIFILE = $SCOREWRAPTEX;
$DVIFILE =~ s/tex$/dvi/;
my $PDFFILE = $SCOREWRAPTEX;
$PDFFILE =~ s/tex$/pdf/;

# remove product files so that we don't accidentally view results
# from a prior run
unlink $SCORETEXFILE;
unlink $DVIFILE;
unlink $PDFFILE;

do_cmd("gregorio $GABCFILE");
do_cmd("$TEXCOMPILER $SCOREWRAPTEX");
do_cmd ("$DVIPDFCONVERTER $DVIFILE");
if ( -e $PDFTK && -x $PDFTK ) {
	my $TMPFILE = $$ . ".pdf";
	rename $PDFFILE, $TMPFILE;
	printf "Reducing size of $PDFFILE...\n";
	do_cmd ("$PDFTK $TMPFILE output $PDFFILE");
	unlink $TMPFILE;
};
do_cmd ("$PDFVIEWER $PDFFILE");

my $AUXFILE = $DVIFILE;
$AUXFILE =~ s/dvi$/aux/;

unlink $DVIFILE or warn "Could not delete temporary file: $DVIFILE\n";
unlink $AUXFILE or warn "Could not delete temporary file: $AUXFILE\n";

print "\n\nOutput is in $PDFFILE .\n";
exit;

sub do_subst {
  my $TAG = $_[0];
  my $VAL = $_[1];

  foreach (@TEMPLATELINES) {
     s/$TAG/$VAL/g;
  }
};

sub do_cmd {
	my $CMD = $_[0];
#debugging	print "Doing command: \n  $CMD\n\n";
	system ($CMD) == 0 
	   or die "\n\nSystem command failed: $CMD : $?\n";
};


#  THE LINES AFTER THIS "END" TAG ARE A TEMPLATE FOR THE TEX FILE TO BE GENERATED
__END__

\documentclass[12pt, letterpaper]{article}
\usepackage{fullpage}
\usepackage{palatino}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{gregoriotex}
\pagestyle{empty}
\begin{document}

\begin{center}\begin{huge}\textsc{XXXX-SCORENAME-XXXX}\end{huge}\end{center}

\setspaceafterinitial{3.2mm plus 0em minus 0em}
\setspacebeforeinitial{3.2mm plus 0em minus 0em}

\def\greinitialformat#1{%
{\fontsize{43}{43}\selectfont #1}%
}

\setfirstlineaboveinitial{\small \textsc{\textbf{XXXX-ANNOTATION-XXXX}}}{\small \textsc{\textbf{XXXX-ANNOTATION-XXXX}}}
\commentary{{\small \emph{XXXX-REFERENCE-XXXX}}}
\setgrefactor{17}
\spaceabovelines=5mm
\includescore{XXXX-SCORETEXFILE-XXXX}

\end{document}


