#!/usr/bin/perl

# gprocess
# a script for 'compiling' gregorio gabc music files
# v0.2
# Copyright (c) 2010 Richard Chonak <chonak@yahoo.com>
# Changes in v0.2:
# -- change list of PDF viewers
# -- invoke LuaLateX instead of lamed/dvipdfm
# -- drop use of pdftk (it's not available for my system)
# -- use latin1 character set instead of utf8
# -- update macro and variable names (with "gre" prefix) as needed
# -- add 'redlines'
# -- 
#
# 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/local/bin/acroread","/usr/bin/acroread");
my $PDFVIEWER = $PDFVIEWERS[0];

my $TEXCOMPILER = "lualatex";

## 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 $PDFFILE = $SCOREWRAPTEX;
$PDFFILE =~ s/tex$/pdf/;
my $AUXFILE = $SCOREWRAPTEX;
$AUXFILE =~ s/tex$/aux/;
my $GAUXFILE = $SCOREWRAPTEX;
$GAUXFILE =~ s/tex$/gaux/;

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


do_cmd("gregorio $GABCFILE");
do_cmd("$TEXCOMPILER $SCOREWRAPTEX");
unlink $AUXFILE;
unlink $GAUXFILE;

print "\n\nOutput is in $PDFFILE .\n";
do_cmd ("$PDFVIEWER $PDFFILE");


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[T1]{fontenc}
\usepackage[utf8]{luainputenc}
\usepackage{palatino}
\usepackage{gregoriotex}
\usepackage{color}
\usepackage{ifthen}
\pagestyle{empty}
\begin{document}
\redlines

\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}%
}

\gresetfirstlineaboveinitial{\small \textsc{\textbf{XXXX-ANNOTATION-XXXX}}}{\small \textsc{\textbf{XXXX-ANNOTATION-XXXX}}}
\commentary{{\small \emph{XXXX-REFERENCE-XXXX}}}

\setgrefactor{17}
\grespaceabovelines=5mm
\includescore{XXXX-SCORETEXFILE-XXXX}

\end{document}


