#!/usr/bin/perl -w


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


# create_distributions.pl creates the following distribution source directories
# from the gsdl directory pointed to by $GSDLHOME (although it uses the 
# main SVN repository to get the latest source code distribution).

# CDROM - main distribution source: gsdl-x.xx-cdrom
# UNIX - some additional unix specific stuff: gsdl-x.xx-unix
# These first two rely on InstallShield X installer to create the actual distributions (Windows/LInux/Mac, web and CD-ROM)
# Source - a source distribution, which just needs to be tarred and gzipped up.

#    creates ChangeLog using `cvs2cl.pl -P -F trunk -r -S -l "-d'date<tomorrow'"
#    where date of last distribution is read from the last line of 
#    $GSDLHOME/DistDates or from -ChangeLogDate command line option
#    should use something like cvs2cl.pl ... -l "-r'last_tag' -r'this_tag'" but
#    I can't get cvs to ignore files which contain neither last_tag or this_tag
#    so ChangeLog contains lots of ancient stuff 

#    creates a copy of everything in svn repository in $tmpdir

#    edits /tmp/gsdl/etc/VERSION, /tmp/gsdl/src/w32server/fnord.cpp
#    and /tmp/gsdl/cgi-bin/gsdlsite.cfg to use new version number
#    and default GSDLHOME

# temporary working directory

my $tmpdir = '/tmp/tmp';

# docs directory - up-to-date copy of everything to go in the docs directory 
# (including the README.txt) is expected to live here
my $docs_dir = '/home/nzdl/gsdl-docs';

my $for_distributions_dir = '/home/nzdl/for-distributions';

# where the windows binaries live (including library.exe and server.exe)
# this currently relies on being a directory ending in "windows"
my $winbin = "$ENV{'GSDLHOME'}/bin/windows";

# ditto for linux binaries (don't forget getpw)
my $linuxbin = "$ENV{'GSDLHOME'}/bin/linux";

# ditto for Mac OS/X binaries
my $macosxbin = "$ENV{'GSDLHOME'}/bin/darwin";

#my $cvsanon = ':pserver:cvs_anon@cvs.scms.waikato.ac.nz:2402/usr/local/global-cvs/gsdl-src';
my $svnroot = "http://svn.greenstone.org";

use strict;
use parsargv;
use util;

use Cwd;


sub print_usage {
    print STDERR "\n";
    print STDERR "create_distributions.pl: Packages up Greenstone distributions.\n\n";
    print STDERR "  usage: $0 [options]\n\n";
    print STDERR "  options:\n";
    print STDERR "   -tmpdir                temporary directory to use instead of $tmpdir\n";
    print STDERR "   -output_dir            directory to output distributions to\n";
    print STDERR "   -version_num           version number of distribution (x.xx)\n";
    print STDERR "   -svn_tag               svn tag from which to create distributions\n";
    print STDERR "                          - if not set latest versions will be used.\n";
    print STDERR "   -unesco                create unesco style release, i.e. only 4 languages enabled\n";
    print STDERR "   -NoChangeLog           don't create ChangeLog\n";
    print STDERR "   -UseExistingLog        use $ENV{'GSDLHOME'}/ChangeLog rather than creating one\n";
    print STDERR "   -ChangeLogDate         date from which to begin ChangeLog - note that ChangeLog\n";
    print STDERR "                          will always run from date to most recent commits, even if\n";
    print STDERR "                          svn_tag is for a previous revision\n\n";
}

&main ();

my $newtmpdir;
my $version_num;
my $svn_tag;
my $unesco;
my $nochangelog;
my $useexistinglog;
my $changelogdate;
my $output_dir;

