#!/usr/bin/perl -Tw
#
# NAME
#   TuneGet
#
# SYNOPSIS
#   <form method=get action="/cgi/abc/TuneGet?F=type&X=ndx&U=url&N=name.suf">
#
# REQUIRES 

	push @INC, '.';
	$host = `hostname`;		# Get our host name
	$host =~ s/[\r\s]+$//;	# Strip off trailing white stuff
	$host =~ s/\..*//;		# Strip off everything after the first dot
if (-f "$host-cgilocal.pm") {
	require "$host-cgilocal.pm";
} else {
	require "cgilocal.pm";	# Where things are kept on this machine.
}
require "sendsubs.pm";	# Routines to send messages

# DESCRIPTION
#
# AUTHOR
#   John Chambers <jc@trillian.mit.edu>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$| = 1;			# Unbuffered STDOUT.
$V = 2;			# Verbose level.
$" = ',';		# Used in verbose messages.
$ONeills = 1;	# Enable O'Neill Project kludge/heuristic.
($me = $0) =~ s".*/"";

require "URLopen.pm";
require "HTTPcon.pm";

$a2p = 'abc2ps';	# Default abc-to-postscript program.
$scale = '55';
$fmt = "_$scale";
$mimetype  = "text/html";

%abctypes = (	# Types that need an intermediate .abc file.
	'EPS'	=> 1,
	'GIF'	=> 1,
	'MIDI'	=> 1,
	'PNG'	=> 1,
	'PS'	=> 1,
	'XXX'	=> 1,
);
%mimetypes = (	# What Content-type to send.
	'ABC'	=> 'text/vnd.abc',
	'EPS'	=> 'application/postscript',
	'GIF'	=> 'text/html',
	'GIFX'	=> 'image/gif',
	'HTML'	=> 'text/html',
	'MIDI'	=> 'audio/midi',
	'PNG'	=> 'text/html',
	'PS'	=> 'application/postscript',
	'TXT'	=> 'text/plain',
	'XXX'	=> 'text/html',
);

$logfile  = "$tmpdir/tune$$.log";
$abcfile  = "$tmpdir/tune$$.abc";
$psfile   = "$tmpdir/tune$$.ps";
$epsfil0  = "$tmpdir/tune$$.eps";
$epsfil1  = "$tmpdir/tune$$001.eps";
$epsfils  = "$tmpdir/tune$$*.eps";
$giffile  = "$tmpdir/tune$$.gif";
$gifurl   = "$tmpurl/tune$$.gif";
$pngfile  = "$tmpdir/tune$$.png";
$pngurl   = "$tmpurl/tune$$.png";
$midifile = "$tmpdir/tune$$.midi";
if (!open(L,">>$logfile")) {
	&send("<b>Can't write \"$logfile\" ($!)\n");
	&quit(1);
}
select L; $| = 1; select STDOUT;
$Lopen = 1;		# Logfile is open.

$keepgif = 1;
$keeppng = 1;

$RA = $ENV{REMOTE_ADDR} || '0.0.0.0';
$SV = $ENV{SERVER_NAME} || 'localhost';
#if ($RA eq '199.172.62.5' || $RA eq '127.0.0.1') {$V = 2}
&lsend("From: $RA V=$V \"$0\".\n") if $V>0;

$cgi = new CGI_Lite;
%data = $cgi->parse_form_data ();
chomp($cwd = `pwd`);
$data{indexdir} = $indexdir;
if ($V>0) {
	&lsend("Form data:\n");
	for $xx (sort keys %data) {
		$yy = $data{$xx};
		&lsend("$xx = \"$yy\"\n");
	}
}

$title = $data{title};
$pattern = $data{pattern};
$URL  = $data{U};
$Fmt  = $data{F};
$Xopt = int($data{X});
$xopt = (defined($data{x}) && int($data{x})) ? '-x' : '';
$nopt = (defined($data{n}) && int($data{n})) ? '-n' : '';
$Nopt = $data{N} || 'Tune.abc';
$Sopt = $data{S} || "0.$scale";
$V    = $data{V} || $V;

