#!/usr/bin/perl -w

use strict;
use Cwd;

my $programName = "get_meris_files.pl";

my $logFileName = "log";
my $logFileMaxSize = 10000000;

my $wgetCommand = "wget -c -a $logFileName --no-verbose --no-check-certificate";

my $l1MerisHost = "oa-ks.eo.esa.int";
my $l2MerisHost = "oa-ks.eo.esa.int";

my $l1MerisUsername = "frs14Nam";
my $l2MerisUsername = "frs24Nam";

my $l1MerisPassword = "frs1onra";
my $l2MerisPassword = "frs2onra";

my $l1IndexFileName = "/ra/mer_frs_l1_ca/index.php";
my $l2IndexFileName = "/ra/mer_frs_l2_ca/index.php";

my $l1subdir = "l1_frs";	# where should the L1 files be written
my $l2subdir = "l2_frs";	# where should the L2 files be written

my @allFiles;			# list of all the files in the index.php
my @newFiles;			# list of new files that need to be downloaded



##########################################
# main program execution
##########################################
my $val;

$val = DoTheWholeThing($l1MerisHost, $l1MerisUsername, $l1MerisPassword, 
		      $l1IndexFileName, $l1subdir);
if(!$val) {
    print "$programName seems to have an error with MERIS L1 downloads\n";
}


$val = DoTheWholeThing($l2MerisHost, $l2MerisUsername, $l2MerisPassword, 
		      $l2IndexFileName, $l2subdir);
if(!$val) {
    print "$programName seems to have an error with MERIS L2 downloads\n";
}


exit(0);



##############################################################
#
# DoTheWholeThing($host, $user, $pass, $indexFileName, $subDir)
#    - grab the index file
#    - parse it
#    - look in directory to see which files are new
#    - download new files
#
# $host - website to contact
# $user - username to login as
# $pass - password
# $indexFileName - filename of index file
# $subDir - sub directory on the local system where files are written
#
# return - 1 on success, 0 on failure
#
##############################################################
sub DoTheWholeThing {
    my ($host, $user, $pass, $indexFileName, $subDir) = @_;

    # remember current directory so we can get back there
    my $originalDir = getcwd();

    # change to the sub dir for the files
    if( !chdir($subDir) ) {
	print "Could not change to dir $subDir\n";
	return 0;
    }

    # clear out the global file arrays
    @allFiles = ();
    @newFiles = ();

    RotateLogFile();

    PrintLog("---------- Starting ---------- ");

    # delete the index file
    unlink("index.php");

    # download the index file
    if( !GetMerisFile($host, $user, $pass, $indexFileName) ) {
	PrintLog("Could not download index file $indexFileName");
	chdir $originalDir;
	return 0;
    }
    
    # parse the index file into @allFiles
    if( !ReadIndex() ) {
	PrintLog("Could not read index file $indexFileName");
	chdir $originalDir;
	return 0;
    }

#    print "---------- All Files ---------\n";
#    PrintFiles(@allFiles);

    # no need to bother figuring out what files I have already
    # downloaded since I am using the -c flag (continue) on wget
    DownloadAllFiles($host, $user, $pass);


    PrintLog("---------- Exiting Normally ---------- ");

    # get us back to the original directory
    chdir $originalDir;

    return 1;
}


##############################################################
#
# PrintFiles(@fileNames)
#  - print all of the files in the argument list
#
##############################################################
sub PrintFiles {
    my $name;
    foreach $name (@_) {
	print "$name\n";
    }
    print "\n";
} # sub printFiles


##############################################################
#
# GetMerisFile($host, $user, $pass, $name)
#    - download the file from the web site
#
# $host - website to contact
# $user - username to login as
# $pass - password
# $name - filename file to download
#
# return - 1 on success, 0 on failure
#
##############################################################
sub GetMerisFile {
    my ($host, $user, $pass, $name) = @_;
    my $file;
    my $val = system("$wgetCommand --http-user=$user --http-passwd=$pass https://$host$name");
    
    if($val == 0) {
	$file = GetFileName($name);
	system("chmod -f g+w $file; chgrp -f shared $file");
	return 1;
    } else {
	print "GetMerisFile - Problem downloading $name\n";
	return 0;
    }

} # sub GetMerisFile