sub main
{
    if (!parsargv::parse(\@ARGV, 
			 'tmpdir/.*/', \$newtmpdir,
			 'version_num/\d\.\d\d[a-z]?', \$version_num,
			 'svn_tag/.*/', \$svn_tag,
			 'unesco', \$unesco,
			 'NoChangeLog', \$nochangelog,
			 'UseExistingLog', \$useexistinglog,
			 'ChangeLogDate/.*/', \$changelogdate,
			 'output_dir/.*/', \$output_dir)) {
	&print_usage();
	die "\n";
    }

    $tmpdir = $newtmpdir if (defined $newtmpdir && -d $newtmpdir);
    $output_dir = "." unless $output_dir =~ /\w/;
    mkdir ($output_dir, 0777) unless -d $output_dir;

    if ($svn_tag !~ /\w/) {
	$svn_tag = $version_num;
	$svn_tag =~ s/\./\_/g;
	$svn_tag = "gsdl-" . $svn_tag . "-distribution";
    }

    &create_changelog() unless ($nochangelog || $useexistinglog);
    
    # Export gsdl to $tmpdir
    print STDERR "\nexporting gsdl to $tmpdir\n\n";
    chdir($tmpdir);
	#`svn export $svnroot/gsdl/trunk gsdl`

    #`cvs -d $cvsanon export -D "1 second ago" gsdl`;
    # use something like the following if you want a previous version
	my $output;
	$output=`svn export $svnroot/gsdl/tags/gsdl-2_74-distribution gsdl`;
	print STDERR $output;
    #`cvs -d $cvsanon export -r gsdl-2_70-distribution-branch gsdl`; 
    
    # Export gli to $tmpdir/gli
   chdir(&util::filename_cat($tmpdir, "gsdl"));
	
	print STDERR "\nexporting indexers to " . &util::filename_cat($tmpdir, "gsdl") . "\n\n";
	$output=`svn export -r 14298 $svnroot/indexers/tags/2-74 indexers`;
	print STDERR $output;	

	print STDERR "\nexporting gli to " . &util::filename_cat($tmpdir, "gsdl") . "\n\n";
	$output=`svn export -r 14298 $svnroot/gli/tags/2-74 gli`;
	print STDERR $output;
    #`cvs -d $cvsanon export -D "1 second ago" gli`;

    # use something like the following if you want a previous version
    #`svn export $svnroot/gli/branches/2.74 gli`;
    
    # Compile gli
    &compile_gli();
    
    # Remove bits used by none of the distributions
    &util::rm(&util::filename_cat($tmpdir, "gsdl", "packages", "cpan", "XML-Parser-2.27.tar.gz"));
    
    # copy ChangeLog into $tmpdir/gsdl
    &util::cp(&util::filename_cat($ENV{'GSDLHOME'}, "ChangeLog"), 
	      &util::filename_cat($tmpdir, "gsdl")) unless $nochangelog;
    
    # edit the VERSION and fnord.cpp files
    &edit_files();
    
    &create_unix_distribution();
    &create_source_distribution();
    &create_cdrom_distribution();
}


sub compile_gli
{
    print STDERR "\ncompiling gli...\n";
    chdir(&util::filename_cat($tmpdir, "gsdl", "gli"));
	my $output;
    $output=`./makegli.sh`;
	print STDERR $output;
    
    # JAR up the gli, then delete the class files
    print STDERR "jarring gli...\n";
    `./makejar.sh`;
    &util::rm_r(&util::filename_cat($tmpdir, "gsdl", "gli", "jar"));
	#metadata.zip is deleted by makegli.sh
    #&util::rm(&util::filename_cat($tmpdir, "gsdl", "gli", "metadata.zip"));
	
    &util::rm_r(&util::filename_cat($tmpdir, "gsdl", "gli", "classes", "org"));
    
    # Move the GLIServer.jar file created by makejar.sh into bin/java
    &util::mv(&util::filename_cat($tmpdir, "gsdl", "gli", "GLIServer.jar"), 
	      &util::filename_cat($tmpdir, "gsdl", "bin", "java"));

    # Make the gli-client.zip file
    chdir($tmpdir);
    
    # take a copy of GLI directory
    &util::cp_r(&util::filename_cat($tmpdir, "gsdl", "gli"), $tmpdir);
    
    my $client_dirname = "gli-client-" . $version_num;
    rename ("gli", $client_dirname);
    chdir(&util::filename_cat($tmpdir, $client_dirname));

    
    # delete unnecessary stuff
    `rm -rf clean.* document.* gems.* gli*.sh gli*.bat make*.sh make*.bat lib src`;
    # make sure permissions are ok
    `chmod a+x client-gli.sh client-gli.bat`;
    chdir($tmpdir);
    
    # zip up the client
    &zip($client_dirname, $client_dirname, $tmpdir, 1);
    
    # Move the zip file to the destination directory
    
    &util::cp(&util::filename_cat($tmpdir, "$client_dirname.zip"), $output_dir);
    &util::rm_r(&util::filename_cat($tmpdir, $client_dirname));
    &util::rm(&util::filename_cat($tmpdir, "$client_dirname.zip"));

}

sub create_unix_distribution
{
    print STDERR "Creating Unix distribution...\n";

    my $unix_dist_dir = &util::filename_cat($output_dir, "gsdl-" . $version_num . "-unix");
    mkdir ($unix_dist_dir, 0777);

    # Web release has a slightly customised version of Install.sh
    &util::cp(&util::filename_cat($tmpdir, "gsdl", "Install.sh"), $unix_dist_dir);
    my $Install_sh_file = &util::filename_cat($unix_dist_dir, "Install.sh");
    open (INSTALL_SH, $Install_sh_file) || die "Failed to open $Install_sh_file\n";
    my $found = 0; my $line = ""; my $file = "";
    while (defined ($line = <INSTALL_SH>)) {
	if ($line =~ /^iscdrom=\"/) {
	    $line = "iscdrom=\"no\"\n";
	    $found = 1;
	}
	$file .= $line;
    }
    close INSTALL_SH;

    if ($found != 1) {
	die "Error while editing $Install_sh_file\n";
    }

    open (INSTALL_SH, ">$Install_sh_file") || die;
    print INSTALL_SH $file;
    close INSTALL_SH;
}