if ($URL) {
	$URL =~ s"^//*"http://$SV/";	# Expand "local" URLs.
} elsif ($tune = $data{data}) {
	&lsend("Called with POST data:\n");
	@tune = split("\n",$tune);
	if ($V>1) {
		for $line (@tune) {
			&lsend("ABC: \"$line\"\n");
		}
	}
} else {
	&lsend("### Called without a URL ###\n");
	&send("<p><b>Called without a URL</b>\n");
	quit(1);
}
$URL =~ s"^http:/+"http://"i;

($ss,$mm,$hh,$DD,$MM,$YY) = gmtime(time);
$ymd = sprintf("%d-%02d-%02d",1900+$YY,1+$MM,$DD);
$hms = sprintf("%02d:%02d:%02d",$hh,$mm,$ss);
$idlines = "H:$ymd $hms UT\t$URL\n";
if ($V>2) {
	&lsend("Environment:\n");
	for $k (sort keys %ENV) {$y = $ENV{$k}; &lsend("\t$k\t$y\n")}
}
if ($V>0) {
	&lsend($idlines);
	for $H ('F','N','U','X') {
		&lsend("$H:\t$data{$H}\n");
	}
}
if ($V>1) {
	&lsend("Data:\n");
	for $k (sort keys %data) {$y = $data{$k}; &lsend("\t$k\t$y\n")}
	if ($x = $ENV{PATH_INFO}) {&lsend("PATH_INFO:\t$x\n");}
}
chdir $cgidir;
chomp($cwd = `pwd`);
&lsend("Changed to $cwd\n") if $V>1; 

#if ($Fmt eq 'PNG') {
#	$mimetype = 'text/html';
#	&send("<p>Sorry, we can't produce PNG files on this server yet.<p>\n");
#	exit 0;
#}

if ($V>1) {
	&lsend("Request for:\n");
	&lsend("\tURL\t$URL\n");
	&lsend("\tFmt\t$Fmt\n");
	&lsend("\tX\t$Xopt\n");
}
if ($ONeills && (index($URL,'jc/music/book/oneills/') >= 0)) {
#	$URL =~ s'/___/'/dev/';
	$isONeills = 1;
}
#if ($URL =~ m"://www.musicaviva.com/abc/scotland.abc") {
#	$V = 5;
#}

$useURLopen = 0;
if (@tune) {
	for $line (@tune) {
		&abcline($line);
	}
} elsif ($useURLopen) {
	if (!URLopen(*U,$URL)) {
		&send("<br>Can't open URL \"$URL\" ($!)\n");
		&lsend("Can't open URL \"$URL\" ($!)\n");
		&send("<br>Here's the <a href=\"$tmpurl/tune$$.log\">log file</a>.");
		&quit(1);
	}
} else {
	$getcmd = "w3cat -T60 +THI \"$URL\"";
	&lsend("$me: getcmd=\"$getcmd\"\n") if $V>2;
	$ENV{"V_w3cat"} = "$V/tmp/w3cat.log";
	unless ($getpid = open(U,"$getcmd |")) {
		$err = "Can't run \"$getcmd\" ($!)";
		&lsend("$me: $err\n") if $V>0;
		&send("<p><b>$err</b><p>\n");
		&quit($!);
	}
	&lsend("$me: Opened getcmd for reading, pid=$getpid.\n") if $V>2;
	$URLhdr = 1;
}
$intune = 0;
line:
	for $line (<U>) {
		&lsend("LINE: $line") if $V>2;
		&send($line) if $V>5;
		if ($URLhdr) {
			&lsend("HDR: $line") if $V>2;
			if ($line =~ /^\s*$/) {
				$URLhdr = 0;
			} else {
				&hdrline($line);
			}
		} elsif (!$intune) {
			&lsend("LINE $line") if $V>2;
			if ($line =~ /^X:\s*(\d+)/) {	# X: starts new tune.
				$XX = int($1);
				$intune = ($Xopt == 0 || $Xopt == $XX) ? 1 : 0;
				&lsend("X: $XX.\n") if $V>1;
			}
		}
		if ($intune) {
			if ($line =~ /^\s+$/) {
				$intune = 0;
				&abcline("\n");
				&lsend("Done with tune $XX.\n") if $V>1;
				if ($Xopt > 0) {	# If $Xopt > 0, use only the one tune.
					close U;
					last line;
				}
			} elsif (!$T && ($line =~ /^T:\s*(.*)\s*$/)) {
				$T = &AdjTitle($1);
				$T =~ s/[^A-Za-z0-9]//g;
			}
			if ($isONeills && ($line =~ /^B:/) && !$extras) {
				&abcline("N:\n");
				&abcline("N:This is from the Online O'Neill's Project. If you have a copy of O'Neill's, please proofread this, \n");
				&abcline("N:and if you find any discrepancies, send a note to the transcriber or to <jc\@trillian.mit.edu>\n");
				&abcline("N:\n");
#				&abcline($idlines);
				$extras = 1;
			}
			&abcline($line);
		}
	}
