#!/usr/bin/perl -w
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#NAME
#  abcMakeSet - make "tune set" from abc tunes
#
#SYNOPSIS
#  abcMakeSet setname abcfile...
#
#REQUIRES
#
#DESCRIPTION
#  Produce a Makefile entry to make a named "set" of tunes in ABC music
#  notation.
#
# The setname arg should be a series of words separated by '_' that will
# have ".abc" appended to give the set's filename. They will be added to
# the upper corner of the .ps and .pdf pages with underscores converted
# to spaces, as an aid in locating the set within a binder of tune-set
# pages.
#
#OPTIONS
# None yet, but there may be some soon.
#
#EXAMPLES
#
#FILES
#
#BUGS
#
#SEE ALSO
#
#AUTHOR
# John Chambers <jc@trillian.mit.edu> 2019
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$| = 1;
$exitstat = 0;
($P = $0) =~ s".*/"";
$V = $ENV{"V_$P"} || $ENV{"D_$P"} || 2;	# Verbose level.

@msgs    = ();		# Line bufer for output to client
$genHdr  = 0;		# Should we generate a header file?
$genFtr  = 0;		# Should we generate a footer file?
$setName = '';		# Name of set entry
@files   = ();		# Files to build set from

#equire "Backup.pm";
#equire "DT.pm";

#sendmsgs();	# Send all the accumulated lines to the client

arg:
for $arg (@ARGV) {
	print STDERR "$P: arg=\"$arg\"\n" if $V>2;
	if (($flg,$nam) = ($arg =~ /^([-+])(.*)$/)) {
		print STDERR "$P: flg=\"$flg\" nam=\"$nam\"\n" if $V>2;
#
		if      (uc($nam) eq 'F') {	# Generate footer file
			print STDERR "$P: Generate ftr file.\n" if $V>2;
			$genFtr = $flg ? 1 : 0;
		} elsif (uc($nam) eq 'H') {	# Generate header file
			print STDERR "$P: Generate hdr file.\n" if $V>2;
			$genHdr = $flg ? 1 : 0;
		} else {
			print STDERR "$P: Unknown option \"$arg\"\n" if $V>0;
		}
		next arg;
	}
	if (!$setName) {	# Get the set name
		$setName = $arg;
		print STDERR "$P: setName=\"$setName\"\n" if $V>2;
		next arg;
	}
	if (-f $arg) {	# Is this a file?
		push @files, $arg;
		print STDERR "$P: File:=\"$arg\"\n" if $V>1;
		next arg;
	}
	print "$P: Arg \"$arg\" unknown.\n" if $V>0;
}
print STDERR "$P: setName=\"$setName\" genHdr=$genHdr getFtr=$genFtr\n" if $V>0;

($setwords = $setName) =~ s/_+/ /g;
print "\n";
print "# $setwords\n";
print "$setName: $setName.abc $setName.pdf\n";
print "$setName= hdr/$setName.hdr \\\n";
print "\tftr/$setName.ftr\n";
print "$setName.abc: \$(" . $setName .")\n";
print "\tabcjoin +C +RS \$(" . $setName .") >$setName.abc\n";
print "#	ln -f $setName.abc set/\n";
print "$setName.ps: $setName.abc fmt/_75.fmt\n";
print "\t\$(a2p) $setName.abc +Ffmt/_75.fmt \\\n";
print "\t|PShdr '$setwords' '' '$setwords' \\\n";
print "\t|PSftr '%P' \$U/ '%D' >$setName.ps\n";
print "$setName.pdf: $setName.ps\n";
print "\tps2pdf $setName.ps $setName.pdf\n";
print "\tln -f $setName.pdf img/\n" if -d 'img';	# Only if the 'img' directory exists
print "hdr/$setName.hdr: ;touch hdr/$setName.hdr\n";
print "ftr/$setName.ftr: ;touch ftr/$setName.ftr\n";
print "\n";

exit $exitstat;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

sub max {my $F='max';
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	local($arg,$num,$val);
	for $arg (@_) {
		if ($arg =~ /(-*)(\d+).(\d*)/) {
			$num = $1 . $2 . '.' . $3;
		} elsif ($arg =~ /(-*\d*)/) {
			$num = int($1);
		} else {
			push @msgs, "$F: \"$arg\"<br>\n" if $V>0;
		}
		if (defined($val)) {$val = $num if $num > $val} else {$val = $num}
	}
	return $val;
}

sub sendmsgs {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Send the buffered-up lines in @msgs to the client, and empty the buffer.    #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	for $line (@msgs) {
		print "$line\n";
	}
	@msgs = ();		# Done with that batch
}

sub Vopt {$Vopt = shift || '1';
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Set the verbosity from various environment variables. The value may #
# be  a  verbose  level (1 digit), plus an optional output file name. #
# The file V is opened to the file, if any, or STDERR by default. The #
# default  value  for the verbosity level is 1, which generally means #
# to produce only serious error messages.                             #
#                                                                     #
# Here's how this routine is typically called:                        #
#    ($P = $0) =~ s'.*/'' unless defined($P);                         #
#    &Vopt($ENV{"V_$P"} || $ENV{"D_$P"} || $ENV{"T_$P"} || '1');      #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#	print "<br>Vopt: Vopt=\"$Vopt\"<br>\n" if $V>1;
	if ($Vopt =~ /^(\d+)(.+)$/) {
		$V = $1;
		$Vfil = $2;
		if (!open(V,">>$Vfil")) {
			print STDERR "$P: Can't write \"$Vfil\" ($!)\n" if $V>0;
			open(V,">>&STDERR");
		}
	} else {
		$V = $Vopt;
		open(V,">>&STDERR");
	}
	select V; $| = 1; select STDOUT; $| = 1;
	$esep = '=' x 70; print STDERR "\n$P $esep\n" if $V>1;
	$hsep = '-' x 70; print STDERR "\n$P $hsep\n" if $V>1;
	print STDERR "$P started with V=$V ", `date` if $V>1;
}