sub create_source_distribution
{
    print STDERR "Creating Source distribution...\n";

    my $source_dist_dir = &util::filename_cat($output_dir, "gsdl-" . $version_num . "-src");
    mkdir($source_dist_dir, 0777);

    my $gsdldir = &util::filename_cat ($source_dist_dir, "gsdl");

    # Copy the entire contents of the exported GSDL directory
    my $tmpgsdldir = &util::filename_cat($tmpdir, "gsdl");
    `cp -r $tmpgsdldir $source_dist_dir`;

    # We want the COPYING file in the source distribution too
    &util::cp(&util::filename_cat($docs_dir, "COPYING"), $gsdldir);

    # We shouldn't distribute the GLI applet signed by us
    if (-e &util::filename_cat($source_dist_dir, "gsdl", "bin", "java", "SignedGatherer.jar")) {
	&util::rm(&util::filename_cat($source_dist_dir, "gsdl", "bin", "java", "SignedGatherer.jar"));
    }

    # We don't want the compiled GLI classes in the source distribution (or the GS3 scripts)
    &util::rm(&util::filename_cat($source_dist_dir, "gsdl", "gli", ".greenstonestore"));
    &util::rm(&util::filename_cat($source_dist_dir, "gsdl", "gli", "GLI.jar"));
    &util::rm(&util::filename_cat($source_dist_dir, "gsdl", "gli", "gli4gs3.bat"));
    &util::rm(&util::filename_cat($source_dist_dir, "gsdl", "gli", "gli4gs3.sh"));

}


sub create_cdrom_distribution
{
    print STDERR "Creating CD-ROM distribution...\n";

    my $cdrom_dist_dir = &util::filename_cat ($output_dir, "gsdl-" . $version_num . "-cdrom");
    mkdir ($cdrom_dist_dir, 0777);

    # docs directory
    &install_docs ($cdrom_dist_dir, 0);

    # gsdl directory
    &install_gsdl ($cdrom_dist_dir);

    # gli directory (in gsdl)
    &install_gli($cdrom_dist_dir, "cdrom");

    # src directory
    &install_src ($cdrom_dist_dir, "cdrom");

    # Windows directory
    &install_windows_specific ($cdrom_dist_dir, 1);

    # Unix directory
    &install_unix_specific ($cdrom_dist_dir);

    # for the cd-rom we want to include the unbuilt bits of the demo collection too
    #my $demodir = &util::filename_cat($cdrom_dist_dir, "gsdl", "collect", "demo");
    #my $tmpdemo = &util::filename_cat($tmpdir, "gsdl", "collect", "demo");
    #die "oops, no demo dir\n" unless -d $demodir;
    #&util::cp (&util::filename_cat ($tmpdemo, "demo.col"), $demodir);
    #&util::cp_r (&util::filename_cat ($tmpdemo, "import"), $demodir);
    #&util::cp_r (&util::filename_cat ($tmpdemo, "metadata"), $demodir);

    # collect directory, with documented example collections 
    my $cdrom_collect_dir = &util::filename_cat ($cdrom_dist_dir, "collect");
    mkdir($cdrom_collect_dir, 0777);

    my $source_collect_dir = &util::filename_cat($for_distributions_dir, "collect");
    my $target_collect_dir = &util::filename_cat($cdrom_dist_dir, "collect");

    &util::cp_r(&util::filename_cat($source_collect_dir, "MARC-e"), $target_collect_dir);
    &util::cp_r(&util::filename_cat($source_collect_dir, "authen-e"), $target_collect_dir);
    &util::cp_r(&util::filename_cat($source_collect_dir, "cltbib-e"), $target_collect_dir);
    &util::cp_r(&util::filename_cat($source_collect_dir, "cltext-e"), $target_collect_dir);
    &util::cp_r(&util::filename_cat($source_collect_dir, "dls-e"), $target_collect_dir);
    &util::cp_r(&util::filename_cat($source_collect_dir, "garish"), $target_collect_dir);
    &util::cp_r(&util::filename_cat($source_collect_dir, "gsarch-e"), $target_collect_dir);
    &util::cp_r(&util::filename_cat($source_collect_dir, "image-e"), $target_collect_dir);
    &util::cp_r(&util::filename_cat($source_collect_dir, "isis-e"), $target_collect_dir);
    &util::cp_r(&util::filename_cat($source_collect_dir, "oai-e"), $target_collect_dir);
    &util::cp_r(&util::filename_cat($source_collect_dir, "wrdpdf-e"), $target_collect_dir);
    &util::cp_r(&util::filename_cat($source_collect_dir, "dspace-e"), $target_collect_dir);
    &util::cp_r(&util::filename_cat($source_collect_dir, "style-e"), $target_collect_dir);
    &util::cp_r(&util::filename_cat($source_collect_dir, "pagedimg-e"), $target_collect_dir);

    # Make sure the whole thing is user-writeable
    `chmod -R u+rw $cdrom_dist_dir`;
}


