#!/bin/perl
#
# Aug 2019 wbc changed this version on www to save midi files, not offer tab and eps
# and not offer download postscript. This is because of all the hits from crawlers
#
# updated July 22 2019 to add test option to use the development version of tab
# Oct 29 2023 added sPAPERSIZE=letter
#
use Cwd;
my $dir = getcwd;
($a, $b) = split /\/wbc/, $dir , 2;
#
$home = $a . "/wbc/";
$filetype=" ";
$ps2pdf="/usr/bin/gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sPAPERSIZE=letter -sOutputFile=- ";
# $home = "/net/ifs-users/wbc";
# $home = "/net/syno-fs/wbc";
# $home = "/home/wbc";
# $tab = "$home/src/nnt/tab.bsd";
$tab = "$home/src/nnt/tab";
$testtab = "$home/src/nnt/tab";
$logfile = ">>$home/tmp/TAB-log";
alarm (30);
$SIG{'ALRM'} = \&end_and_clean;
$SIG{'TERM'} = \&end_and_clean;
$SIG{'INT'} = \&end_and_clean;
$SIG{'QUIT'} = \&end_and_clean;
open(LOGFILE, $logfile);
($f, $e) = split /\?/, $ARGV[0];
$f =~ s/\\//;
$file = "$home/public_html/TabSource/$f";
if ( $e =~ /test/ ) {
$tab = $testtab;
}
print LOGFILE "called with file $file\n";
$ffile = $file;
$ffile =~ s/\.(pdf|mid|eps|spdf)/.tab/;
if ( ! (@status = stat($ffile))) {
print "Content-type: text/html \n\n";
print "Can't open file $ffile
\n";
exit;
}
if ( $file =~ m/eps$/ ) {
$ofile = $file;
$file =~ s/eps/tab/i;
$filetype = "eps";
print "Content-type: application/postscript\n\n";
}
elsif ( $file =~ m/spdf$/ ) {
$ofile = $file;
$ofile =~ s/spdf/pdf/;
@pfile = split /\//, $ofile;
$count = @pfile;
$pfile = @pfile[$count - 1];
print LOGFILE "pfile is $pfile \n";
$file =~ s/spdf/tab/i;
$filetype = "pdf";
print "Content-disposition: attachment; filename = \"$pfile\"\n\n";
}
elsif ( $file =~ m/pdf/ ) {
$ofile = $file;
$file =~ s/pdf/tab/i;
$filetype = "pdf";
print "Content-type: application/pdf\n\n";
}
elsif ( $file =~ m/mid/ ) {
$ofile = $file;
$file =~ s/mid/tab/i;
$filetype = "mid";
print "Content-type: audio/midi\n\n";
}
elsif ( $file =~ m/tab/ ) {
$ofile = $file;
$filetype = "tab";
# print "Content-type: text/plain\n\n";
@pfile = split /\//, $ofile;
$count = @pfile;
$pfile = @pfile[$count - 1];
print "Content-disposition: attachment; filename = \"$pfile\"\n\n";
}
if ( $filetype eq "eps" ) {
if (@status = stat($ofile)) {
$ret = open (TABFILE, $ofile);
}
else {
$ret = open (TABFILE,
"$tab -q -P -300 -G -o stdout $file | /usr/bin/tee $ofile |");
}
}
elsif ( $filetype eq "mid" ) {
if ((@ostatus = stat($ofile)) && (@istatus = stat($file)) && ($ostatus[9] > $istatus[9]) ){
$ret = open (TABFILE, $ofile);
# $ret = open (TABFILE, "$tab -q -midi $file |");
}
else {
unlink $ofile;
$ret = open (TABFILE,
"$tab -q -midi $file | /usr/bin/tee $ofile |");
}
}
elsif ( $filetype eq "pdf" ) {
if ( ( $e !~ /test/ ) && (@ostatus = stat($ofile)) && (@istatus = stat($file)) && ($ostatus[9] > $istatus[9]) ) {
$ret = open (TABFILE, $ofile);
print LOGFILE "sending pre formatted pdf ret is $ret ofile is $ofile $ostatus[9] $istatus[9] $file $ofile\n";
}
else {
if ( $e !~ /test/ ) {
unlink $ofile;
$ret = open (TABFILE, "$tab -q -G -P -o stdout $file | /usr/bin/gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sPAPERSIZE=letter -sOutputFile=- - | /usr/bin/tee $ofile |");
print LOGFILE "sending auto formatted pdf ret is $ret ofile is $ofile $ostatus[9] $istatus[9]\n";
}
else {## test - don't save the pdf output - use the current tab
$ret = open (TABFILE, "$testtab -q -G -P -o stdout $file | /usr/bin/gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sPAPERSIZE=letter -sOutputFile=- - | ");
print LOGFILE "sending test formatted pdf ret is $ret ofile is $ofile $ostatus[9] $istatus[9]\n";
}
}
}
elsif ( $filetype eq "tab" ) {
$ret = open (TABFILE, $ofile);
}
else {
print("this didnt work\n");
}
while () {
print $_;
}
print "\n\n";
close (TABFILE);
close (LOGFILE);
###unlink ( "/tmp/gs\*" );
#system ("/bin/rm -f /tmp/gs*");
exit;
#
# a sig TERM means the net browser has gone on to something else
#
sub end_and_clean {
local ($sig) = @_;
print LOGFILE "caught a signal $sig -- end_and_clean \n";
print LOGFILE "in end_and_clean -- ret is $ret\n";
#
# stop the subprocess
#
kill 9, $ret;
if ( $filetype eq "eps" ) { print (TABFILE "Sorry, processing error\n");}
if ( $filetype eq "pdf" ) { print (TABFILE "Sorry, processing error\n");}
close (TABFILE);
close (LOGFILE);
# unlink ( "/tmp/gs*" );
system ("/bin/rm -f /tmp/gs*");
exit 1 ;
}