#!/usr/bin/env perl
use warnings;

###########################################################################
#
# usersDB2txt.pl -- convenience script to access derby usersDB databases produced by Greenstone3
#
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 2026 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.
#
###########################################################################


# Wrapper script that gives similar command-line functionality to the 
# db2txt and txt2db scripts available for GDBM databases

# Save you having to type in the mouthful:
#    java -Dgsdl3.writablehome=/full/path/to/GS3/web -cp ./web/WEB-INF/lib/gsdl3.jar:./web/WEB-INF/lib/gutil.jar:./web/WEB-INF/lib/derby.jar:./web/WEB-INF/lib/derbyclient.jar:./web/WEB-INF/lib/log4j-1.2.8.jar:./web/WEB-INF/classes org.greenstone.gsdl3.util.usersDB2txt usersDB



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

use strict;
use util;

use File::Basename;

if (scalar(@ARGV) >= 1 ) {
    my ($progname,$dir) = &File::Basename::fileparse($0);

    print STDERR "Usage: $progname \n";
    print STDERR "Note, your Greenstone environment must be set, and the derby";
    print STDERR "server must be running\n";
    exit -1;
}

my $gsdl3_jar = &util::filename_cat($ENV{'GSDL3HOME'}, "WEB-INF", "lib", "gsdl3.jar");
my $gutil_jar = &util::filename_cat($ENV{'GSDL3HOME'}, "WEB-INF", "lib", "gutil.jar");
my $derby_jar = &util::filename_cat($ENV{'GSDL3HOME'}, "WEB-INF", "lib", "derby.jar");
my $derbyclient_jar = &util::filename_cat($ENV{'GSDL3HOME'}, "WEB-INF", "lib", "derbyclient.jar");
my $log4j_jar = &util::filename_cat($ENV{'GSDL3HOME'}, "WEB-INF", "lib", "log4j-1.2.16.jar");
my $classes_dir = &util::filename_cat($ENV{'GSDL3HOME'}, "WEB-INF", "classes");

my $classpath = &util::pathname_cat($gsdl3_jar, $gutil_jar, $derby_jar, $derbyclient_jar, $log4j_jar, $classes_dir);


if ($^O eq "cygwin") {
    # Away to run a java program, using a binary that is native to Windows, so need
    # Windows directory and path separators
    
    $classpath = `cygpath -wp "$classpath"`;
    chomp($classpath);
    $classpath =~ s%\\%\\\\%g;    
}

#&util::envvar_prepend("CLASSPATH",$jdbm_jar);
#&util::envvar_prepend("CLASSPATH",$jdbmwrapper_jar);

my $cmd = "java -Dgsdl3.writeablehome=\"$ENV{'GSDL3HOME'}\" -cp \"$classpath\" org.greenstone.gsdl3.util.usersDB2txt usersDB";

if (system($cmd)!=0) {
    print STDERR "Error: Failed to run cmd\n  $cmd\n";
    print STDERR "  $!\n";
    exit -1;
}


