#!/usr/bin/perl

use strict;

# Config
my $verbose = 1;				# Verbosity level
my @yodlincs = ('/usr/e/share/yodl',		# Yodl macro dirs
		'/Users/e/share/yodl',
		'/usr/local/share/yodl',
		'/usr/share/yodl');
my $yodlinc;					# Actually found
my $progname;					# This script
my $format;					# Output format
my $input;					# Input file

# Verbose messaging
sub chat {
    print STDERR ("$progname: ", @_) if ($verbose);
}

# Usage info
sub usage () {
    die <<"ENDUSAGE";

This is yo2{html,pdf,man,manless}: Yodl Afterburner processing.
Usage: yo2{format} [FLAGS] inputfile[.yo]
Where: FLAGS
           are Yodl processing flags. Run 'yodl' without arguments
	   to see an overview.
       inputfile[.yo]
	   is the file to process. Default extension is .yo.
This processor will also include 'SiteLocal.yo' in the processing run,
if this site-specific file is found one of the directories
'@yodlincs'.

ENDUSAGE
}

# Force an extension onto a file.
sub setext ($$) {
    my $file = shift;
    my $ext  = shift;
    my $ret  = $file;

    $ret =~ s/\.[^.\/]*$//;
    $ret .= ".$ext";
    chat ("Extension $ext for $file: $ret\n");
    return ($ret);
}

# Run a system command.
sub run {
    chat ("Running: @_\n");
    my $ret = system (@_);
    my $exitval = $ret >> 8;
    my $sigval  = $ret & 127;

    die ("$progname: $_[0] caught signal $sigval\n") if ($sigval);
    die ("$progname: $_[0] stopped with $exitval\n") if ($exitval);
}

# Main starts here
# ----------------

# Determine output format and program name.
$progname = $0;
$progname =~ s{.*/}{};
$format = $progname;
$format =~ s{yo2}{};
chat ("Output format: $format\n");

# Try and locate a yodl inc dir.
for my $d (@yodlincs) {
    if (-d $d) {
	$yodlinc = $d;
	chat ("Using include directory: '$yodlinc'\n");
	last;
    }
}
chat "yodlinc: $yodlinc\n";

# Need args.
usage() if ($#ARGV == -1);

# Determine input, add site-stuff to processing
$input = pop(@ARGV);
chat ("Input file: $input\n");
if (-f "$yodlinc/SiteLocal.yo") {
    push (@ARGV, 'SiteLocal');
    chat ("Site-specific SiteLocal.yo will be included in processing\n");
}

# If we're in simple output (onestep) mode, then run it now.
if ($format eq 'html' or $format eq 'man' or $format eq 'manless') {
    chat ("Converting to $format.\n");

    # In 'man | less' mode, run yodl2man. Otherwise, run the real converter.
    if ($format eq 'manless') {
	run ("yodl2man", '-v', @ARGV, $input);
    } else {
	run ("yodl2$format", '-v', @ARGV, $input);
    }

    # For 'manless', pipe to less now.
    if ($format eq 'manless') {
	my $manf = setext ($input, 'man');
	die ("$progname: no output '$manf' found\n") unless (-f $manf);
	run ("groff -Tascii -man $manf | less");
	unlink ($manf);
    }
    exit (0);
}

# We're not in onestep output mode, it must be the LaTeX route.
# Go to the startdir of the input file.
my $dir = $input;
$dir =~ s/[^\/]*$//;
if ($dir) {
    chat ("Working directory: $dir\n");
    chdir ($dir) or die ("$progname: cannot cd to $dir: $!\n");
}

# Always run the LaTeX conversion.
chat ("Converting to LaTeX.\n");
run ('yodl2latex', '-v', @ARGV, $input);

# Now it can be only PDF processing.
chat ("Converting to PDF.\n");
my $ltx = setext ($input, 'latex');
my $pdf = setext ($input, 'pdf');
run ('pdflatex', $ltx, $pdf);
run ('pdflatex', $ltx, $pdf);
run ('pdflatex', $ltx, $pdf);

# Any unresolved references?
my $log = setext ($input, 'log');
if (open (my $if, $log)) {
    while (<$if>) {
	die ("$progname: ", $_) if (/There were undefined references/);
    }
    close ($if);
}

# unlink ($ltx);
