#!/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 util;
use parsargv;

sub print_usage {
    print STDERR "\n";
    print STDERR "exportcol.pl: Exports a collection for writing to CD-ROM.\n\n";
    print STDERR "  usage: $0 [options] collection-name\n\n";
    print STDERR "   -out    Filename or handle to print debug info to.\n";
    print STDERR "           The default is STDERR\n";
    print STDERR "   -cdname The name of the cd - this is what will appear in the start menu once the cd is installed.\n";
    print STDERR "           The default is 'Greenstone Collections'.\n\n";
}

&main();

sub main {
    my ($out, $cdname);
    if (!parsargv::parse(\@ARGV, 
			 'out/.*/STDERR', \$out,
			 'cdname/.*/', \$cdname)) {
	
	&print_usage();
	die "\n";
    }
    my $dirname=""; 
    if ($cdname eq "") {
	$cdname = "Greenstone Collections";
	$dirname = "exported_collections";
    } else {
	$dirname = $cdname;
	$dirname =~ s/\s//g;
	$dirname = "exported_".$dirname;
    }

    my @coll_list = @ARGV;

    if (not @coll_list) { # empty list
	&print_usage();
	exit(1);
    }
    
    my $close_out = 0;
    if ($out !~ /^(STDERR|STDOUT)$/i) {
	open (OUT, ">$out") || die "Couldn't open output file $out\n";
	$out = OUT;
	$close_out = 1;
    }

    # check each collection
    my @valid_coll_list = ();
    foreach $c (@coll_list) {
	my $colldir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $c);
	if (! -d $colldir) {
	    print $out "Invalid collection $c, collection not found at $colldir, ignoring\n";
	    next;
	}
	my $colindexdir = &util::filename_cat ($colldir, "index");
	my $coletcdir = &util::filename_cat ($colldir, "etc");
	if ((!-d $colindexdir) || (!-d $coletcdir)) {
	    print $out "Invalid collection $c, one of the following directories does not exist\n\n";
	    print $out "  $colindexdir\n";
	    print $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
	print $out "exportcol.pl failed: no valid collections specified to export\n";
	die "\n";
    }

    # create exported directory
    my $topdir = &util::filename_cat ($ENV{'GSDLHOME'}, "tmp", $dirname);
    &util::mk_all_dir ($topdir);
    if (!-d $topdir) {
	print $out "exportcol.pl failed: couldn't create directory $topdir\n";
	die "\n";
    }

    # make other directories (we'll assume that if we created topdir
    # successfully there'll be no problems creating these)
    my $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);
    my $binjavadir = &util::filename_cat ($gsdldir, "bin", "java");
    &util::mk_all_dir ($binjavadir);

    # create the install.cfg file
    my $installcfg = &util::filename_cat ($topdir, "install.cfg");
    if (!open (INSTALLCFG, ">$installcfg")) {
	print $out "exportcol.pl failed: Could not create $installcfg\n";
	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")) {
	print $out "exportcol.pl failed: Could not create $manifestcfg\n";
	die "\n";
    }
    print MANIFESTCFG "all:\n";
    print MANIFESTCFG "  {library} {collection}\n\n";
    print MANIFESTCFG "library:\n";
    print MANIFESTCFG "  net32 net16 server.exe\n\n";
    print MANIFESTCFG "database:\n";
    print MANIFESTCFG '  etc ';
    foreach $c (@valid_coll_list) {
	print MANIFESTCFG "collect\\$c\\index\\text\\$c.ldb ";
    }
    print MANIFESTCFG "\n\n";
    print MANIFESTCFG "collection:\n";
    print MANIFESTCFG "  collect etc images macros mappings bin\n";
    close MANIFESTCFG;

    #create the autorun.inf file
    my $autoruninf = &util::filename_cat ($topdir, "Autorun.inf");
    if (!open (AUTORUNINF, ">$autoruninf")) {
	print $out "exportcol.pl failed: Could not create $autoruninf\n";
	die "\n";
    }
    
    print AUTORUNINF "[autorun]\n";
    print AUTORUNINF "OPEN=Setup.exe\n";
    close AUTORUNINF;
    
    # copy the necessary stuff from GSDLHOME
    my $imagesdir = &util::filename_cat ($ENV{'GSDLHOME'}, "images");
    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 $gssetupexe = &util::filename_cat ($ENV{'GSDLHOME'}, "bin", "windows", "gssetup.exe");
    my $setupexe = &util::filename_cat ($ENV{'GSDLHOME'}, "bin", "windows", "Setup.exe");
    my $net32dir = &util::filename_cat ($ENV{'GSDLHOME'}, "bin", "windows", "net32");
    my $net16dir = &util::filename_cat ($ENV{'GSDLHOME'}, "bin", "windows", "net16");
    my $netscapedir = &util::filename_cat ($ENV{'GSDLHOME'}, "bin", "windows", "netscape");
    my $win32sdir = &util::filename_cat ($ENV{'GSDLHOME'}, "bin", "windows", "Win32s");
    my $phindjar = &util::filename_cat ($ENV{'GSDLHOME'}, "bin", "java", "Phind.jar");

    if ((!-d $imagesdir) || (!-d $macrosdir) || (!-d $mappingsdir) || (!-e $maincfg) ||
	(!-e $serverexe) || (!-e $gssetupexe) || (!-e $setupexe) || (!-d $net32dir) ||
	(!-d $net16dir) || (!-d $netscapedir) || (!-d $win32sdir) || (!-e $phindjar)) {
	print $out "exportcol.pl failed: One or more of the following necessary\n";
	print $out "files and directories does not exist\n\n";
	print $out "  $imagesdir\n";
	print $out "  $macrosdir\n";
	print $out "  $mappingsdir\n";
	print $out "  $maincfg\n";
	print $out "  $serverexe\n";
	print $out "  $gssetupexe\n";
	print $out "  $setupexe\n";
	print $out "  $net32dir\n";
	print $out "  $net16dir\n";
	print $out "  $netscapedir\n";
	print $out "  $win32sdir\n";
	print $out "  $phindjar\n";
	die "\n";
    }

    &util::cp_r ($imagesdir, $gsdldir);
    &util::cp_r ($macrosdir, $gsdldir);
    &util::cp_r ($mappingsdir, $gsdldir);
    &util::cp ($maincfg, $etcdir);
    &util::cp ($serverexe, $gsdldir);
    &util::cp ($gssetupexe, $topdir);
    &util::cp ($setupexe, $topdir);
    &util::cp_r ($net32dir, $gsdldir);
    &util::cp_r ($net16dir, $gsdldir);
    &util::cp_r ($netscapedir, $topdir);
    &util::cp_r ($win32sdir, $topdir);
    &util::cp ($phindjar, $binjavadir);

    # 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 $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 $colimagesdir = &util::filename_cat ($colldir, "images");
	# new coll directory
	my $newcoldir = &util::filename_cat ($collectdir, $c);

	&util::mk_all_dir ($newcoldir);
	&util::cp_r ($colindexdir, $newcoldir);
	&util::cp_r ($coletcdir, $newcoldir);
	&util::cp_r ($colimagesdir, $newcoldir) if (-e $colimagesdir);

	# now we need to check the collect.cfg file to make sure its public
	my $collectcfg = &util::filename_cat ($newcoldir, "etc", "collect.cfg");
	open INFILE, "<$collectcfg";
	open OUTFILE, ">$collectcfg.tmp";
	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);
    }
    print $out "exportcol.pl succeeded: The exported collections are in $topdir\n";
    close OUT if $close_out;
}
