#!/usr/local/bin/perl

use strict;
use warnings;

use lib '/cell_root/software/HPCC/lib/site_perl';
#For testing, use RW replica
#use lib '/afs/.glue.umd.edu/software/HPCC/1.1/lib/site_perl';

use Glue::HPCC::Utils::FileUtils qw(parse_archive_metainfo_file);

use Getopt::Long;

sub usage()
{	print STDERR <<EOF;
$0: Dump/validate metainfo file for archived directory

Usage:
$0 [ --verify [ --verify ] [ --quiet | --verbose ] file
where file is either the metainfo file or the archived directory.

--verify will do successively more strict error checking on the
metainfo file, complaining about missing fields, etc.

Normally only important fields from the contents of metainfo file 
will be dumped.  If --verbose given, increasingly arcane fields will
be included (with two --verbose, all fields).  If --quiet is
given no fields will be dumped; really only useful if also give --verify
to do syntax checking.



EOF
}

sub dump_metainfo($;$)
{	my $metainfo = shift;
	my $verbose = shift || 0;
	die "Invalid metainfo $metainfo, expecting hash ref at "
		unless $metainfo && ref($metainfo) eq 'HASH';

	my @all_fields = keys %$metainfo;
	my %all_fields = map { $_ => undef } @all_fields;
	my @std_fields = (
		"DirectoryArchived",
		"DirectoryOwnerUsername",
		"DirectoryOwnerFullname",
		"DirectoryOwnerProjects",
		"Expiration",
		"ReasonForArchiving",
	);
	my @verbose_fields = (
		"DirectoryArchived",
		"DirectoryType",
		"ArchivalDate",
		"ArchivalHost",
		"ArchivedBy",
		"DirectoryOwnerUsername",
		"DirectoryOwnerUID",
		"DirectoryOwnerFullname",
		"DirectoryOwnerProjects",
		"ExpirationTstamp",
		"Expiration",
		"ReasonForArchiving",
		"ls-ld_Output",
		"ls-nd_Output",
		"ExtraLines",
	);

	my @fields = @std_fields;
	@fields = @verbose_fields if $verbose;
	if ( $verbose > 1 )
	{	delete @all_fields{@fields};
		my @temp = sort (keys %all_fields);
		push @fields, @temp;
	}

	FIELD: foreach my $fld (@fields)
	{	delete $all_fields{$fld};
		my $val = $metainfo->{$fld};
		next FIELD unless defined $val;

		if ( ref($val) eq 'ARRAY' )
		{	$val = join "\n\t", @$val;
			$val = "\n\t$val";
		}

		print "$fld: $val\n";
	}

}

#------------- Main

my $help;
my $file;
my $verbose;
my $verify;
my $quiet;
my $DEBUG = 1;

my $DEFSUFF = ".readme";

my $res = GetOptions(
	'help!' => \$help,
	'verbose+' => \$verbose,
	'verify+' => \$verify,
	'quiet!' => \$quiet,
	'file=s' => \$file,
);

unless ( $res )
{	usage;
	die "Error parsing arguments.  Aborting.\n";
}

if ( $help )
{	usage;
	exit 0;
}

if ( @ARGV )
{	unless ( $file )
	{	$file = shift @ARGV;
	}
}

if ( @ARGV )
{	usage;
	my $temp = join ", ", @ARGV;
	die "Extraneous arguments [ $temp ], aborting\n";
}

unless ( $file )
{	usage;
	die "You must provide a path to a metainfo file\n";
}

if ( $quiet && $verbose )
{	usage;
	die "--quiet and --verbose are mutually exclusive.\n";
}


if ( $quiet && ! $verify )
{	warn "Using --quiet without --verify is not very useful.\n";
}


my $newfile;
if ( -e $file )
{	#File exists, if directory try appending .readme
	if ( -d $file )
	{	$newfile = $file . $DEFSUFF;
		if ( -f $newfile )
		{	#This is a file, use it
			print STDERR "[DEBUG] Using $newfile\n" if $DEBUG;
			$file = $newfile;
		} else
		{	die "$file is a directory, and no file $newfile " .
				"found.  Aborting.\n";
		}
	} elsif ( ! -f $file )
	{	die "$file is not a normal file or directory, aborting.\n";
	}
} else
{	#File does not appear to exist.
	#Try appending .readme
	$newfile = $file . $DEFSUFF;
	if ( -f $newfile )
	{	$file = $newfile;
		print STDERR "[DEBUG] Using $newfile.\n" if $DEBUG;
	} else
	{	die "$file nor $newfile found.  Aborting.\n";
	}
}

my $metainfo = parse_archive_metainfo_file($file, $verify);
die "Error parsing metainfo in '$file'\n$metainfo\n"
	unless ( $metainfo && ref($metainfo) eq 'HASH' );

unless ( $quiet )
{	dump_metainfo($metainfo, $verbose);
}
