#!/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\n";
}

&main();

sub main {
    my ($out, $collection);
    if (!parsargv::parse(\@ARGV, 'out/.*/STDERR', \$out)) {
	&print_usage();
	die "\n";
    }

    # get and check the collection name
    if (($collection = &util::use_collection(@ARGV)) eq "") {
	&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;
    }

    # make sure the collection is built (or at least contains all the
    # directories we expect it to) - we expect that it should contain an
    # index and an etc directory and will also copy across an images
    # directory if it exists
    my $colindexdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collection, "index");
    my $colimagesdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collection, "images");
    my $coletcdir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collection, "etc");
    if ((!-d $colindexdir) || (!-d $coletcdir)) {
	print $out "exportcol.pl failed: One or more the the following necessary\n";
	print $out "directories does not exist\n\n";
	print $out "  $colindexdir\n";
	print $out "  $coletcdir\n";
    }

    # create exported directory
    my $topdir = &util::filename_cat ($ENV{'GSDLHOME'}, "tmp", "exported_$collection");
    &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:$collection\n";
    print INSTALLCFG "CollectionDirName:$collection\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 collect\$(COLDIRNAME)\index\text\$(COLDIRNAME).ldb' . "\n\n";
    print MANIFESTCFG "collection:\n";
    print MANIFESTCFG "  collect etc images macros mappings bin\n";
    close MANIFESTCFG;

    # 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 the 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);

    # copy the collection itself
    my $thiscoldir = &util::filename_cat ($collectdir, $collection);
    &util::mk_all_dir ($thiscoldir);
    &util::cp_r ($colindexdir, $thiscoldir);
    &util::cp_r ($coletcdir, $thiscoldir);
    &util::cp_r ($colimagesdir, $thiscoldir) if (-e $colimagesdir);

    print $out "exportcol.pl succeeded: The exported collection is in $topdir\n";
    close OUT if $close_out;
}
