#!/usr/bin/perl -w

use strict;

sub ftparea ($);

my $vlbidir = "/nfs/ftp/people/vlbi/";

my @patterns = qw(cd.uvflg
		  cd.antab
		  cd.log
		  hh.antabfs
		  hh.uvflgfs
		  hh.log
		  ha.log
		  ha.antabfs
		  ha.uvflgfs
		  hb.antab
		  hb.uvflg
		  hb.log
		  ht.log
		  ht.antabfs
		  ht.uvflgfs
		  ho.antab
		  ho.uvflg
		  ho.log
		  pa.log
		  pa.log.\d+
		  ww.log
		  ke.log
		  ke.uvflg
		  ke.antab
		  yg.log
		  yg.uvflg
		  yg.antab
		  sh.log
		  ti.log
		  tid.*log
		  tid.*antab
		  ti.*antab
		  ti_weather.log
		  Tid.*ANTAB
		  ti.*FLAG
		  tid.flag
		  ta.flag
		  ti.flag
		  ti.tsys
		  wa.log
      _s_dss.*\.tsys
      _x_dss.*\.tsys
		  _dss.*\.tsys
		  _?34.tsys
		  _?43.tsys
		  _?45.tsys
		  _dss.*\.flag
		  _dss.*\.weatherinfo
      _35.log
      _35.logfn
      _35.CFG
      _36.log
      _36.logfn
      _36.CFG
      _43.log
      _43.logfn
      _43.CFG
		  45.flag
		  45.log
		  45.weatherinfo
		  43.flag
		  ti-[lr]cp.tsys
      _locations\.dat
      _stations\.dat
      crd\.[a-z]{2}
		  _ti.*
		  ti.*
		 );
#      sch\.[a-z]{2}
#                  \.key
#                  \.vex
#                  \.sum
#                  \.flag
#                  _[\w_-]+\.key
#                  \.oms
#                  \.tv2d

# Do incoming directory

opendir(INCOMING,  "${vlbidir}incoming") 
  || die "Could not open ${vlbidir}incoming: $!\n";

my @files = readdir(INCOMING);
closedir(INCOMING);

foreach (@files) {
  foreach my $p (@patterns) {
    if (/^(.*)$p$/) {
      my $ftppath = $vlbidir . ftparea($1);
      print "ftppath $ftppath \n";
      if (-e "${vlbidir}incoming/$_") {
	if (-e  $ftppath) {
	  if (-d $ftppath) {
	    if (-w  $ftppath) {
	      print "Moving ${vlbidir}incoming/$_ to $ftppath\n";
	      if (system "cp -fp ${vlbidir}incoming/$_ $ftppath") {
		warn "Could not copy ${vlbidir}incoming/$_ to $ftppath\n";
		next;
	      }
        if ($_ =~ /[khy][ebg].log$/) {
          # remove ridiculous number of tpcont measures from AuScope logs
            system("grep -v '#tpicd#tpcont' $ftppath/$_ > /tmp/temp.log") ;
            system("mv /tmp/temp.log $ftppath/$_") ;
            print "filtered AuScope log" ;
        }
	      system "rm -f ${vlbidir}incoming/$_"
		|| warn "Could not delete ${vlbidir}incoming/$_";
        system "chmod a+r $ftppath/$_";
	    } else {
	      warn "$ftppath is not writable. Cannot move $_\n";
	    }
	  } else {
	    warn "$ftppath is not a directory. Cannot move $_\n";
	  }
	} else {
	  warn "$ftppath does not exist. Cannot move $_\n";
	}
      }
    }
  }
}

# Do Parkes pcfs logs

# Should merge into a function - but note different warning message, and command plus removed one warning

my $PARKESPATH = 'cal/parkes/logs';
opendir(PARKES,  "${vlbidir}$PARKESPATH") 
  || die "Could not open ${vlbidir}$PARKESPATH: $!\n";

@files = readdir(PARKES);
closedir(PARKES);

foreach (@files) {
  foreach my $p (@patterns) {
    if (/^(.*)$p$/) {
      my $ftppath = $vlbidir . ftparea($1);
      if (-e  $ftppath) {
	if (-d $ftppath) {
	  if (-w  $ftppath) {
	    print "Copying ${vlbidir}$PARKESPATH/$_ to $ftppath\n"
	      if (! -f "$ftppath/$_");
	    system "cp ${vlbidir}$PARKESPATH/$_ $ftppath";
      system "chmod a+r $ftppath/$_";
	  } else {
	    warn "$ftppath is not writable. Cannot copy $_\n";
	  }
	} else {
	  warn "$ftppath is not a directory. Cannot copy $_\n";
	}
      } else {
	#warn "$ftppath does not exists. Cannot copy $_\n";
      }
    }
  }
}



sub ftparea ($) {
  my $expr = shift;
  $expr =~ s/^_+//;
  my $parentdir = '';

  if ($expr =~ /(^.*\d+)[^\d].*$/) {
    $parentdir = "$1";
  }

#  if ($expr =~ /(^vt02\D)\S/i) {
#    $parentdir .= "/$1";
  if ($expr =~ /^vc\d+/i) {
    $parentdir .= "fringe-checks";
  }

  if ($expr =~ /^[rb][ekg].\d+/i) {
    $parentdir = "radioastron";
  }
  if ($expr =~ /^g[as].\d+/i) {
    $parentdir = "radioastron";
  }

  return "${parentdir}/$expr";
}
