#! /usr/local/bin/perl -w

use DateTime;
use Getopt::Std;

getopts('hRd:',\%opt);

if (exists($opt{h})) {
    print "USAGE: clean_nightlyscience_backlog.pl [-h] [-R] -d <Ndays>\n";
    print "  Clean up old nightlyscience data that was missed.\n";
    print "    -h                  This help\n";
    print "    -R                  Actually do cleanup (no pretend)\n";
    print "    -d <Ndays>          Select number of days into the the past to work\n";
    exit();
}

unless (exists($opt{d})) {
    $opt{d} = 1;
}
$dt = DateTime->now;

for ($i = 0; $i < $opt{d}; $i++) {
    $string = $dt->ymd;
    
    # Generate command.
    $cmd = "nightly_science.pl --clean_old ";
    unless (exists($opt{R})) {
	$cmd .= " --debug ";
    }
    $cmd .= "--date $string";
    print "$cmd\n";
    system($cmd);

    # Count down one day.
    $dt->subtract( days => 1) ;
}