# isweb is 1 if it's one of the web distributions (i.e. if we don't
# want to install the manuals) - this shouldn't be called at all for
# windows web installation as it doesn't use docs at all (not much 
# point for self-extracting exe).
sub install_docs {
    my ($install_dir, $isweb) = @_;

    # COPYING, README*.txt and Support.htm
    &util::cp (&util::filename_cat($docs_dir, "COPYING"), $install_dir);
    &util::cp (&util::filename_cat($docs_dir, "READMEen.txt"), $install_dir);
    &util::cp (&util::filename_cat($docs_dir, "READMEes.txt"), $install_dir);
    &util::cp (&util::filename_cat($docs_dir, "READMEfr.txt"), $install_dir);
    &util::cp (&util::filename_cat($docs_dir, "READMEru.txt-cp1251"), $install_dir);
    &util::cp (&util::filename_cat($docs_dir, "READMEru.txt-koi8-r"), $install_dir);
    &util::cp (&util::filename_cat($docs_dir, "Support.htm"), $install_dir);

    &force_windows_line_endings(&util::filename_cat($install_dir, "COPYING"));
    &force_windows_line_endings(&util::filename_cat($install_dir, "READMEen.txt"));
    &force_windows_line_endings(&util::filename_cat($install_dir, "READMEes.txt"));
    &force_windows_line_endings(&util::filename_cat($install_dir, "READMEfr.txt"));

    # The web distributions don't have any of the manuals
    return if ($isweb);

    # docs directory, but not any of the Word files or Latex files
    &util::cp_r (&util::filename_cat($docs_dir, "docs"), $install_dir);
    my $en_docs = &util::filename_cat ($install_dir, "docs", "english", "*.doc");
    `rm $en_docs`;
    my $es_docs = &util::filename_cat ($install_dir, "docs", "spanish", "*.doc");
    `rm $es_docs`;
    my $ru_docs = &util::filename_cat ($install_dir, "docs", "russian", "*.doc");
    `rm $ru_docs`;
    my $fr_latex = &util::filename_cat ($install_dir, "docs", "french", "latex");
    `rm -r $fr_latex`;
}


