#!/usr/bin/perl
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#DESCRIPTION
#  abcjoin - combine ABC files
#
#SYNOPSIS
#  abcjoin file...
#
#DESCRIPTION
#  Join a list of ABC files into a single file.  The  tunes  will  be
#  separated  by  a blank line, and will be numbered consecutively if
#  they don't have distinct X: index numbers already.
#
#  The result is written to standard output.
#
#OPTIONS
#
# +H<name>
#    Incorporate the header file <name>.  If it exists, we will read
#    it in, and we will also /local/bin/perlnerate %% directives that tell abc2ps
#    to center its title and then put subsequent titles at the left.
#    
# +R
#     Renumber the tunes.
#     The tunes will be renumbered consecutively, starting from  X:1.
#     The  default  is  -R,  which  also renumbers, but only when the
#     input X: indexes aren't increasing.  So if the input tunes  are
#     already  numbered  in  increasing  order,  the default will use
#     their numbers.
#
# +S  
#     Generate separator lines.
#     This is done with the abc2ps %%sep directive. At present, there
#     is no way to specify the parameters.
#
#BUGS
#  The output X: numbers will be distinct.  This is a feature.
#
#  We never read from standard input.  Maybe we should add an  option
#  to do this.
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu> http://trillian.mit.edu/~jc/
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$| = 1;
$V = 2;
$Xlast = -1;

for $f (@ARGV) {
	print "#$0: arg='$f'\n" if $V>3;
	if (($flg,$opts) = ($f =~ /^([-+])(.*)/i)) {
		print "#$0: flg=$flg' opts='$opts'\n" if $V>2;
		while ($opts =~ s/^(.)//) {
			$opt = uc($1);
			print "#$0: opt='$opt'\n" if $V>2;
			if ($opt eq 'S') {
				$sep = ($flg eq '+') ? 1 : 0;		# Generate separators.
				print "#$0: sep=$sep.\n" if $V>1;
			} elsif ($opt eq 'H') {
				$hopt = ($flg eq '-') ? 0 : 1;		# Look for header file.
				print "#$0: hopt=$hopt.\n" if $V>2;
				if (open(F,$opts)			# Look for the header file.
				||  open(F,"$opts.hdr")) {	# Can we read it?
#					print "\n";
#					print "%%topspace   0.00cm\n";
#					print "%%titlespace 0.30cm\n";
#					print "%%titlefont  Helvetica-bold 16\n";
#					print "%%titleleft  no\n";
#					print "%%staffsep   0\n";
#					print "\n";
					$hfile = 1;
					&onefile();			# Copy it to output.
					$hfile = 0;
#					print "\n";
#					print "%%titlespace 0.00cm\n";
#					print "%%partsspace 0.30cm\n";
#					print "%%partsfont  Helvetica-bold 14\n";
#					print "%%partsleft  yes\n";
#					print "%%staffsep   40\n";
#					print "\n";
					$opts = '';
				} else {
					print STDERR "#$0: Can't read \"$opts\" ($!)\n" if $V>1;
				}
			} elsif ($opt eq 'N' || $opt eq 'R') {
				$ren = ($flg eq '+') ? 1 : 0;		# Renumber the tunes.
				print "#$0: ren=$ren.\n" if $V>2;
			} else {
				print STDERR "#$0: Unknown option '$opt'\n" if $V>0;
			}
		}
	} elsif (open(F,$f)) {
		if ($last && $files) {
			print "\n";
		}
		&onefile();
	} else {
		print STDERR "#$0: Can't read \"$f\" [$~]\n";
	}
}
exit 0;

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Read one file and copy it to output.  It must be the file F.
#
sub onefile {
	local($l,$T,$X);
	$last = '';
	for $l (<F>) {
		$l =~ s/\s+$//;;				# Trim trailing white space
		if ($l || $last) {
			$l =~ s/^(\w:)\s*/$1 /;		# Adjust spacing after :
			if ($l =~ /^X:\s*(\d+)/) {	# X: line
				$X = $1;
				$Xnext = $ren ? 1 : $X;	# The input tune's number, if renumbering
				print "% Xnext=$Xnext Xlast=$Xlast\n" if $V>3;
				if ($Xnext <= $Xlast) {$Xnext = $Xlast + 1}
				print "% Xnext=$Xnext Xlast=$Xlast\n" if $V>2;
				print "\n%%sep 8 8 8cm\n" if $sep && $tunes;
				$l = "X: $Xnext";
				print "\n$l\n";
				++$tunes;
				$Xlast = $Xnext;		# Remember this tune index
			} elsif ($l =~ /^T:\s*(.+)/) {	# T: line
				if (!defined($X)) {
					$X = 0;
					print "\nX: 0\n";
				}
				print "%T $1\n" if $V>2;
				if (!defined($T) || (!$hopt) || $hfile) {
					print "% Copy title because T not defined.\n" if !defined($T) && $V>2;
					print "% Copy title in header file.\n"        if $hfile && $V>2;
					print "% Copy title because no H option.\n"   if !$hopt && $V>2;
					$T = $1;				# Only do first title for non-header files.
					if ($hfile || !$hopt) {
						print "T: $T\n";	# Use regular title line.
					} else {
						print "P: $T\n";	# Convert title to "part" name.
					}
				}
			} else {
				print "$l\n";
			}
		}
		$last = $l;
	}
	++$files;
	}
