#!/usr/local/bin/perl 

use CGI ':standard';
$query = new CGI;
$cfile = "r1301-1900.tar.gz";
$cfile17 = "r2001-2900.tar.gz";

print header;
print start_html('Download MOCCA MC'),
#    '<body bgcolor="#eefffa">',
    h1('<FONT COLOR="#800080">Download MOCCA simulation production files</FONT> '),
    " <LI> Available Run numbers are 1301-1900 </LI>",p,
    ' <LI> 10^17-10^19 energy range for runs 2001-2900 </LI>',
#     p,'<applet code="Bubbles.class" width=500 height=500> alt="Your browser understands the &lt;APPLET&gt; tag but isn't running the applet, for some reason." Your browser is completely ignoring the &lt;APPLET&gt; tag! </applet>',
    start_form,
    '<FONT COLOR="#008080">Enter run number(s) </FONT> ',textfield('runn'),
    p,	
    $query->checkbox(-name=>'range',	
			-checked=>'0',
			-value=>'1',
			-label=>' Use range (ex.: 1301-1305) '),
    $query->checkbox(-name=>'tar',	
			-checked=>'0',
			-value=>'1',
			-label=>' Use tar  '),

    $query->checkbox(-name=>'sum',	
			-checked=>'0',
			-value=>'1',
			-label=>' Add summary files  '),



    $query->checkbox(-name=>'ntp',	
			-checked=>'0',
			-value=>'1',
			-label=>' Add ntuples  '),
    p,hr,
    " You can download complete set of input and output files (without ntuples):",
"<UL>","<li>",a({href=>$cfile},$cfile),"</li>"
,"<li>",a({href=>$cfile17},$cfile17),"</li></UL>",
    p, "Autogen files: ","<ul>",
    "<li>",a({href=>"autogen.r1301-r1900"},"autogen.r1301-r1900"),"</li>",
    "<li>",a({href=>"autogen.r2001-r2900"},"autogen.r2001-r2900"),"</li>","</ul>",
    p,hr,
    a({href=>"mailto:Andrej.Filipcic\@ijs.si"},"Andrej.Filipcic\@ijs.si"),
    p,
    end_form,
    hr;

if ($query->param) {
    $runrange=param('runn');
    $range=param('range');
    $addntSp=param('ntp');
    $tar=param('tar');
    $addsum=param('sum');
    if($range == "1" ) { 
	@tmp=split(/-/, $runrange);
	$runf=$tmp[0];
	$runl=$tmp[1];
    }
    else {
	$runf=$runrange;
	$runl=$runrange;
    }
    if($tar != "1") {
	for($i=$runf;$i<=$runl;$i++) {
	    $runnt=$i;
	    do getdata();
	}
    } else {
	$tfile="/usr/local/etc/httpd/htdocs/auger/private/Mocca/tmp/r${runf}-$runl.tar";
	$istmp=`/usr/local/bin/gtar -cf $tfile`;
	for($i=$runf;$i<=$runl;$i++) {
	    $runnt=$i;
	    do getdata1();
	}
	$tfilegz="tmp/r${runf}-$runl.tar.gz";
	$istmp=`/usr/contrib/bin/gzip -f $tfile`;
	print "Tar file: ", a({href=>$tfilegz},$tfilegz),p,hr;
    }
}
sub getdata {
    $runn=$runnt;
    $fseq=$runn-1300;
    if($runn>2000) {$fseq=$runn-2000};
#    @file=`/usr/local/bin/stageqry -a -V MOCCA4 -P -q $fseq `;
    @file="ntp/r$runn.ntp";
#    $runn=$fseq+1300;
     @ofile="out/r$runn.log.gz";
#    @ofile=`find out -name r$runn.log.gz`;
#    @ifile=`find inp -name r$runn.inp`;
#    $nfile="http://merlot.ijs.si/auger/private/Mocca$file[0]";
    if($#ofile == -1) {
	print 
	    "Run number: ",em($runn)," does not exist",
	    p,
	    hr;
    } else {
#	@ofile=`find out -name r$runn.o'*'`;
	@ifile=`find inp -name r$runn.inp.gz`;
#	if(@ifile == "") {@ifile="not found"};
	@sfile="sum/r$runn.sum.gz";
	@sldfile="sum/r$runn.sum_ld.gz";
	$nfile="http://merlot.ijs.si/auger/private/Mocca/$file[0]";
	if(@ifile == "") {
	print 
	    '<h4 align="center"><FONT COLOR="#008080">',"Run number: ",em($runn),'</FONT></h2>',
	    p,
	    '<FONT COLOR="#FF0000">Does not exist</FONT>',
	    p,
	    hr;
	}
	else{
	print 
	    '<h4 align="center"><FONT COLOR="#008080">',"Run number: ",em($runn),'</FONT></h2>',
	    p,
	    "Input file: ", a({href=>@ifile},@ifile),
	    p,
	    "Output file: ", a({href=>@ofile},@ofile),
	    p,
	    "Summary file:", a({href=>@sfile},@sfile),
	    p,
	    "Summary file:", a({href=>@sldfile},@sldfile),
	    p,
	    "Ntuple file: ",a({href=>$nfile},@file),
	    p,
	    hr;
    }
    }
}

sub getdata1 {
    $runn=$runnt;
    $fseq=$runn-1300;
    @file="ntp/r$runn.ntp";
    $runn=$fseq+1300;
    @ofile="out/r$runn.log.gz";
#    @ofile=`find out -name r$runn.log.gz`;
    @sfile="sum/r$runn.sum.gz";
    @sldfile="sum/r$runn.sum_ld.gz";
    if($#ofile == -1) {
	print 
	    "Run number: ",em($runn)," does not exist",
	    p,
	    hr;
    } else {
	@ifile=`find inp -name r$runn.inp.gz`;
	$nfile="http://merlot.ijs.si/auger/private/Mocca/$file[0]";
	$istmp=`/usr/local/bin/gtar -rf $tfile $ifile[0] `;
	$istmp=`/usr/local/bin/gtar -rf $tfile $ofile[0]`;
	if($addsum == "1") {
	    $istmp=`/usr/local/bin/gtar -rf $tfile $sfile[0] `;
	    $istmp=`/usr/local/bin/gtar -rf $tfile $sldfile[0]`;
	}
	if($addntp == "1") {
	    $istmp=`/usr/local/bin/gtar -rhf $tfile $file[0]`;
	}
#	print "ifile $ifile[0] ofile $ofile[0]",p;
    }
}
#print a({href=>'../cgi_docs.html'},'Go to the documentation');

