#!/usr/bin/perl 
#
# Copyright (c) Members of the EGEE Collaboration. 2004-2010.
# See http://www.eu-egee.org/partners/ for details on the copyright holders.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


BEGIN{
$inst = $ENV{GLITE_PREFIX};
$inst = "/opt/glite" unless $inst;
$sbin = "$inst/sbin";
$bin = "$inst/bin";
$test = "$inst/examples";
$purge = "$bin/glite-lb-purge";
$status = "$test/glite-lb-job_status";
$log = "$test/glite-lb-job_log";
$prefix = "/tmp/purge_test_$$";
$delay = 60;

$ENV{PATH} .= ":$bin";
}

$option = shift;
$server = shift;

die qq{
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

   This script will DESTROY ALL DATA in the specified bookkeeping server.

Don't run it unless you are absolutely sure what you are doing.
If you really mean it, the magic usage is:

   $0 --i-want-to-purge server:port

Good luck!

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	
} unless $option eq '--i-want-to-purge';

die "usage: $0 --i-want-to-purge server:port\n" unless $server;

sub logit {
	my $ids = shift;
	my $prefix = shift;
	my $failed = 0;

	for (qw/aborted cleared cancelled waiting/) {
		my $key = $_ eq waiting ? 'other' : $_;
		$id = `$test/glite-lb-$_.sh -m $server`;
		chomp $id;
		die "$test/glite-lb-$_.sh" if $?;
		$id =~ s/EDG_JOBID=//;
		$ids->{$key} = $id;
#print "$status $id | head -1\n";
		$stat = `$status $id | head -2 | tail -1`;
		chomp $stat;
		$stat =~ s/state :\s*//;
#print "$id: ".uc($stat)." ".uc($_)."\n";
		$failed = 1 if uc($stat) ne uc($_);

		system "$log $id | grep -v '^[ 	]*\$' | grep -v '^Found' >${prefix}_$_";
	}

	!$failed;
}
	
print "** Hey, purging the whole database\n";
system "$purge --server $server --return-list --aborted=0 --cleared=0 --cancelled=0 --other=0";
die "$purge: $!\n" if $!;

print "** Logging test jobs\n";

die "!! failed\n" if !logit \%old,"${prefix}_old";
print "** So far good, sleeping $delay seconds\n";
sleep $delay;
print "** OK, another set of jobs\n";
die "!! failed\n" if !logit \%new,"${prefix}_new";

print "** Dry run\n";
$failed = 0;

$half = $delay/2;
for (qw/aborted cleared cancelled other/) {
	open LIST,"$purge --server $server --dry-run --return-list --$_=${half}s| grep '^https://'|" or die "!! run $purge\n"; 

	$id = <LIST>; chomp $id;
	if ($old{$_} ne $id) {
		$failed = 1;
		print "!! $old{$_} (old $_) is not there\n";
	}
	else {
		print "$_ $id OK\n";
	}
	$id = <LIST>;
	if ($id) {
		$failed = 1;
		chomp $id;
		print "!! $id should not be there\n";
	}
	close LIST;

	open LIST,"$purge --server $server --dry-run --return-list --$_=0s | grep '^https://'|" or die "!! run $purge\n"; 

	$cnt = 0;
	while ($id = <LIST>) {
		chomp $id;
		if ($old{$_} ne $id && $new{$_} ne $id) {
			$failed = 1;
			print "!! $id should not be there\n";
		}
		else {
			print "$_ $id OK\n";
		}
		$cnt++;
	}
	
	close LIST;
	if ($cnt != 2) {
		$failed = 1;
		print "!! bad number of $_ jobs ($cnt)\n";
	}
}

die "!! aborting\n" if $failed;

print "** Server defaults\n";

open LIST,"$purge --server $server --dry-run --return-list | grep '^https://'|" or die "!! run $purge\n";

$failed = 0;
while ($id = <LIST>) {
	$failed = 1;
	print "$id\n";
}

die "!! Oops, should not do anything, too short defaults?\n" if $failed;

print "Nothing purged as expected\n";

print "** Purge the first set of jobs\n";

open DUMP,"$purge --server $server --server-dump --aborted=${half}s --cleared=${half}s --cancelled=${half}s --other=${half}s | grep '^Server dump:'|"
	or die "!! run $purge\n";

$dump = <DUMP>; chomp $dump; $dump =~ s/Server dump: //;
close DUMP;

die "!! no dump file reported\n" unless $dump;
#print "DEBUG: dump file: '$dump'\n";
@list = glob "${prefix}_old*";
system "cat @list | sort >${prefix}_old_all";
system "cat $dump | sed -e s/^.*DATE/DATE/ | sort >${prefix}_old_dump";
sleep 60;
system "diff ${prefix}_old_all ${prefix}_old_dump >/dev/null";

die "!! aggregate log and dump differ\n" if $? & 0xff00;

print "diff OK\n";

print "** Purge the rest\n";
open DUMP,"$purge --server $server --server-dump --aborted=0 --cleared=0 --cancelled=0 --other=0 | grep '^Server dump:'|"
	or die "!! run $purge\n";

$dump = <DUMP>; chomp $dump; $dump =~ s/Server dump: //;
close DUMP;

die "!! no dump file reported\n" unless $dump;
#print "DEBUG: dump file: '$dump'\n";
@list = glob "${prefix}_new*";
system "cat @list | sort >${prefix}_new_all";
system "cat $dump | sed -e s/^.*DATE/DATE/ | sort >${prefix}_new_dump";
system "diff ${prefix}_new_all ${prefix}_new_dump >/dev/null";

die "!! aggregate log and dump differ\n" if $? & 0xff00;

print "diff OK\n";


print "** Anything left?\n";
open LIST,"$purge --server $server --return-list --dry-run --aborted=0 --cleared=0 --cancelled=0 --other=0 | grep '^https://'|" or die "!! $purge\n";

$id = <LIST>;
close LIST;
die "!! Yes, but should not\n" if $id;
print "No, OK\n";

print "\n** All tests passed **\n";
exit 0;

END{ unlink glob "${prefix}*" if $prefix; }
