#!/usr/bin/perl

# Configure here
#--------------------------------------------

$SETIDIR = "/usr/local/seti";
$SETICLIENT = "setiathome";
$NICE = 10;

# No changes should be needed below this line
#--------------------------------------------
$SETIMAX=0.999;
$MYPROG=0.0;
$STATES=["new.","in progress. (%2.2f\%)","finished.","empty"];

sub getSetiState($){
  my $dir = shift;
  $MYPROG=0.0;
  if(open(MYFILE,"< $dir/state.sah")){
    @file=<MYFILE>;
    foreach $line (@file){
      if($line =~ /prog=./){
      	@prog=split("=",$line);
      	$MYPROG=$prog[1]*100;
      	return ($prog[1]>=$SETIMAX?2:1);
      }
    } 
    return 0;
  }else{
    return 3;
  }
}

sub status($){
  my $p = shift;
  if($p =~ /html/i){
    chdir $SETIDIR;
    opendir(DIR,".") or die "Can't open the current directory: $!\n";
    @DIRS = readdir(DIR) or die "Unable to read directory: $!\n";
    print "<TABLE>\n";
    print "<TR><TD>Dir.</TD><TD>Status</TD></TR>\n";
    foreach $DIR (@DIRS){
      if (($DIR =~ /([0-9]+)/)&&(-d $DIR)){
        $p=$STATES->[getSetiState($DIR)];
        printf "<TR><TD>$DIR</TD><TD>is $p</TD></TR>\n",$MYPROG;
      }
    }
    print "</TABLE>\n";
  }else{
    chdir $SETIDIR;
    opendir(DIR,".") or die "Can't open the current directory: $!\n";
    @DIRS = readdir(DIR) or die "Unable to read directory: $!\n";
    foreach $DIR (@DIRS){
      if (($DIR =~ /([0-9]+)/)&&(-d $DIR)){
        $p=$STATES->[getSetiState($DIR)];
        printf "$DIR is $p\n",$MYPROG;
      }
    }
  }
}

sub run{
  $R=qx(ps -C setiathome --format="cmd");
  @a=split("\n",$R);
  if(scalar @a==1){
    chdir $SETIDIR;
    opendir(DIR,".") or die "Can't open the current directory: $!\n";
    @DIRS = readdir(DIR) or die "Unable to read directory: $!\n";
    foreach $DIR (@DIRS){
      if(($DIR =~ /([0-9]+)/)&&(-d $DIR)&&(getSetiState($DIR)!=2)){
      	chdir($DIR);
        exec "./setiathome -stop_after_process -email -nice $NICE >/dev/null &" or die "Setiathome could not be run";
        exit;
      }
    }
    print "All clients seem to be finished. Please transmit!";
  }
}

sub transmit{
  chdir $SETIDIR;
  opendir(DIR,".") or die "Can't open the current directory: $!\n";
  @DIRS = readdir(DIR) or die "Unable to read directory: $!\n";
  $mycount=0;
  print "Transmitting data.\n";
  foreach $DIR (@DIRS){
    if(($DIR =~ /([0-9]+)/)&&(-d $DIR)&&(getSetiState($DIR)>1)){
      chdir($DIR);
      $r=`./setiathome -stop_after_xfer`;
      @x=split("\n",$r);
      foreach $line (@x){
        if($line =~ /.completed:./){
          print "$line\t";
        }
        if($line =~ /.time:./) {
          print "$line\n";
        }
      }
      $mycount++;
      chdir("..");
    }
  }
  printf "%d packets transmitted.\n",$mycount;
}

sub usage {
  print "\nUsage: seticache.pl {run|status|transmit}\n\n";
}

# Catch error if no parameter is given
if(!@ARGV) { usage; exit 1;}

if(@ARGV[0] =~ m/\Arun\Z/i ) {
  run;
  exit 0;
} 
if(@ARGV[0] =~ m/\Astatus\Z/i ) {
  if(scalar @ARGV == 2){
    status(@ARGV[1]);
  }else{
    status("normal");
  }
  exit 0;
} 
if(@ARGV[0] =~ m/\Atransmit\Z/i ) {
  transmit;
  exit 0;
} 

# Catch error if a wrong parameter is given
usage;
exit 1;
