###############################################################################
#
# DBDrivers/TDB.pm -- utility functions for writing to tdb databases. Should be
#                     hauntingly similar to GDBM utility functions.
#
# A component of the Greenstone digital library software from the New Zealand
# Digital Library Project at the University of Waikato, New Zealand.
#
# Copyright (C) 2011-2015 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.
#
###############################################################################

package DBDrivers::TDB;

# Pragma
use strict;

# Libraries
use Cwd;
use ghtml;
use util;
use DBDrivers::GDBM;

sub BEGIN
{
    if (!defined $ENV{'GSDLHOME'} || !defined $ENV{'GSDLOS'}) {
        die("Error! Environment not prepared. Have you sourced setup.bash?\n");
    }
    if (!defined $ENV{'GEXTTDBEDIT_INSTALLED'}) {
        die("Error! Path to TDB binaries not found. Have you sourced setup.bash?\n");
    }
    @DBDrivers::TDB::ISA = ( 'DBDrivers::GDBM' );
}

sub new
{
    my $class = shift(@_);
    return bless ($self, $class);
}

# -----------------------------------------------------------------------------
#   TDB IMPLEMENTATION
# -----------------------------------------------------------------------------

## Ask TDB executables to display debugging information?
my $debug = 0; # 1 to enable

## Should the TDB used a specific affinity?
my $forced_affinity = -1; # zero upwards indicates the affinity

## Default TDB file extension
my $infodb_file_extension = '.tdb';

## @function _get_tdb_executable(string)
#
sub _get_tdb_executable
{
    my $program = shift(@_);
    if (!defined $ENV{GEXTTDBEDIT_INSTALLED} || !-d $ENV{GEXTTDBEDIT_INSTALLED})
    {
	die('Fatal Error! Path to TDB binaries not found. Have you sourced setup.bash?');
    }
    my $program_exe = &util::filename_cat($ENV{GEXTTDBEDIT_INSTALLED} . '/bin/' . $program . &util::get_os_exe());
    if (!-x $program_exe)
    {
	die('Fatal Error! File doesn\'t exist or isn\'t executable: ' . $program_exe);
    }
    return $program_exe;
}
## get_tdb_executable(string) => string ##


# With infodb_handle already set up, these functions work the same as parent version
# sub close_infodb_write_handle {}
# sub delete_infodb_entry {}
# sub write_infodb_entry {}
# sub write_infodb_rawentry {}


## @function open_infodb_write_handle(string, string)
#
sub open_infodb_write_handle
{
    my $infodb_file_path = shift(@_);
    my $opt_append = shift(@_);

    my $txt2tdb_exe = &_get_tdb_executable('txt2tdb');

    my $pool_key = $infodb_file_path;
    my $cmd = '"' . $txt2tdb_exe . '"';
    if ((defined $opt_append) && ($opt_append eq "append")) {
        $cmd .= ' -append';
    }
    $cmd .= ' "' . $infodb_file_path . '"';
    # Optional flags
    if ($forced_affinity >= 0) {
        $cmd = 'taskset -c 5 ' . $cmd;
    }
    if ($debug) {
        $cmd .= ' -debug';
    }

    # we're going to pipe the key value pairs, in the appropriate format, from
    # within the buildproc, so we create a piped handle here
    my $infodb_file_handle = undef;
    print STDERR "tdb::open_infodb_write_handle(" . $infodb_file_path . ")\n";
    if(!open($infodb_file_handle, "| $cmd")) {
        print STDERR "Error: Failed to open pipe to $cmd\n";
        print STDERR "       $!\n";
        return undef;
    }
    binmode($infodb_file_handle,":utf8");
    return $infodb_file_handle;
}
## open_infodb_write_handle(string, string) => filehandle ##


## @function get_infodb_file_path(string, string)
#
sub get_infodb_file_path
{
    my $collection_name = shift(@_);
    my $infodb_directory_path = shift(@_);
    my $infodb_file_name = &util::get_dirsep_tail($collection_name) . $infodb_file_extension;
    return &util::filename_cat($infodb_directory_path, $infodb_file_name);
}
## get_infodb_file_path(string, string) => string ##


## @function read_infodb_file
#
sub read_infodb_file
{
    my $infodb_file_path = shift(@_);
    my $infodb_map = shift(@_);

    my $tdb2txt_exe = &_get_tdb_executable('tdb2txt');

    if (!open (PIPEIN, '"' . $tdb2txt_exe . '" "' . $infodb_file_path . '" |')) {
        print STDERR 'Error: Failed to open pipe to ' . $tdb2txt_exe . "\n";
        print STDERR "       $!\n";
        return undef;
    }

    binmode(PIPEIN,":utf8");

    my $infodb_line = "";
    my $infodb_key = "";
    my $infodb_value = "";
    while (defined ($infodb_line = <PIPEIN>)) {
        if ($infodb_line =~ /^\[([^\]]+)\]$/) {
            $infodb_key = $1;
        }
        elsif ($infodb_line =~ /^-{70}$/) {
            $infodb_map->{$infodb_key} = $infodb_value;
            $infodb_key = "";
            $infodb_value = "";
        }
        else {
            $infodb_value .= $infodb_line;
        }
    }
    close (PIPEIN);
}
## read_infodb_file(string, hashmap) => void ##


## @function read_infodb_keys(string, hashmap)
#
sub read_infodb_keys
{
    my $infodb_file_path = shift(@_);
    my $infodb_map = shift(@_);

    my $tdbkeys_exe = &_get_tdb_executable('tdbkeys');

    if (!open (PIPEIN, '"' . tdbkeys_exe . '" "' . $infodb_file_path . '" |')) {
        die("Error! Couldn't open pipe from read_infodb_keys: $infodb_file_path\n$!\n");
    }

    binmode(PIPEIN,":utf8");

    my $infodb_line = "";
    my $infodb_key = "";
    my $infodb_value = "";
    while (defined ($infodb_line = <PIPEIN>)) {
        # remove end of line
        chomp $infodb_line;
        $infodb_map->{$infodb_line} = 1;
    }

    close (PIPEIN);
}
## read_infodb_keys(string, hashmap) => void ##


## @function set_infodb_entry(string, string, hashmap)
#
sub set_infodb_entry
{
    my $infodb_file_path = shift(@_);
    my $infodb_key = shift(@_);
    my $infodb_map = shift(@_);

    # Protect metadata values that go inside quotes for tdbset
    foreach my $k (keys %$infodb_map) {
        my @escaped_v = ();
        foreach my $v (@{$infodb_map->{$k}}) {
            if ($k eq "contains") {
                # protect quotes in ".2;".3 etc
                $v =~ s/\"/\\\"/g;
                push(@escaped_v, $v);
            }
            else {
                my $ev = &ghtml::unescape_html($v);
                $ev =~ s/\"/\\\"/g;
                push(@escaped_v, $ev);
            }
        }
        $infodb_map->{$k} = \@escaped_v;
    }

    # Generate the record string
    my $serialized_infodb_map = &_convert_infodb_hash_to_string($infodb_map);

    # Store it into GDBM
    my $tdbset_exe = &_get_tdb_executable('tdbset');
    my $cmd = '"' . $tdbset_exe . '" "' . $infodb_file_path . '" "' . $infodb_key . '" "' . $serialized_infodb_map . '"';
    my $status = system($cmd);

    return $status;
}
## set_infodb_entry(string, string, hashmap) => integer ##

1;
