#!/star/Perl/bin/perl
use Astro::FITS::CFITSIO;
use File::Basename;
require 'server.pl';

my $logDir=$TMPWEB."logs/";


$diskFile=$logDir."disksOffline.txt";

sub getEmail {
my $archive=$_[0];
if ($archive =~ /^vsa/i) {
    return "vsa-support\@roe.ac.uk";
}
elsif ($archive =~ /^osa/i) {
    return "osa-support\@roe.ac.uk";
}
else {
 return "wsa-support\@roe.ac.uk";
}
}

sub getDisks {
    return glob("/disk[0-9]*");
}


sub getDisksOld {
    my @disks;
    open(FSTAB, "/etc/fstab") or die "Can't open /etc/fstab";
    while(<FSTAB>) {
	push(@disks, $disk) if ($disk) = $_ =~ m~^[^#].*/(disk\d\d)~;
					       }
	close (FSTAB);
	return @disks;
    }

sub getKeyVal { 
my $status = 0;
my $fitsFileName=$_[0]; 
my $keyword=$_[1]; 
my $fptr =Astro::FITS::CFITSIO::open_file($fitsFileName,Astro::FITS::CFITSIO::READONLY(),$status); 
$fptr->read_key_str($keyword,$keyval,undef,$status);
return $keyval;
}

sub getUnfiltFileName {
my $filtFilename=$_[0];
my $unfiltDir=$_[1];
($name,$path,$suffix) = fileparse($filtFilename);
(my $unFiltName=$name) =~ s/_two//g;
   my $unFiltFilename= $unfiltDir."/".$unFiltName;
  if (-e $unFiltFilename){
	return $unFiltFilename;
    }
}

sub getUnfiltDirName {
    $filtFilename=$_[0];
    my @disks=@_[1..@_];  

   ($name,$path,$suffix) = fileparse($filtFilename);

$dirname  = basename($path);

$verPos=index($dirname,"_v",0);
$version=substr($dirname,$verPos+2,100);
$preVersion=$version;
  for ($preversion=$version; $preversion > 0 ; $preversion-- ) { 
($unfiltDirName = $dirname) =~ s/_v${version}/_v${preVersion}/g;

foreach (@disks) {
    $ingestDir="/".$_."/wsa/ingest/fits/".$unfiltDirName;

    if (-e $ingestDir) {
    return $ingestDir;
}
}

}


}








sub cgiLog {
    my $cgiName=$_[0];
    my $ip=$_[1];
    my $bytesSent=$_[2];
    my $extra=$_[3];
    my $err=$_[4];
    my $logString=$cgiName.",".$ip.",".$bytesSent.",".$extra.",".$err;
($sec,$min,$hour,$day,$mon,$year,$a1,$a2,$a3) = localtime (time);

$year=$year+1900;

$thisMonth= (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[$mon];
$mon++;
$timeStamp=sprintf("%02d",$day)."/".$thisMonth."/".$year." ".sprintf("%02d",$hour).":".sprintf("%02d",$min).":".sprintf("%02d",$sec);
$logFile=$logDir."cgi".$year."_".sprintf("%02d",$mon)."_".sprintf("%02d",$day).".log";
$logString=$timeStamp.",".$logString;
open(LOGFILE, ">> $logFile") || die "cant open logfile: $!";
print LOGFILE "$logString\n";
close(LOGFILE);
} 

sub isDiskOnline {
	if (-e $diskFile) {
        print "$diskFile ::\n";
	my $path=$_[0];
	#$pos1=index($path,"/",0);
	#$pos2=index($path,"/",$pos1+1);
	#if ($pos1 >=0 && $pos2 > $pos1) {
	#$disk=substr($path,$pos1+1,$pos2-$pos1-1);
	#print "$disk ::\n";
		open (DISKFILE, "< $diskFile") || die return 1;
		while (<DISKFILE>) {
		    chomp;
		    my $diskLine=$_;
			print "$diskLine :: $path\n";
			if ($path =~ /$diskLine/) {
				return 0;
			}
		}	
	return 1;
	
	}
	else {
		return 1;
	}
	
}

sub getMatchingFiles {
    my @mFiles;
    my $fileQ=$_[0];
my $archive="wsa";
if ($fileQ=~ /\/v20/ || $fileQ=~ /^v20/ ) {
    $archive="vsa";
}

my $sPos=rindex($fileQ,'/');
my $uPos=index($fileQ,'_',$sPos);

my $stem= substr $fileQ, $sPos+2, $uPos-$sPos-2;
$fileBit= substr $fileQ, $sPos+1, 128;
    my $globStr;
#    print "<!--sPos $sPos file bit :$fileBit: stem :$stem: archive $archive-->";
#print "$stem \n";
    if ($fileQ=~ /\/e20/ || $fileQ=~ /^e20/ ) {
	 $globStr="/disk??/?sa/products/{stacks,mosaics}/".$stem."*/".$fileBit."*";
    } else {
 $globStr="/disk??/".$archive."/ingest/fits/".$stem."*/".$fileBit."*";
    }
#print "<!-- :$globStr: \n -->";
my @mFiles = glob($globStr);
#print "files\n";
#print "<!-- mfile0 $mFiles[0]\n -->";

	return @mFiles;
}
sub getJpegMatchingFiles {
    my @mFiles;
    my $fileQ=$_[0];
my $archive="wsa";
if ($fileQ=~ /\/v20/ || $fileQ=~ /^v20/ ) {
    $archive="vsa";
}
    if ($fileQ=~ /\/e20/ || $fileQ=~ /^e20/ ) {
	$archive="?sa";
    }
my $sPos=rindex($fileQ,'/');
my $uPos=index($fileQ,'_',$sPos);

my $stem= substr $fileQ, $sPos+2, $uPos-$sPos-2;
$fileBit= substr $fileQ, $sPos+1, 128;

#print "$stem \n";
my $globStr="/disk??/".$archive."/products/jpgs/".$stem."*/".$fileBit."*";
#print "$globStr \n";
my @mFiles = glob($globStr);
#print "files\n";
#print "@mFiles\n";

	return @mFiles;
}
