#!/usr/bin/perl
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#NAME
#  mkprog - make a program
#
#SYNOPSIS
#  mkprog [options] name page...
#
#DESCRIPTION
#  This is a tool that helps make a program of tune sets out  of  collections
#  of  ABC  files.   This  is used in Makefiles to simplify the entries for a
#  program of music.
#
#  The name argument is a name for the program.  We first create a  directory
#  by that name. If there is a "program" directory, we create a program/$name
#  directory, otherwise we create the ./$name directory.
#
#  The  rest of the args are the names of sets.  There should be Makefile entires
#  for each of the dances, to create the dance.abc,  dance.ps  and  dance.pdf
#  files.  We run through the list doing a "make $dance" for each dance name.
#  Then we link the resulting files into the program's directory.
#
#OPTIONS
#  Options can start with '+' (for "yes" or "enable") or  '-'  (for  "no"  or
#  "disable).   All the options are a single letter, and they may be combined
#  into a single string if you like.  The case doesn't matter.  Some  options
#  may  be  followed by a numeric argument, with a default used if there's no
#  number.  If we show only a '-' option, then '+' means the same thing.
#
#
#  +I   "Informative". Don't actually make files, just say what would be done.
#  -I   Do everything.  This is the default.
#
#  -P   "Print"  After making sure that all the files exist, they are sent to
#       the default printer.
#
#  -R   Reverse the print order, printing the last dance's files first.
#
#  +Sn  Sleep between prints.  The default is a 5-second sleep.
#  -S   Don't sleep between prints.
#
#  +T   Link .abc files to .txt files, for browsers that don't grok .abc.
#  -T   Don't link .abc files to .txt files.  [default]
#
#  -Vn  sets the verbose level to n.  Without a number, +V increments and -V
#       decrements the default verbose level, which is usually 1, but may be
#       higher for debugging purposes.
#
#AUTHOR John Chambers <jc@trillian.mit.edu>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$| = 1;
($P = $0) =~ s".*/"";
$V = $ENV{"V_$P"} || 1;

$Iopt  = 0;		# Information only, don't make or print
$Popt  = 0;		# Print if true
$Ropt  = 0;		# Reverse the page order
$Sopt  = 5;		# Delay between lpr commands
$task = 'Make';	# Make or Prnt
$lnabc = 1;		#  Link source file as .abc
$lntxt = 0;		#  Link source file as .txt

$name = shift;
if (($flg,$opts) = ($name =~ /^([-+])(.*)$/)) {
	print "$0: Opts \"$opts\" (flg='$flg')\n" if $V>1;
	while (($opt,$opts) = ($opts =~ /^(.)(.*)$/)) {
		if (uc($opt) eq 'A') {
			$lnabc = $flg eq '+' ? 1 : 0;
			print("$0: lnabc=$lnabc (" . ($lnabc ? '' : "don't ") . "link source file to .abc)\n") if $V>0;
		} elsif (uc($opt) eq 'I') {
			$Iopt = ($flg eq '+') ? 1 : 0;
			print "$0: Iopt=$Iopt (information only)\n" if $V>0;
		} elsif (uc($opt) eq 'P') {
			++$Popt;
			print "$0: Popt=$Popt (print)\n" if $V>1;
		} elsif (uc($opt) eq 'R') {
			$Ropt = ($flg eq '-') ? 0 : 1;
			print "$0: Ropt=$Ropt (reverse order)\n" if $V>0;
		} elsif (uc($opt) eq 'S') {
			if ($opts =~ s/^(\d+)//) {
				$Sopt = $1;	# Sleep time after lpr command
			} else {
				$Sopt ++;	# Incr lpr sleep time
			}
			print "$0: Sopt=$Sopt (sleep between sets)\n" if $V>1;
		} elsif (uc($opt) eq 'T') {
			$lntxt = $flg eq '+' ? 1 : 0;
			print("$0: lntxt=$lntxt (" . ($lntxt ? '' : "don't ") . "link source file to .txt)\n") if $V>0;
		} elsif (uc($opt) eq 'V') {		# Is number included?
			if ($opts =~ s/^(\d+)//) {	# Set $V to that value
				$V = int($1);
			} else {		# Decrement or increment $V according to flag
				$V += ($flg eq '-') ? -1 : 1;
			}
			print "$0: V=$V (verbose level)\n" if $V>0;
		} else {
			print STDERR "$0: Unknown option '$opt' ignored.\n" if $V>0;
		}
		print "$0: opts \"$opts\" (flg='$flg')\n" if $V>1;
	}
	$name = shift;
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Directory kludge: If we have a "program" subdirectory, we create the target #
# directory there.  Otherwise, we create it under the current directory.      #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
if (-d "program") {
	$pdir = "program/$name";
} else {
	$pdir = "$name";
}
unless (-d $pdir) {
	print "$P: Make dir $pdir/\n" if $V>1;
	mkdir $pdir, 0755;
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Now we run through the list of dance pages, making sure each is up to date, #
# and linking each one's files into the program directory.                    #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
while ($dance = ($Ropt ? pop @ARGV : shift @ARGV)) {
	print "---	$task $dance ...\n" if $V>0;
	$abc = $lnabc ? "$dance.abc" : '';
	$txt = $lntxt ? "$dance.txt" : '';
	$ps  = "$dance.ps";
	$pdf = "$dance.pdf";
	@mkout = $Iopt ? `make -k $abc $ps $pdf` : ();
	print @mkout if $?;
	if ($?) {
		print STDERR "$P: Problems with $dance.\n" if $V>0;
	}
	if (-f "$abc") {
		unlink("$pdir/$abc") if -f "$pdir/$abc";
		print "$P: Link $abc to $pdir ...\n" if $V>1;
		unlink("$pdir/$abc") if -f "$pdir/$abc" && $lnabc;
		unlink("$pdir/$txt") if -f "$pdir/$txt" && $lntxt;
		link("$abc","$pdir/$abc") if $lnabc;	# Link in the ABC source file
		link("$abc","$pdir/$txt") if $lntxt;	# Also provide the ABC as "plain text"
	} else {
		print "$P: ABC file '$abc' does not exist.\n" if $V>0;
		if (@files = glob("$dance*.abc")) {
			print "\tThese files exist:\n" if $V>0;
			system 'ls', '-ld', @files;
		}
	}
	if (-f $ps) {
		unlink("$pdir/$ps") if -f "$pdir/$ps";
		print "$P: Link $ps to $pdir ...\n" if $V>1;
		link("$ps","$pdir/$ps");
		if ($Popt) {
			print "$P: Print $dance ...\n" if $V>1;
			system "lpr $pdir/$ps" unless $Iopt;
			sleep $Sopt if $Sopt>0;
		}
	} else {
		print "$P: PS file '$ps' does not exist.\n" if $V>0;
		if (@files = glob("$dance*.ps")) {
			print "\tThese files exist:\n" if $V>0;
			system 'ls', '-ld', @files;
		}
	}
	if ( -f $pdf) {
		unlink("$pdir/$pdf") if -f "$pdir/$pdf";
		print "$P: Link $pdf to $pdir ...\n" if $V>1;
		link($pdf,"$pdir/$pdf") unless $Iopt;
	} else {
		print "$P: PDF file '$pdf' does not exist.\n" if $V>0;
		if (@files = glob("$dance*.pdf")) {
			print "\tThese files exist:\n" if $V>0;
			system 'ls', '-ld', @files;
		}
	}
}
