#!/usr/bin/perl
#
#NAME
#  TrackSort - process list of ripped tracks
#
#SYNOPSIS
#  TrackSort [file]..
#
#DESCRIPTION
#  This processes a list of .wav files, from a CD for example.  We  currently
#  produce MP3 output and an HTML listing.
#
#  The  input  (file  or  stdin) should be at list of tracks in the following
#  format, with tabs separating the fields:
#
#  N	filename	time	TrackName	Track's full name
#
#  The five fields are:
#
#  N is the track number, preferably a 2-digit number.
#
#  filename should be a file or path to the file.
#
#  TrackName is the basename of output files.
#
#  Track's full name
#    is what the track should be called in text output
#
#
#OPTIONS
#  Options say which columns to produce in the output An option  starts  with
#  '-'  to  suppress a column or '+' to include a column.  There is a default
#  set ...
#
#  +all   Produce all the output columns
#
#  +abc
#
#  +html
#
#  +mp3
#
#  +ndx
#
#  +wav
#
#  +words
#
#EXAMPLES
#
#FILES
#
#BUGS
#
#SEE ALSO
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$| = 1;
($P = $0) =~ s".*/"";
$V = $ENV{"V_$P"} || 1;
$exitstat = 0;
$htmlfile = "tracklist.html";
$lnks = 1;
$ttlopt = 1;
$timopt = 1;
&allopts();		# Default is now to produce everything

