#!/usr/bin/perl -w

use strict;
use OpenIsis;
use Getopt::Std;
use Data::Dumper;
use XML::Simple;

my $config=XMLin();

print Dumper($config);

my %opts;

getopts('d:m:q', \%opts);

my $db_dir = $opts{d};

die "usage: $0 -d [database_dir] -m [database1,database2] " if (! %opts);

#--------------------------------------------------------------------

my $last_tell=0;

my @isis_dirs = ( '.' );	# use dirname as database name

if ($opts{m}) {
	@isis_dirs = split(/,/,$opts{m});
}

my @isis_dbs;

foreach (@isis_dirs) {
	if (-e "$common::isis_data/$db_dir/$_/LIBRI") {
		push @isis_dbs,"$common::isis_data/$db_dir/$_/LIBRI/LIBRI";
	}
	if (-e "$common::isis_data/$db_dir/$_/PERI") {
		push @isis_dbs,"$common::isis_data/$db_dir/$_/PERI/PERI";
	}
	if (-e "$common::isis_data/$db_dir/$_/AMS") {
		push @isis_dbs,"$common::isis_data/$db_dir/$_/AMS/AMS";
	}
	if (-e "$common::isis_data/$db_dir/$_/ARTI") {
#		push @isis_dbs,"$common::isis_data/$db_dir/$_/ARTI/ARTI";
	}
}

foreach my $isis_db (@isis_dbs) {

	my $db = OpenIsis::open( "$isis_db" ) || warn "can't open '$isis_db'";

	my $max_rowid = OpenIsis::maxRowid( $db );

	my $last_pcnt = 0;

	for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
		my $row = OpenIsis::read( $db, $row_id );

		# output current process indicator
		my $pcnt = int($row->{mfn} * 100 / $max_rowid);
		if ($pcnt != $last_pcnt) {
			printf STDERR ("%5d / %5d -- %-2d %%\n",$row->{mfn},$max_rowid,$pcnt) if (! $opts{q});
			$last_pcnt = $pcnt;
		}
	}
}