&lsend("EOF from getcmd.\n") if $V>2;
if (!@abc) {
	&send("<b>X:$Xopt not found in $URL</b>\n");
	&lsend("X:$Xopt not found in $URL\n");
	&send("<p>Headers:<pre>\n");
	&send(@hdr);
	&send("</pre>\n");
}
if ($Fmt eq 'ABC' || $Fmt eq 'TXT') {
	$mimetype = $mimetypes{$Fmt};
	&send("\n");
	&send(@abc);
#	&send($idlines);
	&send("\n");
	&quit(0);
}

unless ($abctypes{$Fmt}) {
	&send("Format <b>$Fmt</b> not implemented yet.<br>\n");
	&lsend("Format $Fmt not implemented yet.\n");
	&quit(1);
}
unless (open(ABC,">$abcfile")) {
	&send("<b>Can't write \"$abcfile\" ($!)\n");
	&lsend("Can't write \"$abcfile\" ($!)\n");
	&quit(1);
}
print ABC @abc;
#print ABC $idlines if $Fmt ne 'EPS';
print ABC "\n";
close ABC;

if ($Fmt eq 'PS' || $Fmt eq 'GIF' || $Fmt eq 'PNG') {
	if (($URL =~ '/Test/') || ($URL =~ '/src/jcabc2ps/')) {
		$ENV{'V_abc2ps'} = '8';
		$ENV{'D_abc2ps'} = '8';
		$a2p = 'jcabc2ps';
		$fmt = '_95';
	}
	$pscmd = "$a2p $abcfile -F $fmt -s $Sopt $nopt $xopt -maxs 2000 -o -O $psfile >>$logfile 2>&1";
	&lsend("pscmd=\"$pscmd\"\n") if $V>1;
	if ($V>1) {
		$which = `which $a2p`;
		&lsend("$a2p is $which\n") if $V>1;
	}
	@psout = `$pscmd`;
	if ($?) {
		&send("pscmd=\"$pscmd\" returned status $?.<br>\n");
		&send(@psout);
		&lsend("pscmd=\"$pscmd\" returned status $?.\n");
		&lsend(@psout);
		quit($?);
	}
	if (!open(PS,$psfile)) {
		&send("<b>Can't find postscript file \"$psfile\"</b>\n");
		&quit(1);
	}
	if ($Fmt eq 'PS') {
		$mimetype = $mimetypes{$Fmt};
		for $line (<PS>) {
			&send($line);
		}
	} elsif ($Fmt eq 'GIF') {
		close PS;
		$gifcmd = "(ps2gif $psfile) >>$logfile 2>&1";
		if ($V>1) {
			$which = `which $a2p`;
			&lsend("$a2p is $which");
		}
		@gifout = `$gifcmd`;
		if ($?) {
			&send("gifcmd=\"$gifcmd\" returned status $?<br>\n");
			&lsend(@gifout) if $V>1;
			&quit($?);
		}
		$keepgif = 1;
		$mimetype = $mimetypes{$Fmt};
		&send("<html><head><title>$T</title></head><body>\n");
		&send("<img src=\"$gifurl\" alt=\"$URL\"><br>\n");
		&send("<center>\n");
		&send("<hr>\n");
		&send("Return tune in other formats:<br>\n");
		$script = $ENV{'SCRIPT_NAME'};
		$host = $ENV{'HTTP_HOST'} || $ENV{'SERVER_NAME'} || 'localhost';
		$hscr = "$host$script";
		&send("<table border=3 cellpadding=3 cellspacing=3><tr>\n");
		&send("<td><a href=\"$URL\">File</a></td>\n");
		&send("<td><a href=\"http://$hscr?F=ABC&X=$Xopt&U=$URL&N=/$T.abc\">ABC</a></td>\n");
		&send("<td><a href=\"http://$hscr?F=TXT&X=$Xopt&U=$URL&N=/$T.txt\">TXT</a></td>\n");
		&send("<td><a href=\"http://$hscr?F=PS&X=$Xopt&U=$URL&N=/$T.ps\">PS</a></td>\n");
		&send("<td><a href=\"http://$hscr?F=EPS&X=$Xopt&U=$URL&N=/$T.eps\">EPS</a></td>\n");
		&send("<td><a href=\"http://$hscr?F=GIF&X=$Xopt&U=$URL&N=/$T.gif\">GIF</a></td>\n");
		&send("<td><a href=\"http://$hscr?F=MIDI&X=$Xopt&U=$URL&N=/$T.midi\">MIDI</a></td>\n");
		&send("</tr></table>\n");
		&send("</center>\n");
		&ShowVars() if $V>2;
		&send("</body>\n");
	} elsif ($Fmt eq 'PNG') {
		close PS;
		$pngcmd = "(ps2png $psfile) >>$logfile 2>&1";
		if ($V>1) {
			$which = `which $a2p`;
			&lsend("$a2p is $which");
		}
		@pngout = `$pngcmd`;
		if ($?) {
			&send("pngcmd=\"$pngcmd\" returned status $?<br>\n");
			&lsend(@pngout) if $V>1;
			&quit($?);
		}
		$keeppng = 1;
		$mimetype = $mimetypes{$Fmt};
		&send("<html><head><title>$T</title></head><body>\n");
		&send("<img src=\"$pngurl\" alt=\"$URL\"><br>\n");
		&send("<center>\n");
		&send("<hr>\n");
		&send("Return tune in other formats:<br>\n");
		$script = $ENV{'SCRIPT_NAME'};
		$host = $ENV{'HTTP_HOST'} || $ENV{'SERVER_NAME'} || 'localhost';
		$hscr = "$host$script";
		&send("<table border=3 cellpadding=3 cellspacing=3><tr>\n");
		&send("<td><a href=\"$URL\">File</a></td>\n");
		&send("<td><a href=\"http://$hscr?F=ABC&X=$Xopt&U=$URL&N=/$T.abc\">ABC</a></td>\n");
		&send("<td><a href=\"http://$hscr?F=TXT&X=$Xopt&U=$URL&N=/$T.txt\">TXT</a></td>\n");
		&send("<td><a href=\"http://$hscr?F=PS&X=$Xopt&U=$URL&N=/$T.ps\">PS</a></td>\n");
		&send("<td><a href=\"http://$hscr?F=EPS&X=$Xopt&U=$URL&N=/$T.eps\">EPS</a></td>\n");
		&send("<td><a href=\"http://$hscr?F=GIF&X=$Xopt&U=$URL&N=/$T.gif\">GIF</a></td>\n");
		&send("<td><a href=\"http://$hscr?F=PNG&X=$Xopt&U=$URL&N=/$T.png\">PNG</a></td>\n");
		&send("<td><a href=\"http://$hscr?F=MIDI&X=$Xopt&U=$URL&N=/$T.midi\">MIDI</a></td>\n");
		&send("</tr></table>\n");
		&send("</center>\n");
		&ShowVars() if $V>2;
		&send("</body>\n");
	} else {
		&send("Format <b>$Fmt</b> not known.<br>\n");
		quit(1);
	}
} elsif ($Fmt eq 'EPS') {
	$epscmd = "$a2p $abcfile -E $nopt -o -O $epsfil0 >>$logfile 2>&1";
	&lsend("epscmd=\"$epscmd\"\n") if $V>1;
	if ($V>1) {
		$which = `which $a2p`;
		&lsend("$a2p is $which\n") if $V>1;
	}
	@epsout = `$epscmd`;
	if ($?) {
		&send("epscmd=\"$epscmd\" returned status $?.<br>\n");
		&send(@epsout);
		&lsend("epscmd=\"$epscmd\" returned status $?.\n");
		&lsend(@epsout);
		quit($?);
	}
	unless (open(EPS,($epsfile=$epsfil0)) || open(EPS,($epsfile=$epsfil1))) {
		&send("<b>Can't find postscript file \"$epsfil0\" or \"$epsfil1\" </b>\n");
		&send("Here's the diagnostic output:\n");
		&send("<pre>\n");
		print @epsout;
		&send("</pre>\n");
		&quit(1);
	}
	if ($Fmt eq 'EPS') {
		$mimetype = $mimetypes{$Fmt};
		for $line (<EPS>) {
			&send($line);
		}
	} elsif ($Fmt eq 'GIF') {
		close EPS;
		$gifcmd = "(eps2gif $epsfile) >>$logfile 2>&1";
		if ($V>1) {
			$which = `which $a2p`;
			&lsend("$a2p is $which");
		}
		@gifout = `$gifcmd`;
		if ($?) {
			&send("gifcmd=\"$gifcmd\" returned status $?<br>\n");
			&lsend(@gifout) if $V>1;
			&quit($?);
		}
		if (!open(GIF,$giffile)) {
			&send("<b>Can't find postscript file \"$giffile\"</b>\n");
			&send("Here's the diagnostic output:\n");
			&send("<pre>\n");
			print @gifout;
			&send("</pre>\n");
			&quit(1);
		}
		$mimetype = $mimetypes{$Fmt};
		while (read(GIF,$line,10000)) {&send($line)}
		close GIF;
		quit(0);
	} else {
		&send("Format <b>$Fmt</b> not known.<br>\n");
		quit(1);
	}
} elsif ($Fmt eq 'XXX') {
	$mimetype = $mimetypes{$Fmt};
	$keepeps = 1;
	$keepabc = 1;
	&send("XXX format:<br>\n") if $V>1;
	$epscmd = "$a2p $abcfile -E $nopt -o -O $epsfil0 2>&1";
	&lsend("epscmd=\"$epscmd\"\n") if $V>1;
	if ($V>1) {
		$which = `which $a2p`;
		&lsend("$a2p is $which\n") if $V>1;
	}
	@epsout = `$epscmd`;
	if ($?) {
		&send("epscmd=\"$epscmd\" returned status $?.<br>\n");
		&send(@epsout);
		&lsend("epscmd=\"$epscmd\" returned status $?.\n");
		&lsend(@epsout);
		quit($?);
	}
	&lsend("Command's output:\n") if $V>1;
	&lsend(@epsout);
#	&lsend("Matching \"$epsfils\" ...\n") if $V>1;
#	@epsfiles = glob($epsfils);
#	unless (@epsfiles) {
#		&send("<b>Can't find EPS files \"$epsfils\" </b>\n");
#		&send("Here's the diagnostic output:\n");
#		&send("<pre>\n");
#		print @epsout;
#		&send("</pre>\n");
#		&quit(1);
#	}
#	&send("<ol>\n");
	&lsend("Files output:\n") if $V>1;
	for $line (@epsout) {
		&lsend("Line: $line") if $V>1;
		if (($epspth,$title) = ($line =~ /^\[(.*)\]\s*(.*)\s*$/)) {
			&lsend("---: epspth=\"$epspth\" title=\"$title\"\n") if $V>1;
			($epsfil = $epspth) =~ s"^$tmpdir/*"";
			&send("\t<li><a href=\"$tmpurl/$epsfil\">$epsfil</a> $title\n");
		}
	}
	&send("</ol>\n");
} elsif ($Fmt eq 'MIDI') {
	$midicmd = "abc2midi $abcfile -v -o $midifile >>$logfile 2>&1";
	&lsend("midicmd=\"$midicmd\"\n") if $V>1;
	if ($V>1) {
		$which = `which abc2midi`;
		&lsend("abc2midi is $which\n");
	}
	@midiout = `$midicmd`;
#	if ($?) {
#		&send("midicmd=\"$midicmd\" returned status $?<br>\n");
#		&send("Here's the <a href=\"$tmpurl/tune$$.log\">log file</a>.<br>\n");
#		quit($?);
#	}
	if (!open(MIDI,$midifile)) {
		&send("<b>Can't find postscript file \"$midifile\"</b>\n");
		&send("midicmd=\"$midicmd\"<br>\n");
		&send("Exit status was $?<br>\n");
		&send("Here's the diagnostic output:\n");
		&send("<pre>\n");
		print @midiout;
		&send("</pre>\n");
		&quit(1);
	}
	$mimetype = $mimetypes{$Fmt};
	while (read(MIDI,$line,10000)) {&send($line)}
} else {
	&send("Format <b>$Fmt</b> not implemented yet.<br>\n");
	&quit(1);
}
quit(0); 

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
sub abcline {
	local($arg,$line);
line:
	for $arg (@_) {
		$line = $arg;
		if ($HTML) {
			if ($line =~ s"^<[/\w]+>"") {
			} elsif ($line =~ s"<[/\w]+>\s+$"\n") {
			}
			next line if ($line =~ /^\s+$/);
		}
#		if ($idlines && ($line =~ /^K:/)) {
#			push @abc, $idlines;
#			$idlines = '';
#		}
		push @abc, $line;
	}
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 
# Adjust the title. We do a bit of canonicalization, including converting all
# but the first letter of each word to lower case.
#
sub AdjTitle {
	local($t) = @_;
	print "AdjTitle: \"$t\"\n" if $V>3;
	$t =~ s/^\s+//;					# Strip initial white space.
	$t =~ s/\s+$//;					# Strip trailing white space.
	$t =~ s/\s+/ /g;				# All white space to single space.
	print "AdjTitle: \"$t\"\n" if $V>3;
	$t =~ s/^(the|a|an) //i;		# Strip initial articles.
	print "AdjTitle: \"$t\"\n" if $V>3;
	$t =~ s/\b(\w)([\w']*)\b/\u$1\L$2/g;	# Ulll case all words except:
	print "AdjTitle: \"$t\"\n" if $V>3;
	$t =~ s/\bMc(\w)/Mc\u$1/ig;		# McPhuaiogh special case.
	print "AdjTitle: \"$t\"\n" if $V>3;
	$t =~ s/\bO'(\w)/O'\u$1/ig;		# O'Bhaoirre special case.
	print "AdjTitle: \"$t\"\n" if $V>3;
	$t =~ s"(\w)'s\b"$1's"ig;		# Bhaiesse's special case.
	print "AdjTitle: \"$t\"\n" if $V>3;
	return $t;
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
sub hdrline {
	local($line) = @_;
	if ($line =~ /^content-type:\s*(.*)/i) {
		if ($1 eq 'text/html') {
			$HTML = 1;		# Bad news!
		}
	}
	push @hdr, $line;
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Here we clean up our scratch files and exit.  The first thing we do  is  to
# purge  all  the intermediate files that are more than a day old.  This will
# leave behind only the *.log files, which are kept for statistics.  Then, if
# we are exiting normally, we get rid of the intermediate files from this get
# operation, except for those that have a keep flag set.  Such files are  the
# target of hyperlinks sent to the user, so we keep them for a day.
#
sub quit {
	local($s) = @_;
	system "find $tmpdir -mtime +1 '('  -name '*.abc'  -o -name '*.ps'  -o -name '*.eps'  -o -name '*.gif'  -o -name '*.png'  -o -name '*.midi'  ')' -exec ls -lidg {} ';' -exec rm -f {} ';' >>$logfile 2>&1 & ";
	system "rm -f /tmp/w3cat.log";
	&lsend("$me: quit($s) pngfile=\"$pngfile\ giffile=\"$giffile\ psfile=\"$psfile\" epsfile=\"$epsfile\" abcfile=\"$abcfile\" logfile=\"$logfile\"\n") if $V>1;
	if ($s) {
		&send("<br><hr>Error exit status $s; here are some files to check:");
		&send("<dl compact >");
		if (-f $logfile)  {&send("<dt>log  <dd><a href=\"$tmpurl/tune$$.log\">tune$$.log</a>")}
		if (-f $abcfile)  {&send("<dt>ABC  <dd><a href=\"$tmpurl/tune$$.abc\">tune$$.abc</a>")}
		if (-f $psfile)   {&send("<dt>PS   <dd><a href=\"$tmpurl/tune$$.ps\">tune$$.ps</a>")}
		if (-f $giffile)  {&send("<dt>GIF  <dd><a href=\"$tmpurl/tune$$.gif\">tune$$.gif</a>")}
		if (-f $pngfile)  {&send("<dt>PNG  <dd><a href=\"$tmpurl/tune$$.png\">tune$$.png</a>")}
		if (-f $midifile) {&send("<dt>MIDI <dd><a href=\"$tmpurl/tune$$.midi\">tune$$.midi</a>")}
		&send("</dl>"); 
	} elsif ($V < 3) {
		if (-f $midifile) {unlink $midifile; &lsend("$me: unlinked \"$midifile\"\n")}
		if (-f $giffile && !$keepgif)  {unlink $giffile ; &lsend("$me: unlinked \"$giffile\"\n")}
		if (-f $pngfile && !$keeppng)  {unlink $pngfile ; &lsend("$me: unlinked \"$pngfile\"\n")}
		if (-f $psfile)   {unlink $psfile  ; &lsend("$me: unlinked \"$psfile\"\n")}
		if (-f $epsfile && !$keepeps)  {unlink $epsfile ; &lsend("$me: unlinked \"$epsfile\"\n")}
		if (-f $epsfil0 && !$keepeps)  {unlink $epsfil0 ; &lsend("$me: unlinked \"$epsfil0\"\n")}
		if (-f $epsfil1 && !$keepeps)  {unlink $epsfil1 ; &lsend("$me: unlinked \"$epsfil1\"\n")}
		if (-f $abcfile && !$keepabc)  {unlink $abcfile ; &lsend("$me: unlinked \"$abcfile\"\n")}
#		if (-f $logfile)  {unlink $logfile ; &lsend("$me: unlinked \"$logfile\"\n")}
	}
	&lsend("$me: quit($s)\n") if $V>0;
	close L;
	exit $s;
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
sub send {
	print "Content-type: $mimetype\n\n" if !$hdrDone++;
	print @_;
}


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
sub ShowVars {
	&send("Environment:<br><dl>\n");
	for $k (sort keys %ENV) {$y = $ENV{$k}; &send("\t<dt>$k<dd>\"$y\"\n")}
	&send("</dl>\n");
	&send("Data:<br><dl>\n");
	for $k (sort keys %data) {$y = $data{$k}; &send("\t<dt>$k<dd>\"$y\"\n")}
	if ($x = $ENV{PATH_INFO}) {&lsend("\t<dt>PATH_INFO:<dd>\"$x\"\n");}
	&send("</dl>\n");
}
