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

use strict;
use XML::LibXML;

use lib '/usr/local/mpiwg/archive_devel';
use MPIWGStor;

# make output unbuffered
$|=1;

# program version
my $version = "0.1 (27.6.2006 ROC)";
my $help = 
"use: checkmeta [options] dir
options:
  -debug  show debugging info
  -dry-run  simulate, dont'do anything
  -access=free  adds free access tag (use access=mpiwg for restricted access)
";
logger("INFO", "checkmeta $version");


#######################################################
# internal parameters
#

# read command line parameters
my $args = MPIWGStor::parseargs;
if (! scalar(%$args)) {
    print $help, "\n";
    exit 1;
}

# debug level
$debug = (exists $$args{'debug'}) ? $$args{'debug'} : 0;

# simulate action only
my $dry_run = (exists $$args{'dry-run'}) ? $$args{'dry-run'} : 0;
logger('DEBUG', "dry-run: $dry_run");

# access type
my $access_type = (exists $$args{'access'}) ? $$args{'access'} : "";
logger('DEBUG', "access: $access_type");

# index.meta namespace (not really implemented!)
my $namespace = "";


my $xml_changed = 0;
my $errcnt = 0;
my $warncnt = 0;

#######################################################
# check parameters that were passed to the program
#
my $docdir = $$args{'path'};
if (! $docdir) {
    logger("ABORT", "no document directory given!");
    exit 1;
}
# strip double slashes
$docdir = sstrip($docdir, 1);
if (! -d $docdir) {
    logger("ABORT", "document directory \'$docdir\' doesn't exist!");
    exit 1;
}


#######################################################
# subroutines
#

#
# sets access tag to given access_type
# removes access tag if access_type is empty
#
sub change_access {
    my ($access_type, $index_root) = @_;

    my $parent_tag = $index_root->findnodes('//meta/access-conditions')->get_node(1);
    if ($parent_tag) {
	my $access_tag = $parent_tag->findnodes('access')->get_node(1);
	if ($access_tag) {
	    $parent_tag->removeChild($access_tag);
	}
    } else {
	$parent_tag = create_element_path('meta/access-conditions', $index_root, $namespace);
    }

    if ($access_type eq "free") {
	create_element_path('access@type=free', $parent_tag, $namespace);
    } elsif (length $access_type > 0) {
	my $acc_tag = create_element_path('access@type=institution', $parent_tag, $namespace);
	create_text_path('name', $access_type, $acc_tag, $namespace);
    }
    $xml_changed++
}







#######################################################
# Main
#

my $index_doc;
my $index_root;

# load index.meta file
if (-f "$docdir/index.meta") {
    ($index_doc, $index_root) = read_xml("$docdir/index.meta");
} else {
    logger("ABORT", "index file \'$docdir/index.meta\' doesn't exist!");
    exit 1;
}

if ($access_type) {
    change_access($index_root);
}


logger("INFO", "$warncnt warnings");
logger("INFO", "$errcnt errors");
if ($errcnt > 0) {
    logger("ABORT", "there were errors!");
    exit 1;
} elsif ($xml_changed) {
    # write new index.meta file
    if ($dry_run) {
	logger('DEBUG', "would write $docdir/index.meta");
	logger('DEBUG', $index_doc->toString(1));
    } else {
	#write_xml($index_doc, "$docdir/index.meta");
    }
    logger("DONE", "changed index file successfully!");
} else {
    logger("DONE", "didn't change index file");
}