##############################################################
#
# ReadIndex()
#    - parses the file "index.php" for MER_FRS file names
#    - adds the file names to @allFiles global variable
#
# return - 1 on success, 0 on failure
#
##############################################################
sub ReadIndex {

  my $line;
  my @parts;
  my $part;
  my $name;
  my $junk;

  #open index file
  open INDEXFILE, "index.php" or return 0;       # open index file
  while ($line = <INDEXFILE>) {        	         # loop through lines of the file
    @parts = split(/href=\"/, $line);            # split line with href="
    foreach $part  (@parts) {	                 # loop through all hrefs in line
      if($part =~ /^\/MER_FRS_/) {               # if part begins with "/MER_FRS_"
	($name, $junk) = split(/\"/, $part, 2);  # only keep file name
	push @allFiles, $name;	                 # add file name to the array
      }
    }

  } # while index file
  close(INDEXFILE);


  # check to make sure at leas some files were found
  if(@allFiles == 0) {
      PrintLog("no MERIS files found in index");
      return 0;
  }

  return 1;
} # sub ReadIndex


##############################################################
#
# GetFileName($path)
#    - return the file name form the given path
#
##############################################################
sub GetFileName {
  my ($path) = @_;
  my @parts;

  @parts = split(/\//, $path);         # split path into pieces
  return $parts[$#parts];             # last part is file name
  
} # sub GetFileName


##############################################################
#
# FindNewFiles()
#    - loop through all of the files in @allFiles
#    - if the file does not exist in the current directory
#        add the filename to @newFiles
#
##############################################################
sub FindNewFiles {
  my $path;
  my $file;

  foreach $path (@allFiles) {	         # loop through allFiles array
    $file = GetFileName($path);             # last part is file name
    if(!(-e $file)) {                    # if file not in current directory
      push @newFiles, $path;             # add full file name to newFiles array
    }
  }
} # sub FindNewFiles


##############################################################
#
# DownloadNewFiles($host, $user, $pass)
#    - download all of the files in the @newFiles array
#
# $host - website to contact
# $user - username to login as
# $pass - password
#
##############################################################
sub DownloadNewFiles {
    my($host, $user, $pass) = @_;

    my $name;
    my $i;

    # need to loop backwards to grab the oldest files first
    for($i=$#newFiles; $i>=0; $i--) {
	$name = $newFiles[$i];
	GetMerisFile($host, $user, $pass, $name);
    }
} # sub printFiles



##############################################################
#
# DownloadNewFiles($host, $user, $pass)
#    - download all of the files in the @newFiles array
#
# $host - website to contact
# $user - username to login as
# $pass - password
#
##############################################################
sub DownloadAllFiles {
    my($host, $user, $pass) = @_;

    my $name;
    my $i;

    # need to loop backwards to grab the oldest files first
    for($i=$#allFiles; $i>=0; $i--) {
	$name = $allFiles[$i];
	GetMerisFile($host, $user, $pass, $name);
    }
} # sub printFiles


##############################################################
#
# RotateLogFile
#    - rotate log file so it does not get to big
#
##############################################################
sub RotateLogFile {
    my $size = 0;

    $size = -s $logFileName;
    if($size > $logFileMaxSize) {
	print "$programName rotating log\n";
	rename($logFileName, "$logFileName.old");
    }
} # sub RotateLogFile


##############################################################
#
# PrintLog($str)
#    - print the string to the log file
#
##############################################################
sub PrintLog {
    my ($str) = @_;

    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    $year += 1900;
    $mon += 1;
    my $timeStr = sprintf("%4d-%02d-%02d %02d:%02d:%02d ", 
			  $year, $mon, $mday, $hour, $min, $sec);

    if(!open(LOG, ">>", $logFileName)) {
	print "$timeStr Could not open log file \"$logFileName\"\n";
	return;
    }

    print LOG "$timeStr $str\n";
    close LOG;

} # sub PrintLog



