#!/star/Perl/bin/perl -w
require 'server.pl';
BEGIN { push @INC,'.'; }
use File::Basename;
$email=$ARGV[1];
$file=$ARGV[0];
$release=$ARGV[2];

$email =~ s/;//g;
$file =~ s/;//g;

#print "$email $file";

$i=0;
$j=0;
$no_files=0;
$flag=0;
$comp=0;

$basedir  = dirname($file);
#&cgiLog($file,$baseDir);
$file = basename($file);

#&cgiLog($file,$basedir);
#print "$file\n";
#print "$basedir\n";


$dirfits=$DR2FITS;
$sedString=$DR2FITS."/[0-9][0-9]/";

if ($release eq "dr3") {
$dirfits=$DR3FITS;
$sedString=$DR3FITS."/[0-9][0-9]/";
}


$sedString=~ s/\//\\\//g;
#print "$string\n"; 
#$pTar=" -P --transform='s/\\/6dFGS//g'  ";
$pTar=" -P --transform='s/$sedString//g'  ";





if ($file=~/gz/) {
    $comp=1;
   
    $ufile=$file;
    $ufile=~ s/csv\.gz/csv/;

    system ("gunzip -cf $TMPWEB/$basedir/$file > $TMPWEB/$basedir/$ufile");
    $file=~ s/csv\.gz/csv/;
}


$prefix=$file;
$prefix=~ s/csv//;
$tarfile=$prefix."tar";
$tempfile=$prefix."tmp";
#print "$file\n";
#print "$tempfile\n";
open(CSVFILE,"<$TMPWEB/$basedir/$file");
open(OUTFILE,">$TMPWEB/$basedir/$tempfile") || die "unable to create file";

while (<CSVFILE>){

    chomp;
@parts=split (/,/,$_);

if ($flag == 0 ){
foreach (@parts) {
if ($_ =~/targetname/i) {
$flag=1;
}
if ($flag==0){
$i=$i+1;
}
}
}
if ($flag != 0 and $j !=0){
$parts[$i] =~ s/^\s*//; # strip leading spaces
$parts[$i] =~ s/\s*$//; # strip trailing spaces

if ($parts[$i]=~/g/i and (length $parts[$i]) > 2){

    $dir=substr($parts[$i],1,2);
}
else{
    $dir="fooey";
}
#    $filename="/scat4/www/sss/fits/".$dir."/".$parts[$i].".fits";

$filename="$dirfits/".$dir."/".$parts[$i].".fits";
#&cgiLog($filename);

    if (-e $filename and $no_files < 1500){
print OUTFILE "$filename\n";
$no_files=$no_files+1;
} else {
#	$no_files=$no_files+1;
    }
}
$j=$j+1;
}


close (OUTFILE);
close(CSVFILE);
#&cgiLog("hereee");

#$pTar=" -P $DR2FITS/00/  -P $DR2FITS/01/  -P $DR2FITS/02/  -P $DR2FITS/03/  -P $DR2FITS/04/  -P $DR2FITS/05/  -P $DR2FITS/06/  -P $DR2FITS/07/  -P $DR2FITS/08/  -P $DR2FITS/09/  -P $DR2FITS/10/  -P $DR2FITS/11/  -P $DR2FITS/12/  -P $DR2FITS/13/  -P $DR2FITS/14/  -P $DR2FITS/15/  -P $DR2FITS/16/  -P $DR2FITS/17/  -P $DR2FITS/18/  -P $DR2FITS/19/  -P $DR2FITS/20/  -P $DR2FITS/21/  -P $DR2FITS/22/  -P $DR2FITS/23/ ";




#system ("cd /home/6df/public_html/tmp; /usr/ucb/tar -P /scat4/www/sss/fits/00/  -P /scat4/www/sss/fits/01/  -P /scat4/www/sss/fits/02/  -P /scat4/www/sss/fits/03/  -P /scat4/www/sss/fits/04/  -P /scat4/www/sss/fits/05/  -P /scat4/www/sss/fits/06/  -P /scat4/www/sss/fits/07/  -P /scat4/www/sss/fits/08/  -P /scat4/www/sss/fits/09/  -P /scat4/www/sss/fits/10/  -P /scat4/www/sss/fits/11/  -P /scat4/www/sss/fits/12/  -P /scat4/www/sss/fits/13/  -P /scat4/www/sss/fits/14/  -P /scat4/www/sss/fits/15/  -P /scat4/www/sss/fits/16/  -P /scat4/www/sss/fits/17/  -P /scat4/www/sss/fits/18/  -P /scat4/www/sss/fits/19/  -P /scat4/www/sss/fits/20/  -P /scat4/www/sss/fits/21/  -P /scat4/www/sss/fits/22/  -P /scat4/www/sss/fits/23/ -cf $tarfile -R $tempfile; /usr/local/bin/gzip $tarfile");


system ("cd $TMPWEB/$basedir; tar $pTar -cf $tarfile -T $tempfile; gzip $tarfile");

if ($comp ==1){
#    system ("cd /home/6df/public_html/tmp; /usr/local/bin/gzip -f $file");
}
if (!open(MAIL,"| /usr/lib/sendmail -t"))
{
print "error - couldn't send email, check address entered in form \n"; 
}
else
{
print MAIL "To: $email\n";
print MAIL "From:$EMAIL\n";
print MAIL "Subject: 6dFGS TAR saveset request\n";
print MAIL "\n";
$gzippedtar="$TMPWEB/".$basedir."/".$tarfile.".gz";
if (-e $gzippedtar){

print MAIL "The TAR saveset of FITS files relating to your 6dFGS query \n";
print MAIL "has been processed and the results can be downloaded from: \n";
print MAIL "$BASETMPURL/$basedir/$tarfile.gz \n";

if ($no_files == 0) {
 print MAIL "WARNING TAR savest appears to contain no files";
}
if ($no_files > 1500){
    print MAIL "TAR saveset limited 1500 files out of $no_files";
}
}
else {
    print MAIL "Error - unable to complete your request for a TAR saveset";
print MAIL "of you 6dFGS query. Please try again later or email mar@roe.ac.uk";} 
}

close (MAIL);