sub install_gsdl {
    my ($install_dir) = @_;

    my $gsdldir = &util::filename_cat ($install_dir, "gsdl");
    mkdir ($gsdldir, 0777);

    my $gsdlbindir = &util::filename_cat ($gsdldir, "bin");
    mkdir ($gsdlbindir, 0777);
    &util::cp_r (&util::filename_cat ($tmpdir, "gsdl", "bin", "script"), $gsdlbindir);
    &util::cp_r (&util::filename_cat ($tmpdir, "gsdl", "bin", "java"), $gsdlbindir);
    &util::cp_r (&util::filename_cat ($tmpdir, "gsdl", "cgi-bin"), $gsdldir);
    &util::cp_r (&util::filename_cat ($tmpdir, "gsdl", "collect"), $gsdldir);
    &util::cp_r (&util::filename_cat ($tmpdir, "gsdl", "etc"), $gsdldir);

    #Get the modified main.cfg file with only core language interfaces enabled
    if ($unesco) {
	&util::cp(&util::filename_cat($for_distributions_dir, "etc", "main.cfg"), &util::filename_cat($gsdldir, "etc"));
    }
    # Make sure everything in bin/script is executable
    my $bin_script_dir = &util::filename_cat($gsdldir, "bin", "script");
    `chmod a+x $bin_script_dir/*`;

    # Make sure cgi scripts are executable
    my $gliserver_script = &util::filename_cat($gsdldir, "cgi-bin", "gliserver.pl");
    `chmod a+x $gliserver_script`;
    # should we be doing these ones - usability feedback scripts ??
    #my $perl_script = &util::filename_cat($gsdldir, "cgi-bin", "perl.cgi");
    #`chmod a+x $perl_script`;
    #my $readresults_script = &util::filename_cat($gsdldir, "cgi-bin", "readresults.cgi");
    #`chmod a+x $readresults_script`;
    #my $trackreport_script = &util::filename_cat($gsdldir, "cgi-bin", "trackreport.cgi");
    #`chmod a+x $trackreport_script`;

    &force_windows_line_endings(&util::filename_cat($gsdldir, "cgi-bin", "gsdlsite.cfg"));
    &force_windows_line_endings(&util::filename_cat($gsdldir, "etc", "main.cfg"));
    &force_windows_line_endings(&util::filename_cat($gsdldir, "etc", "oai.cfg"));

    # Make sure certain files in the etc directory are writeable by everyone
    my $error_txt_file = &util::filename_cat($gsdldir, "etc", "error.txt");
    `chmod a+w $error_txt_file`;
    my $history_db_file = &util::filename_cat($gsdldir, "etc", "history.db");
    `chmod a+w $history_db_file`;
    my $key_db_file = &util::filename_cat($gsdldir, "etc", "key.db");
    `chmod a+w $key_db_file`;
    my $main_cfg_file = &util::filename_cat($gsdldir, "etc", "main.cfg");
    `chmod a+w $main_cfg_file`;
    my $usage_txt_file = &util::filename_cat($gsdldir, "etc", "usage.txt");
    `chmod a+w $usage_txt_file`;
    my $users_db_file = &util::filename_cat($gsdldir, "etc", "users.db");
    `chmod a+rw $users_db_file`;  # Readable too

    # We shouldn't distribute the GLI applet signed by us
    if (-e &util::filename_cat($gsdlbindir, "java", "SignedGatherer.jar")) {
	&util::rm(&util::filename_cat($gsdlbindir, "java", "SignedGatherer.jar"));
    }

    &util::cp_r (&util::filename_cat ($tmpdir, "gsdl", "images"), $gsdldir);
    &util::cp_r(&util::filename_cat ($tmpdir, "gsdl", "macros"), $gsdldir);
    &util::cp_r (&util::filename_cat ($tmpdir, "gsdl", "mappings"), $gsdldir);
    &util::cp_r (&util::filename_cat ($tmpdir, "gsdl", "perllib"), $gsdldir);

    # Rename perllib/strings.properties to perllib/strings_en.properties
    my $stringsrbfile = &util::filename_cat ($gsdldir, "perllib", "strings.properties");
    &util::cp ($stringsrbfile, &util::filename_cat ($gsdldir, "perllib", "strings_en.properties"));
    &util::rm ($stringsrbfile);
    `touch $stringsrbfile`;

    # Untar Ping.tgz and XML-Parser.tar.gz
    my $wd = cwd;
    chdir (&util::filename_cat($gsdldir, "perllib", "cpan"));
    `tar xvzf Ping.tgz`;
    unlink ("Ping.tgz");
    &util::cp(&util::filename_cat($for_distributions_dir, "perllib", "cpan", "XML-Parser.tar.gz"),
	      &util::filename_cat($gsdldir, "perllib", "cpan"));
    `tar xvzf XML-Parser.tar.gz`;
    unlink ("XML-Parser.tar.gz");
    chdir ($wd);

    # make sure that modelcol collection contains all the right 
    # empty directories
    my $modelindex = &util::filename_cat ($gsdldir, "collect", "modelcol", "index");
    &util::mk_all_dir ($modelindex) unless -d $modelindex;
    my $modelbuilding = &util::filename_cat ($gsdldir, "collect", "modelcol", "building");
    &util::mk_all_dir ($modelbuilding) unless -d $modelbuilding;
    my $modelarchives = &util::filename_cat ($gsdldir, "collect", "modelcol", "archives");
    &util::mk_all_dir ($modelarchives) unless -d $modelarchives;
    my $modelimport = &util::filename_cat ($gsdldir, "collect", "modelcol", "import");
    &util::mk_all_dir ($modelimport) unless -d $modelimport;
    my $modelperllib = &util::filename_cat ($gsdldir, "collect", "modelcol", "perllib");
    &util::mk_all_dir ($modelperllib) unless -d $modelperllib;
    my $modelimages = &util::filename_cat ($gsdldir, "collect", "modelcol", "images");
    &util::mk_all_dir ($modelimages) unless -d $modelimages;

    # demo collection needs to be pre-built
    my $collectdir = &util::filename_cat ($gsdldir, "collect");
    &util::rm_r (&util::filename_cat($collectdir, "demo"));
    if (!&get_built_collection ("demo", $collectdir)) {
	die "Couldn't get built version of demo collection\n";
    }
#******
    if ($unesco) {
	# Get modified version of demo collect.cfg file with only core language strings
	&util::cp(&util::filename_cat($for_distributions_dir, "collect", "demo", "etc", "collect.cfg"),
		  &util::filename_cat($collectdir, "demo", "etc"));
    }
}


