# -*-perl-*-

# $Id: ScourDB.plm,v 1.2 2002/07/08 15:56:35 thomas Exp $

# Scour the database, looking for atoms over a certain age

# $Log: ScourDB.plm,v $
# Revision 1.2  2002/07/08 15:56:35  thomas
# 0.2.3 release
#
# Revision 1.1.1.1  2002/05/21 14:13:36  thomas
# sushi - backup to disk
#

package Sushi::Command::ScourDB;

use strict;
use Getopt::Long;
use Time::localtime;

# Mount things here
my $MNTDIR = "/s/sushi/mountpoints";

###############################################################################

sub main {
  my ($class) = shift;

  my ($res);

  # Default number of days to look
  my ($days) = 31;

  Getopt::Long::Configure ("bundling");
  if (!($res = GetOptions (
			   'days|d=s',   \$days
			  ))) {
    $main::log->error (&help);
    return 0;
  }

  defined ($ARGV[0]) and $days = shift @ARGV;

  my $age = time - $days*24*60*60;
  lookForAtoms ($MNTDIR, $age);

  return 0;
}

###############################################################################

sub lookForAtoms {
  my $dir = shift;
  my $age = shift;

  my ($dh, $de);

  if (!defined ($dh = new DirHandle $dir)) {
    $::log->error ("ScourDB: Failed to open directory $dir\n");
    return ;
  }
  while (defined ($de = $dh->read)) {
    $de =~ /^\.\.?$/ and next;
    # $::log->log (4, "Checking $dir/$de\r");
    if ($de =~ /^\d{8}\.\d{6}$/) {
      checkResult ("$dir/$de", $age);
    } elsif (-d "$dir/$de") {
      lookForAtoms ("$dir/$de", $age);
    }
  }
}

###############################################################################

sub checkResult {
  my $dest = shift;
  my $age = shift;

  my $mtime = (stat($dest))[9];
  my ($type, $name, $ts);
  my ($group, $year, $mon, $mday, $hours, $min, $sec) =

  $::log-> (4, "ScourDB: Found directory $dest with mtime " . ctime($mtime) . "\n");
  (undef, undef, undef, $type, undef, $name, undef, $ts) = split (/\//, $dest);
  if ($ts = /^(.*):(\d{4})(\d{2})(\d{2})\.(\d{2})(\d{2})(\d{2})$/) {
    ($group, $year, $mon, $mday, $hours, $min, $sec) =
      ($1, $2, $3, $4, $5, $6, $7);
  } else {
    $::log->error ("ScourDB: Can't parse timestamp in $dest!\n");
    return;
  }
  my $timestamp = timelocal ($sec, $min, $hours, $mday, $mon, $year);

  ($timestamp >= $age) and return;

  # Read the atom and change the result record
  my $atoms = Sushi::Atom->readAtoms (
				      schedule_group => $group,
				      type           => $type,
				      name           => $name
				     );
  if ((my $numatoms = scalar (@$atoms))!=1) {
    $::log->error ("ScourDB: Retrieved $numatoms atoms when reading $group:$type:$name\n");
    return;
  }
  my $atom = (@$atoms)[0];
  foreach my $result ($atom->getResults) {
    if ($result->{timestamp} == $timestamp) {
      print "Delete result $dest for atom " . $atom->commonName . "\n";
    }
  }
}

###############################################################################

sub shortHelp {
  return "List atoms";
}

###############################################################################

sub help {
  my $help = <<__Eof__

atom\tlist <atom-spec to list>
\tdel <atom-spec to delete>
\tfailactive
options:\t[(--schedule_group|-s) <group>] [(--type|-t) <type>] [(--name|-n) <atom name>] [(--id|-i) <atom id>] [(--results|-r)] [(--weights|-w) <limits>]

__Eof__
;
}

###############################################################################

1;
