#!/usr/bin/perl -w

###########################################################################
#
# exportcol.pl --
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 1999 New Zealand Digital Library Project
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###########################################################################

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

use strict;
no strict 'refs'; # allow filehandles to be variables and vice versa
no strict 'subs'; # allow barewords (eg STDERR) as function arguments

use util;
use parse2;
use printusage;

my $arguments = 
    [ 
      { 'name' => "cdname",
	'desc' => "{exportcol.cdname}",
	'type' => "string",
	'deft' => "Greenstone Collections",
	'reqd' => "no" }, 
      { 'name' => "cddir",
	'desc' => "{exportcol.cddir}",
	'type' => "string",
	'deft' => "exported_collections",
	'reqd' => "no" },
      { 'name' => "noinstall",
	'desc' => "{exportcol.noinstall}",
	'type' => "flag",
	'reqd' => "no" },
      { 'name' => "language",
	'desc' => "{scripts.language}",
	'type' => "string",
	'reqd' => "no" },
      { 'name' => "out",
	'desc' => "{exportcol.out}",
	'type' => "string",
	'deft' => "STDERR",
	'reqd' => "no" },
      { 'name' => "xml",
	'desc' => "{scripts.xml}",
	'type' => "flag",
	'reqd' => "no",
	'hiddengli' => "yes" },
      { 'name' => "gli",
	'desc' => "",
	'type' => "flag",
	'reqd' => "no",
	'hiddengli' => "yes" },

      ];

my $options = { 'name' => "exportcol.pl",
		'desc' => "{exportcol.desc}",
		'args' => $arguments };

sub gsprintf
{
    return &gsprintf::gsprintf(@_);
}


&main();