sub install_gli
{
    my ($install_dir, $type) = @_;

    # Copy the Greenstone Librarian Interface
    my $source_dir = &util::filename_cat($tmpdir, "gsdl", "gli");
    my $target_dir = &util::filename_cat($install_dir, "gsdl");
    &util::cp_r($source_dir, $target_dir);

    my $gli_dir = &util::filename_cat($target_dir, "gli");
    &force_windows_line_endings(&util::filename_cat($gli_dir, "READMEen.txt"));
    &force_windows_line_endings(&util::filename_cat($gli_dir, "READMEes.txt"));
    &force_windows_line_endings(&util::filename_cat($gli_dir, "READMEfr.txt"));

    # Make Unix scripts executable
    `chmod a+x $gli_dir/*.sh`;

    # Copy dictionary.properties to dictionary_en.properties to prevent problems with non-English machines
    &util::cp(&util::filename_cat($gli_dir, "classes", "dictionary.properties"),
	      &util::filename_cat($gli_dir, "classes", "dictionary_en.properties"));

    if ($unesco) {
	# Copy the customised languages.xml file with only core languages enabled
	&util::cp(&util::filename_cat($for_distributions_dir, "gli", "classes", "xml", "languages.xml"),
		  &util::filename_cat($gli_dir, "classes", "xml"));
    }
    # Remove unwanted stuff - all distributions
    &util::rm(&util::filename_cat($gli_dir, ".greenstonestore"));

    # Don't need Greenstone 3 scripts
    &util::rm(&util::filename_cat($gli_dir, "gli4gs3.bat"));
    &util::rm(&util::filename_cat($gli_dir, "gli4gs3.sh"));

    # Remove unwanted stuff - Unix distributions
    if ($type eq "unix") {
	# Don't need Windows scripts
	&util::rm(&util::filename_cat($gli_dir, "clean.bat"));
	&util::rm(&util::filename_cat($gli_dir, "document.bat"));
	&util::rm(&util::filename_cat($gli_dir, "gems.bat"));
	&util::rm(&util::filename_cat($gli_dir, "gli.bat"));
	&util::rm(&util::filename_cat($gli_dir, "makegli.bat"));

	# Don't need Windows utilities
	&util::rm_r(&util::filename_cat($gli_dir, "winutil"));

	# Don't need Windows README file
	&util::rm(&util::filename_cat($gli_dir, "READMEru.txt-cp1251"));

	# Rename Unix README file
	&util::cp(&util::filename_cat($gli_dir, "READMEru.txt-koi8-r"),
		  &util::filename_cat($gli_dir, "READMEru.txt"));
	&util::rm(&util::filename_cat($gli_dir, "READMEru.txt-koi8-r"));
    }

    # Remove unwanted stuff - Windows distributions
    if ($type eq "windows") {
	# Don't need Unix scripts
	&util::rm(&util::filename_cat($gli_dir, "clean.sh"));
	&util::rm(&util::filename_cat($gli_dir, "document.sh"));
	&util::rm(&util::filename_cat($gli_dir, "gems.sh"));
	&util::rm(&util::filename_cat($gli_dir, "gli.sh"));
	&util::rm(&util::filename_cat($gli_dir, "makegli.sh"));
	&util::rm(&util::filename_cat($gli_dir, "makejar.sh"));
    }
}


sub install_src {
    my ($install_dir, $type) = @_;

    my $srcdir = &util::filename_cat ($install_dir, "src");
    my $srcwindir = &util::filename_cat ($srcdir, "Windows");
    my $srcunixdir = &util::filename_cat ($srcdir, "Unix");
    mkdir ($srcdir, 0777);
    &util::cp_r (&util::filename_cat ($tmpdir, "gsdl", "lib"), $srcdir);
    &util::cp_r (&util::filename_cat ($tmpdir, "gsdl", "packages"), $srcdir);
    &util::cp_r (&util::filename_cat ($tmpdir, "gsdl", "src"), $srcdir);
    &util::cp (&util::filename_cat ($tmpdir, "gsdl", "Install.txt"), $srcdir);
    &force_windows_line_endings(&util::filename_cat($srcdir, "Install.txt"));

    if ($type ne "unix") {
	mkdir ($srcwindir, 0777);
	&util::cp (&util::filename_cat ($tmpdir, "gsdl", "setup.bat"), $srcwindir);
	&util::cp (&util::filename_cat ($tmpdir, "gsdl", "win32.mak"), $srcwindir);
	&util::cp (&util::filename_cat ($tmpdir, "gsdl", "WIN32cfg.h"), $srcwindir);
    }

    if ($type ne "windows") {
	mkdir ($srcunixdir, 0777);
	&util::cp (&util::filename_cat ($tmpdir, "gsdl", "Makefile.in"), $srcunixdir);
	&util::cp (&util::filename_cat ($tmpdir, "gsdl", "acconfig.h"), $srcunixdir);
	&util::cp (&util::filename_cat ($tmpdir, "gsdl", "aclocal.m4"), $srcunixdir);
	&util::cp (&util::filename_cat ($tmpdir, "gsdl", "config.h.in"), $srcunixdir);
	&util::cp (&util::filename_cat ($tmpdir, "gsdl", "configtest.pl"), $srcunixdir);
	&util::cp (&util::filename_cat ($tmpdir, "gsdl", "configure"), $srcunixdir);
	# make sure configure and setup scripts are executable
	my $configure_script = &util::filename_cat ($srcunixdir , "configure");
	`chmod a+x $configure_script`;

	&util::cp (&util::filename_cat ($tmpdir, "gsdl", "configure.in"), $srcunixdir);
	&util::cp (&util::filename_cat ($tmpdir, "gsdl", "config.sub"), $srcunixdir);
	&util::cp (&util::filename_cat ($tmpdir, "gsdl", "config.guess"), $srcunixdir);
	&util::cp (&util::filename_cat ($tmpdir, "gsdl", "install-sh"), $srcunixdir);
	&util::cp (&util::filename_cat ($tmpdir, "gsdl", "setup.bash"), $srcunixdir);
	&util::cp (&util::filename_cat ($tmpdir, "gsdl", "setup.csh"), $srcunixdir);
	`chmod a+x $srcunixdir/setup.*`;

	# get all the configure scripts
	`cd $srcdir; find -name "configure" -exec chmod a+x {} \\; ; cd -`
	
    }
}

