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

###########################################################################
#
# csv2usersDB.pl -- convenience script to batch add users via csv file
# 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 calls csv2usersDB java class. It adds to / replaces the contents on Derby usersDB database

# Saves 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.16.jar:./web/WEB-INF/lib/commons-io-2.21.0.jar:./web/WEB-INF/lib/commons-codec-1.21.0.jar:./web/WEB-INF/lib/commons-csv-1.14.1.jar:./web/WEB-INF/classes org.greenstone.gsdl3.util.csv2usersDB <filename.csv> usersDB [-append]


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;
#no strict 'refs'; # allow filehandles to be variables and vice versa
#no strict 'subs'; # allow barewords (eg STDERR) as function arguments

#use FileHandle;
use util;

#use gsprintf 'gsprintf';
#use printusage;
#use parse2;

if (scalar(@ARGV) <1 || scalar(@ARGV) >2  || $ARGV[0] =~ /--?h(elp)?/ ||
    (scalar(@ARGV) == 2 && $ARGV[1] !~ /-append/)) {

    print STDERR "Usage: csv2usersDB.pl infile.csv [-append] \n";
    print STDERR "Without -append, the list of users in the csvfile will replace those already in the database. However, passwwords of existing users will be retrieved first.\n";
    print STDERR "-append means append the csv users to the existing DB. If a user already exists, the csv fields will be used to update the eisting record\n";
    print STDERR "The csv fields used are :\n";
    print STDERR "   username, email, groups, comment, status, password\n";
    print STDERR "If the password field is empty, and the user exists already in the database, the existing password will be used\n";
    pritn STDERR "For other fields, an empty value will delete any existing value in the DB\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 $io_jar =  &util::filename_cat($ENV{'GSDL3HOME'}, "WEB-INF", "lib", "commons-io-2.21.0.jar");
my $codec_jar =  &util::filename_cat($ENV{'GSDL3HOME'}, "WEB-INF", "lib", "commons-codec-1.21.0.jar");
my $csv_jar = &util::filename_cat($ENV{'GSDL3HOME'}, "WEB-INF", "lib", "commons-csv-1.14.1.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, $io_jar, $codec_jar, $csv_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;    
}

my $append = "";
if (scalar (@ARGV) > 1 && $ARGV[1] =~ /-append/) {
    $append = " -append";
}

my $cmd = "java -Dgsdl3.writeablehome=\"$ENV{'GSDL3HOME'}\" -cp \"$classpath\" org.greenstone.gsdl3.util.csv2usersDB ".$ARGV[0]." usersDB $append";

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