sub main {
    my ($language, $out, $cdname, $cddir);
    
    my $noinstall = 0;
    my $xml = 0;
    my $gli = 0;

    my $hashParsingResult = {};

    # parse options
    my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");

    # If parse returns -1 then something has gone wrong
    if ($intArgLeftinAfterParsing == -1)
    {
	&PrintUsage::print_txt_usage($options, "{exportcol.params}");
	die "\n";
    }
    
    foreach my $strVariable (keys %$hashParsingResult)
    {
	eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
    }
    
    # If $language has been specified, load the appropriate resource bundle
    # (Otherwise, the default resource bundle will be loaded automatically)
    if ($language && $language =~ /\S/) {
	&gsprintf::load_language_specific_resource_bundle($language);
    }
    
    if ($xml) {
        &PrintUsage::print_xml_usage($options);
	print "\n";
	return;
    }
    
    if ($gli) { # the gli wants strings to be in UTF-8
	&gsprintf::output_strings_in_UTF8; 
    }
    
    # can have more than one collection name,  
    # if the first extra option is -h, then output the help
    if (scalar(@ARGV) == 0 || (@ARGV && $ARGV[0] =~ /^\-+h/)) {
	&PrintUsage::print_txt_usage($options, "{exportcol.params}");
	die "\n";
    }

    my @coll_list = @ARGV;

    my $close_out = 0;
    if ($out !~ /^(STDERR|STDOUT)$/i) {
	open (OUT, ">$out") || 
	    (&gsprintf(STDERR, "{common.cannot_open_output_file}\n", $out) && die);
	$out = 'main::OUT';
	$close_out = 1;
    }

    # first we do a quick check to see if the export coll function has been 
    # installed
    my $gssetupexe = &util::filename_cat ($ENV{'GSDLHOME'}, "bin", "windows", "gssetup.exe"); 
    if (!-e $gssetupexe) {
	&gsprintf($out, "{exportcol.fail} {exportcol.export_coll_not_installed}\n");
	die "\n";
    }

    # check each collection
    my @valid_coll_list = ();
    foreach my $c (@coll_list) {
	my $colldir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $c);
	if (! -d $colldir) {
	    &gsprintf($out, "{exportcol.coll_not_found}\n", $c, $colldir);
	    next;
	}
	my $colindexdir = &util::filename_cat ($colldir, "index");
	my $coletcdir = &util::filename_cat ($colldir, "etc");
	if ((!-d $colindexdir) || (!-d $coletcdir)) {
	    &gsprintf($out, "{exportcol.coll_dirs_not_found}\n", $c);
	    &gsprintf($out, "  $colindexdir\n");
	    &gsprintf($out, "  $coletcdir\n");
	    next;
	}
	# the collection seems ok, we add it to the valid coll list
	push @valid_coll_list, $c;
    }

    if (not @valid_coll_list) {
	# no valid colls left
	&gsprintf($out, "{exportcol.fail} {exportcol.no_valid_colls}\n");
	die "\n";
    }

    # create exported directory
    my $topdir = &util::filename_cat ($ENV{'GSDLHOME'}, "tmp", $cddir);
    &util::mk_all_dir ($topdir);
    if (!-d $topdir) {
	&gsprintf($out, "{exportcol.fail} {exportcol.couldnt_create_dir}\n", $topdir);
	die "\n";
    }

    # we create either a self installing cd, or one that runs off the cd (and
    # doesn't install anything

    # create all the directories - we assume that if we created the top dir ok,
    # then all the other mkdirs will go ok
    my $gsdldir;
    if ($noinstall) {
	$gsdldir = $topdir;
    } 
    else {
	$gsdldir = &util::filename_cat ($topdir, "gsdl");
	&util::mk_all_dir ($gsdldir);
    }
    
    my $collectdir = &util::filename_cat ($gsdldir, "collect");
    &util::mk_all_dir ($collectdir);
    my $etcdir = &util::filename_cat ($gsdldir, "etc");
    &util::mk_all_dir ($etcdir);

    #create the config files
    if (!$noinstall) {
	# create the install.cfg file
	my $installcfg = &util::filename_cat ($topdir, "install.cfg");
	if (!open (INSTALLCFG, ">$installcfg")) {
	    &gsprintf($out, "{exportcol.fail} {exportcol.couldnt_create_file}\n", $installcfg);
	    die "\n";
	}
	print INSTALLCFG "CompanyName:New Zealand Digital Library\n";
	print INSTALLCFG "CollectionName:$cdname\n";
	print INSTALLCFG "CollectionDirName:$cdname\n";
	print INSTALLCFG "CollectionVersion:1.0\n";
	print INSTALLCFG "CollectionVolume:1\n";
	print INSTALLCFG "ProgramGroupName:Greenstone\n";
	close INSTALLCFG;
	
	# create the manifest.cfg file
	my $manifestcfg = &util::filename_cat ($topdir, "manifest.cfg");
	if (!open (MANIFESTCFG, ">$manifestcfg")) {
	    &gsprintf($out, "{exportcol.fail} {exportcol.couldnt_create_file}\n", $manifestcfg);
	    die "\n";
	}
	print MANIFESTCFG "all:\n";
	print MANIFESTCFG "  {library} {collection}\n\n";
	print MANIFESTCFG "library:\n";
	print MANIFESTCFG "  server.exe\n\n";
	print MANIFESTCFG "database:\n";
	print MANIFESTCFG '  etc ';
	foreach my $c (@valid_coll_list) {
	    print MANIFESTCFG "collect\\$c\\index\\text\\$c.gdb ";
	}
	print MANIFESTCFG "\n\n";
	print MANIFESTCFG "collection:\n";
	print MANIFESTCFG "  collect etc macros mappings web\n";
	close MANIFESTCFG;
	
    }	

    #create the autorun.inf file
    my $autoruninf = &util::filename_cat ($topdir, "Autorun.inf");
    if (!open (AUTORUNINF, ">$autoruninf")) {
	&gsprintf($out, "{exportcol.fail} {exportcol.couldnt_create_file}\n", $autoruninf);
	die "\n";
    }
    
    print AUTORUNINF "[autorun]\n";
    if ($noinstall) {
	print AUTORUNINF "OPEN=server.exe\n";
    } else {
	print AUTORUNINF "OPEN=Setup.exe\n";
    }
    close AUTORUNINF;
    
    # copy the necessary stuff from GSDLHOME
    my $webdir = &util::filename_cat ($ENV{'GSDLHOME'}, "web");
    my $macrosdir = &util::filename_cat ($ENV{'GSDLHOME'}, "macros");
    my $mappingsdir = &util::filename_cat ($ENV{'GSDLHOME'}, "mappings");
    my $maincfg = &util::filename_cat ($ENV{'GSDLHOME'}, "etc", "main.cfg");
    my $serverexe = &util::filename_cat ($ENV{'GSDLHOME'}, "bin", "windows", "server.exe");
    my $setupexe = &util::filename_cat ($ENV{'GSDLHOME'}, "bin", "windows", "Setup.exe");

    if ((!-d $webdir) || (!-d $macrosdir) || (!-d $mappingsdir) || (!-e $maincfg) ||
	(!-e $serverexe) || (!-e $gssetupexe) || (!-e $setupexe)) {
	&gsprintf($out, "{exportcol.fail} {exportcol.non_exist_files}\n");
	&gsprintf($out, "  $webdir\n");
	&gsprintf($out, "  $macrosdir\n");
	&gsprintf($out, "  $mappingsdir\n");
	&gsprintf($out, "  $maincfg\n");
	&gsprintf($out, "  $serverexe\n");
	&gsprintf($out, "  $gssetupexe\n");
	&gsprintf($out, "  $setupexe\n");
	die "\n";
    }

    &util::cp_r ($webdir, $gsdldir);
    &util::cp_r ($macrosdir, $gsdldir);
    &util::cp_r ($mappingsdir, $gsdldir);
    &util::cp ($maincfg, $etcdir);
    &util::cp ($serverexe, $gsdldir);

    if (!$noinstall) {
	&util::cp ($gssetupexe, $topdir);
	&util::cp ($setupexe, $topdir);
    }	
    
    # now change the home.dm macro file to a simple version
    my $newmacrodir = &util::filename_cat ($gsdldir, "macros");
    my $exporthome = &util::filename_cat ($newmacrodir, "exported_home.dm");
    my $oldhome = &util::filename_cat ($newmacrodir, "home.dm");
    if (-e $exporthome) {
	&util::rm($oldhome);
	&util::mv($exporthome, $oldhome);
    }

    # copy the collections over 
    foreach my $c (@valid_coll_list) {
	#old directories
	my $colldir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $c);
	my $colindexdir = &util::filename_cat ($colldir, "index");
	my $coletcdir = &util::filename_cat ($colldir, "etc");
	my $colmacrosdir = &util::filename_cat ($colldir, "macros");
	my $colimagesdir = &util::filename_cat ($colldir, "images");
	my $colscriptdir = &util::filename_cat ($colldir, "script");
	my $coljavadir = &util::filename_cat ($colldir, "java");
	my $colstyledir = &util::filename_cat ($colldir, "style");
	my $colflashdir = &util::filename_cat ($colldir, "flash");

	# new coll directory
	my $newcoldir = &util::filename_cat ($collectdir, $c);

	&util::mk_all_dir ($newcoldir);
	&util::cp_r ($colindexdir, $newcoldir);
	&util::rename_gdbm_file(&util::filename_cat ($newcoldir, "index", "text", $c));
	&util::cp_r ($coletcdir, $newcoldir);
	&util::cp_r ($colmacrosdir, $newcoldir) if (-e $colmacrosdir);
	&util::cp_r ($colimagesdir, $newcoldir) if (-e $colimagesdir);
	&util::cp_r ($colscriptdir, $newcoldir) if (-e $colscriptdir);
	&util::cp_r ($coljavadir, $newcoldir) if (-e $coljavadir);
	&util::cp_r ($colstyledir, $newcoldir) if (-e $colstyledir);
	&util::cp_r ($colflashdir, $newcoldir) if (-e $colflashdir);

	# now we need to check the collect.cfg file to make sure it's public
	my $collectcfg = &util::filename_cat ($newcoldir, "etc", "collect.cfg");
	open INFILE, "<$collectcfg";
	open OUTFILE, ">$collectcfg.tmp";
	my $line;
	while ($line = <INFILE>) {
	    if ($line =~ /^\s*public\s+false/) {
		print OUTFILE "public\ttrue\n";
		last; # stop matching once we have found the line
	    } else {
		print OUTFILE "$line";
	    }
	}
	# continue with no checking
	while ($line = <INFILE>) {
	    print OUTFILE "$line";
	}
	close INFILE;
	close OUTFILE;
	&util::mv("$collectcfg.tmp", $collectcfg);
    }
    &gsprintf($out, "{exportcol.success}");

    my $successcolls = "";
    my $first = 1;
    foreach my $c (@valid_coll_list) {
	if ($first) {
	    $first=0;
	} else {
	    $successcolls .=", ";
	}
	$successcolls .= "$c";
    }

    my $gsdl_home = $ENV{'GSDLHOME'};
    my $portable_topdir = $topdir;
    # Disabled this because it isn't currently useful (the GLI applet doesn't do exporting)
    # It doesn't work on Windows, either
    # $portable_topdir =~ s/$gsdl_home/\$GSDLHOME/g;

    &gsprintf($out, "{exportcol.output_dir}\n", $successcolls, $portable_topdir);
    &gsprintf($out, "exportcol.pl succeeded:{exportcol.instructions}\n");
    close OUT if $close_out;
}