sub install_windows_specific {
    my ($install_dir, $cd_rom) = @_;

    my $windir = &util::filename_cat ($install_dir, "Windows");
    my $winbindir = &util::filename_cat ($windir, "bin");
    mkdir ($windir, 0777);
    mkdir ($winbindir, 0777);
    &util::cp_r ($winbin, $winbindir);

    # make sure there aren't any .svn directories laying around
    &remove_svn_dirs ($windir);
}

sub install_unix_specific {
    my ($install_dir) = @_;

    my $unixdir = &util::filename_cat ($install_dir, "Unix");
    my $unixbindir = &util::filename_cat ($unixdir, "bin");
    mkdir ($unixdir, 0777);
    mkdir ($unixbindir, 0777);
    &util::cp (&util::filename_cat($tmpdir, "gsdl", "Install.sh"), $unixdir);

    # make sure Install.sh is executable
    my $install_sh = &util::filename_cat($unixdir, "Install.sh");
    `chmod a+x $install_sh`;

    # Get Linux binaries, and make sure they're all executable
    &util::cp_r ($linuxbin, $unixbindir);
    my $linuxbindir = &util::filename_cat($unixbindir, 'linux');
    `chmod -R a+x $linuxbindir`;

    # remove the non-static mgquery_old program from distributions
    &util::rm(&util::filename_cat($linuxbindir, "mgquery_old"));

    # Get Mac OS/X binaries, and make sure they're all executable
    &util::cp_r ($macosxbin, $unixbindir);
    my $macosxbindir = &util::filename_cat($unixbindir, 'darwin');
    `chmod -R a+x $macosxbindir`;

    # make sure there aren't any .svn directories laying around
    &remove_svn_dirs ($unixdir);
}


# gets all the right bits of a built collection from 
# $GSDLHOME/collect and copies them to $collect_dir
# returns 1 if successful, 0 if not
sub get_built_collection {
    my ($colname, $collect_dir) = @_;

    my $from_dir = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $colname);
    if (!-d $from_dir) {
	print STDERR "\nERROR: No collection at $from_dir\n";
	return 0;
    }
    
    my $to_dir = &util::filename_cat ($collect_dir, $colname);
    mkdir ($to_dir, 0777) unless -d $to_dir;
    
    # get the built indexes
    my $index_dir = &util::filename_cat ($from_dir, "index");
    if (-d $index_dir) {
	# if build.cfg exists we'll assume collection is built ok
	if (-e &util::filename_cat ($index_dir, "build.cfg")) {
	    &util::cp_r ($index_dir, $to_dir);
	} else {
	    print STDERR "\nERROR: no build.cfg at $index_dir (collection not built?)\n";
	    rmdir ($to_dir);
	    return 0;
	}
    } else {
	print STDERR "\nERROR: collection at $from_dir appears unbuilt (no index directory)\n";
	return 0;
    }
    &util::cp_r ($index_dir, $to_dir);
    
    # get the collect.cfg file
    mkdir (&util::filename_cat($to_dir, "etc"), 0777);
    &util::cp (&util::filename_cat($from_dir, "etc", "collect.cfg"),
	       &util::filename_cat($to_dir, "etc"));

    # get the .col file
    &util::cp (&util::filename_cat($from_dir, "$colname.col"),
	       $to_dir);
    # get any other directories - import, metadata, images, macros, perllib
    my $from_import = &util::filename_cat ($from_dir, "import");
    &util::cp_r ($from_import, $to_dir) if -d $from_import;
    my $from_metadata = &util::filename_cat ($from_dir, "metadata");
    &util::cp_r ($from_metadata, $to_dir) if -d $from_metadata;
    my $from_images = &util::filename_cat ($from_dir, "images");
    &util::cp_r ($from_images, $to_dir) if -d $from_images;
    my $from_macros = &util::filename_cat ($from_dir, "macros");
    &util::cp_r ($from_macros, $to_dir) if -d $from_macros;
    my $from_perllib = &util::filename_cat ($from_dir, "perllib");
    &util::cp_r ($from_perllib, $to_dir) if -d $from_perllib;

    # make sure there aren't any .svn directories laying around
    &remove_svn_dirs ($to_dir);

    return 1;
}

sub force_windows_line_endings
{
    my ($filepath) = @_;

    open(IN, "<$filepath");
    my $text = join('', <IN>);
    close(IN);

    $text =~ s/\n\x0A/\n\x0D\x0A/g;
    $text =~ s/([^\x0D])\x0A/$1\x0D\x0A/g;

    open(OUT, ">$filepath");
    print OUT "$text";
    close(OUT);
}


