#!/usr/bin/perl -w

BEGIN {
    die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
    die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
    #unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins");
   # unshift (@INC, "$ENV{'GSDLHOME'}/perllib/classify");
}

#use arcinfo;
use colcfg;
#use plugin;
use docprint;
use util;
use parsargv;
use FileHandle;
use XML::Writer;

&main();
sub print_usage() {
    print STDOUT "Usage: convert_coll_from_gs2.pl [options] coll-name\n";
    print STDOUT "options:\n";
    
}

sub main {

    my ($collectdir);
    # note that no defaults are passed for most options as they're set
    # later (after we check the collect.cfg file)
    if (!parsargv::parse(\@ARGV,
			 'verbosity/\d+/', \$verbosity,
			 'collectdir/.*/', \$collectdir,
			 'faillog/.*/', \$faillog)) {
	&print_usage();
	die "\n";
    }

    # get and check the collection name
    if (($collection = &util::use_collection(@ARGV, $collectdir)) eq "") {
	&print_usage();
	die "\n";
    }

    $collconfigfilename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "etc", "collect.cfg");
    my $collectcfg;
    if (-e $collconfigfilename) {
	$collectcfg = &colcfg::read_collect_cfg ($collconfigfilename);

    }
    $buildconfigfilename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "building", "build.cfg");
    my $buildcfg;
    if (-e $buildconfigfilename) {
	$buildcfg = &colcfg::read_build_cfg ($buildconfigfilename);

    }

    my $outfile = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "building", "buildConfig.xml");
    if (-e $outfile) {
	print STDOUT "buildConfig file already exists! overwriting it!\n";
	
    }

    my $buildtype;
    if (defined $collectcfg->{'buildtype'}) {
	$buildtype = $collectcfg->{'buildtype'};
    } else {
	$buildtype = 'mg';
    }

    my $output = new IO::File(">$outfile");
    my $writer = new XML::Writer(OUTPUT => $output);

    $writer->startTag('buildConfig');
 
    if (defined $collectcfg->{'collectionmeta'}) {
	my $collmeta = $collectcfg->{'collectionmeta'};
	$writer->startTag('metadataList');
	#just do collname for now
	my $collnames = $collmeta->{'collectionname'};
	foreach $lang (keys %$collnames) {
	    my $short_lang;
	    if ($lang eq 'default') {
		$short_lang = 'en';
	    } else {
	        ($short_lang) = $lang =~ /^\[l=(..)\]/;
	    }
	    $writer->startTag('metadata', 'lang'=>$short_lang, 'name'=>'collName');
	    $writer->characters($collnames->{$lang});
	    $writer->endTag('metadata');
	}
	my $collabouts = $collmeta->{'collectionextra'};
	foreach $lang (keys %$collabouts) {
	    my $short_lang;
	    if ($lang eq 'default') {
		$short_lang = 'en';
	    } else {
	        ($short_lang) = $lang =~ /^\[l=(..)\]/;
	    }
	    $writer->startTag('metadata', 'lang'=>$short_lang, 'name'=>'collDescript');
	    $writer->characters($collabouts->{$lang});
	    $writer->endTag('metadata');
	}
	$writer->endTag('metadataList');
    }
    $writer->startTag('serviceRackList');
    
    if ($buildtype =~ /^mg/) {
	if ($buildtype eq 'mgpp') {
	    #for each level
	    my @levels = ('Document');
	    my $defaultlevel = 'Document';
	    if (defined $collectcfg->{'levels'}) {
		push @levels, @{$collectcfg->{'levels'}};

		foreach $l(@levels){
		    $defaultlevel = 'Section' if $l eq 'Section';
		}
	    }
	   
	    $writer->startTag('serviceRack', 'name'=>'GS2MGPPRetrieve');
	    $writer->emptyTag('defaultLevel', 'name'=>$defaultlevel);
	    $writer->endTag('serviceRack');
	    
	    $writer->startTag('serviceRack', 'name'=>'GS2MGPPSearch');
	    $writer->emptyTag('defaultLevel', 'name'=>$defaultlevel);
	    $writer->startTag('levelList');
	    foreach $level (@levels) {
		$writer->emptyTag('level', 'name'=>$level);
	    }
	    $writer->endTag('levelList');
	    
            #fieldlist
	    
	} elsif ($buildtype eq 'mg') {
	    $writer->emptyTag('serviceRack', 'name'=>'GS2MGRetrieve');
	    $writer->startTag('serviceRack', 'name'=>'GS2MGSearch');
	}
	
	#indexes
	my $indexmap = {};
	if (defined $buildcfg->{'indexmap'}) {
	    $indexmap_t = $buildcfg->{'indexmap'};
	    foreach $i (@$indexmap_t) {
		my ($k, $v) = $i =~ /^(.*)\-\>(.*)$/;
		$indexmap->{$k} = $v;
	    }
	} else {
	    print STDERR "indexmap not defined";
	}
	my $defaultindex;
	if (defined $collectcfg->{'defaultindex'}) {
	    $defaultindex = $collectcfg->{'defaultindex'};
	} else {
	    print STDERR "defaultindex not defined";
	}
	$defaultindex = $indexmap->{$defaultindex};
	$writer->emptyTag('defaultIndex', 'name'=>$defaultindex);
	$writer->startTag('indexList');
	#for each index
	foreach $i (keys %$indexmap) {
	    $index = $indexmap->{$i};
	    $writer->emptyTag('index', 'name'=>$index);
	}	
	$writer->endTag('indexList');
	
	$writer->endTag('serviceRack');
    }

    #my $classifiers = {};
    my $count = 1;
    my $phind = 0;
    my $started_classifiers = 0;
    if (defined $collectcfg->{'classify'}) { 
	my $classifiers = $collectcfg->{'classify'};
	foreach $cl (@$classifiers) {
	    print STDERR "cl=$cl\n";
	    $name = "CL$count";
	    $count++;
	    my ($classname) = @$cl[0];
	    if ($classname eq "Phind") {
		$phind=1;
		next;
	    }
	    
	    if (not $started_classifiers) {
		$writer->startTag('serviceRack', 'name'=>'GS2Browse');
		$writer->startTag('classifierList');
		$started_classifiers = 1;
	    }
	    my $title = ''; #use buttonname if defined, otherwise use metadata
	    
	    for ($i=0; $i<scalar(@$cl); $i++) {
		$arg = @$cl[$i];
		if ($arg eq "-buttonname") {
		    $title = @$cl[$i+1];
		    last;
		}
		if ($arg eq "-metadata") {
		    $title = @$cl[$i+1];
		}
	    }

	    $writer->startTag('classifier', 'name'=>$name);
	    $writer->startTag('metadataList');
	    #title meta
	    $writer->startTag('metadata', 'name'=>'Title');
	    $writer->characters($title);
	    $writer->endTag('metadata');
	    $writer->endTag('metadataList');
	    $writer->endTag('classifier');
	    
	    
	} #foreach classifier
	if ($started_classifiers) {
	    # end the classifiers
	    $writer->endTag('classifierList');
	    $writer->endTag('serviceRack');
	}
	if ($phind) {
	    # if phind classifier
	    $writer->emptyTag('serviceRack', 'name'=>'PhindPhraseBrowse');
	}
	
    }
    $writer->endTag('serviceRackList');
    $writer->endTag('buildConfig');
    $writer->end();
    $output->close();

}

#$writer->startTag('');
#$writer->endTag('');
#$writer->characters();
#$writer->emptyTag('');

1;