for $arg (@ARGV) {
	++$args;
	if (($flg,$opt) = ($arg =~ /^([-+])(.*)/)) {
		$o = lc($opt);
		if ($o eq 'all') {
			print "$P: Writing everything:\n" if $V>0;
			&allopts();
		} elsif ($o eq 'html') {
			++$opts; ++$htmlopt;
			print "$P: Writing HTML.\n" if $V>0;
		} elsif ($o eq 'mp3') {
			++$opts; ++$mp3opt; ++$lnks;
			print "$P: Writing MP3.\n" if $V>0;
		} elsif ($o eq 'wav') {
			++$opts; ++$wavopt; ++$lnks;
			print "$P: Linking WAV.\n" if $V>0;
		} elsif ($o eq 'abc') {
			++$opts; ++$abcopt; ++$lnks;
			print "$P: Linking ABC.\n" if $V>0;
		} elsif ($o eq 'ndx') {
			++$opts; ++$ndxopt; ++$lnks;
			print "$P: Linking Index.\n" if $V>0;
		} elsif ($o eq 'words') {
			++$opts; ++$wrdopt; ++$lnks;
			print "$P: Linking Words.\n" if $V>0;
		} else {
			print STDERR "$P: Unknown option \"$opt\" ignored.\n";
		}
	} elsif (-f $arg) {
		++$files;
		unless (open(F,$arg)) {
			print STDERR "$P: Can't read \"$arg\" ($!)\n";
			next;
		}
		while ($line = <F>) {
			&oneline($line);
		}
		close F;
	} else {
		print STDERR "$P: Unknown arg \"$arg\" ignored.\n";
	}
}
unless ($files) {
	while ($line = <STDIN>) {
		&oneline($line);
	}
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Generate the WAV output.

if ($wavopt) {
	writeWAVs();
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Generate the MP3 output.

if ($mp3opt) {
	writeMP3s();
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Generate the HTML output.

if ($htmlopt) {
	if (open(HTML,">$htmlfile")) {
		print "$P: Writing \"$htmlfile\"\n" if $V>1;
		writeHTML();
	} else {
		print STDERR "$P: Can't write \"$htmlfile\" ($!)\n" if $V>1;
	}
}

exit $exitstat;
sub allopts {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	++$htmlopt;
	print "$P: Writing HTML.\n" if $V>1;
	++$mp3opt;
	++$lnks;
	print "$P: Writing MP3.\n" if $V>1;
	++$wavopt;
	++$lnks;
	print "$P: Linking WAV.\n" if $V>1;
	++$abcopt;
	++$lnks;
	print "$P: Linking ABC.\n" if $V>1;
	++$ndxopt;
	++$lnks;
	print "$P: Linking Index.\n" if $V>1;
	++$wrdopt;
	++$lnks;
	print "$P: Linking Words.\n" if $V>1;
}

sub writeHTML {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	local($t,$ndx,$pth,$tim,$nam,$ttl,$wavnam);
	print "$P: Writing \"$htmlfile\"\n" if $V>0;
	print HTML "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>\n";
	print HTML "<html>\n";
	print HTML "<head>\n";
	print HTML "<title>___ ___ ___</title>\n";
	print HTML "</head>\n";
	print HTML "<body>\n";
	print HTML "<H1>___ ___ ___</H1>\n";
	print HTML "<hr>\n";
	print HTML "<table border>\n";
	for ($t = 0; $t <= $maxtrack; $t++) {
		next unless $Ndx[$t];
		$ndx = $Ndx[$t];	# Track index number
		$pth = $Pth[$t];	# Track path
		$tim = $Tim[$t];	# Track time
		$nam = $Nam[$t];	# Track name
		$ttl = $Ttl[$t];	# Track title
		$wavnam = $wavopt ? "$nam.wav" : $pth;
		print HTML "\t<tr>\n";		# Row 1: files, title
		if ($ndxopt) {	# Index
			print HTML "\t\t<th>$ndx</th>\n";
		}
		if ($wavopt) {	# WAV
			print HTML "\t\t<td><a href='$wavnam'>WAV</a></td>\n";
		}
		if ($mp3opt) {	# MP3
			print HTML "\t\t<td><a href='$nam.mp3'>MP3</a></td>\n";
		}
		if ($abcopt) {	# ABC
			print HTML "\t\t<td><a href='$nam.abc'>ABC</a></td>\n";
		}
		if ($wrdopt) {	# Words
			print HTML "\t\t<td><a href='$nam.txt'>Words</a></td>\n";
		}
		if ($ttlopt) {	# Title
			print HTML "\t\t<td>$ttl</td>\n";
		}
		if ($timopt) {	# Time
			print HTML "\t\t<td>$tim</td>\n";
		}
		print HTML "\t</tr>\n";
		print HTML "\t<tr>\n";		# Row 2: Space for text
		print HTML "\t\t<td colspan=$lnks></td>\n";
		print HTML "\t\t<td>___</td>\n";
		print HTML "\t</tr>\n";
	}
	print HTML "</table>\n";
	print HTML "</body>\n";
}

sub writeMP3s {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	local($t,$cmd);
	local($ndx,$pth,$tim,$nam,$ttl);
	print "$P: Converting tracks to MP3 ...\n" if $V>0;
	for ($t = 0; $t <= $maxtrack; $t++) {
		next unless $Ndx[$t];
		$ndx = $Ndx[$t];	# Track index number
		$pth = $Pth[$t];	# Track path
		$tim = $Tim[$t];	# Track time
		$nam = $Nam[$t];	# Track name
		$ttl = $Ttl[$t];	# Track title
	#	$ttl =~ s/'/\\'/g;	# Escape single quotes
		print "$P: MP3 $t \"$ttl\" ...\n";
		$cmd = "lame -h --tt \"$ttl\" $pth $nam.mp3";
		print "CMD: $cmd\n" if $V>0;
		system $cmd;
	}
}

sub writeWAVs {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	local($t,$cmd);
	local($ndx,$pth,$tim,$nam,$ttl);
	print "$P: Linking tracks to WAV ...\n" if $V>0;
	for ($t = 0; $t <= $maxtrack; $t++) {
		next unless $Ndx[$t];
		$ndx = $Ndx[$t];	# Track index number
		$pth = $Pth[$t];	# Track path
#		$tim = $Tim[$t];	# Track time
		$nam = $Nam[$t];	# Track name
		$ttl = $Ttl[$t];	# Track title
		print "$P: Track $t is \"$ttl\".\n";
		$cmd = "ln -f $pth $nam.wav";
		print "CMD: $cmd\n" if $V>0;
		system $cmd;
	}
}

sub oneline { my $F="$P/oneline";
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	local($line) = @_;
	local($t);
	$line =~ s/[\s\r]+$//;
	return unless $line;
	return if ($line =~ /^\s*#/);
	if ($line =~ /^(\d+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)$/) {
		$t = int($1);
		$Ndx[$t] = $1;	# Track index number
		$Pth[$t] = $2;	# Track path
		$Tim[$t] = $3;	# Track time
		$Nam[$t] = $4;	# Track name
		$Ttl[$t] = $5;	# Track title
		$maxtrack = $t if ($maxtrack < $t);
		return;
	}
	print STDERR "$F: Can't parse line:\n\t$line\n";
}