sub create_changelog {

    my ($tag, $date, $file);

    my $datefile = &util::filename_cat ($for_distributions_dir, "DistDates");
    if ($changelogdate !~ /\w/) {
	# get date from for-distributions/DistDates (and update DistDates)
	open (DATES, $datefile) || die "can't open $datefile\n";
	my $line = "";
	while (defined ($line = <DATES>)) {
	    if ($line =~ /\w/) {
		($tag, $date) = $line =~ /^(\S+)\t(.*)$/;
		$changelogdate = $date unless ($tag eq $svn_tag);
	    }
	    $file .= $line;
	}
	close DATES;
    }

    if ((!defined $tag) || ($tag ne $svn_tag)) {
	open (DATES, ">$datefile") || die;
	print DATES $file if defined $file;
	print DATES "$svn_tag\t" . `date`;
	close DATES;
    }

    print STDERR "Creating ChangeLog from $changelogdate to most recent\n";

    chdir($ENV{'GSDLHOME'});
    my $cmd = "$for_distributions_dir/bin/script/cvs2cl.pl -P -F trunk -r -S -l \"-d'$changelogdate<tomorrow'\"";
    system ($cmd);
}

# simply recurses directory structure beginning at $dir
# and deletes any .svn administrative directories it comes 
# across
sub remove_svn_dirs {
    my ($dir) = @_;

    if (!-d $dir) {return;}

    opendir (DIR, $dir) || die;
    my @files = readdir DIR;
    closedir DIR;

    foreach my $file (@files) {
	next if $file =~ /^\.\.?$/;
	my $fullpath = &util::filename_cat ($dir, $file);
	if (-d $fullpath) {
	    if ($file eq ".svn") {
		&util::rm_r ($fullpath);
	    } else {
		&remove_svn_dirs ($fullpath);
	    }
	}
    }
}

# mode is 0 to create .zip and .tgz, 1 to create .zip only, and 2 to create
# .tgz only
sub zip {
    my ($zip_to, $zip_from, $dir, $mode) = @_; 

    chdir ($dir);

    if ($mode != 2) {
	my $to = $zip_to . ".zip";
	unlink ($to) if -e $to;
	print STDERR "zipping up $to\n";
	`zip -r $to $zip_from`;
    }
    if ($mode != 1) {
	my $to2 = $zip_to . ".tgz";
	unlink ($to2) if -e $to2;
	print STDERR "tarring and gzipping $to2\n";
	print STDERR "tar cvzf $to2 $zip_from\n";
	system ("tar cvzf $to2 $zip_from");
    }
}

sub edit_files {
    # edit VERSION file
    my $version_file = &util::filename_cat ($tmpdir, "gsdl", "etc" , "VERSION");
    open (VERSION, $version_file) || die "failed to open $version_file\n";
    my $found = 0; my $line = ""; my $file = "";
    while (defined ($line = <VERSION>)) {
	if ($line =~ s/(gsdl version: )x\.xx/$1$version_num/) {
	    $found ++;
	} elsif ($line =~ s/(svn tag: )gsdl-x_xx-distribution/$1$svn_tag/) {
	    $found ++;
	}
	$file .= $line;
    }
    close VERSION;
    if ($found != 2) {
	die "error while editing $version_file\n";
    } 
    
    open (VERSION, ">$version_file") || die;
    print VERSION $file;
    close VERSION;
    
    # edit gsdlconf.h
    my $gsdlconf_file = &util::filename_cat ($tmpdir, "gsdl", "lib", "gsdlconf.h");
    open (GSDLCONF, $gsdlconf_file) || die;
    $found = 0; $line = ""; $file = "";
    while (defined ($line = <GSDLCONF>)) {

	if ($line =~ s/(\#define GSDL_VERSION \")x\.xx(\")/$1$version_num$2/) {
	    $found ++;
	}
	$file .= $line;
    }
    close GSDLCONF;

    if ( !$found ) {
		die "error while editing $gsdlconf_file\n";
    } 
    
    open (GSDLCONF, ">$gsdlconf_file") || die;
    print GSDLCONF $file;
    close GSDLCONF;
}

END {
    # remove any temporary files we created
    print STDERR "\ndeleting temporary files\n";
    my $tmp_gsdlsite_file = &util::filename_cat ($tmpdir, "gsdlsite.cfg");
    if (-e $tmp_gsdlsite_file) {
	print STDERR "$tmp_gsdlsite_file\n";
	unlink($tmp_gsdlsite_file);
    }
    my $tmp_gsdlhome = &util::filename_cat ($tmpdir, "gsdl");
    if (-d $tmp_gsdlhome) {
	print STDERR "$tmp_gsdlhome\n";
	&util::rm_r ($tmp_gsdlhome);
    }
}
