#!/usr/bin/perl
#****************************************************************************
#  ##   ##         #####   #####  ##     **        NoSQL RDBMS - dbm        *
#  ###  ##        ####### ####### ##     **        $Revision: 1.1.1.1 $       *
#  #### ##        ###     ##   ## ##     ************************************
#  #######  ####  #####   ##   ## ##     **   Carlo Strozzi (c) 1998-2000   *
#  ####### ######   ##### ## # ## ##     ************************************
#  ## #### ##  ##     ### ##  ### ##     **           Adapted by            *
#  ##  ### ###### ####### ######  ###### **          Carlo Strozzi          *
#  ##   ##  ####   #####   #### # ###### **     e-mail: carlos@linux.it     *
#****************************************************************************
#   NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.                          *
#   This program comes with ABSOLUTELY NO WARRANTY; for details             *
#   refer to the GNU General Public License.                                *
#****************************************************************************
# NOTE: to edit, set ts=8 in 'vi' (or equivalent)
#       to print, pipe through 'pr -t -e8'
#****************************************************************************
#
# usage: nosql dbm <DBMfile> <command> <key> <value>
#
#****************************************************************************

#                               -ldb    -lndbm    -lgdbm
BEGIN { @AnyDBM_File::ISA = qw(DB_File NDBM_File GDBM_File) }
use strict;
use Fcntl;
use AnyDBM_File ();

my $no_hdr;
my $no_ihdr;

$0 =~ s-.*/-- ;
while ( $ARGV[0] =~ /^-/ ){             # Get args
  $_ = shift ;
  if( /^-N$/ || /^--no-header$/ ){ $no_hdr++ ; next ; }
  if( /^-I$/ || /^--ignore-input-header$/ ){ $no_hdr++ ; next ; }
  die "\n$0: unknown option: $_\n" ;
}

my($file,$command,$key,$data) = @ARGV;

usage() unless $file and $command and defined &{$dbmc::{$command}};

# remove any file extension
my $chop = join '|', qw{db.? pag dir};
$file =~ s/\.($chop)$//;

my %DB = ();
my @range = ();
my($mode, $flags) = $command =~ 
    /^(?:view)$/ ? (0644, O_RDONLY) : (0644, O_RDWR|O_CREAT);

tie %DB, "AnyDBM_File", $file, $flags, $mode || die "Can't tie $file: $!";
dbmc->$command();
untie %DB;

sub usage {
    my $cmds = join "|", sort keys %dbmc::;
    die "usage: nosql dbm filename [$cmds] [key]\n";
}

sub dbmc::insert {
    die "Can't use empty data!\n" unless $data;
    $DB{$key} = $data;
    print "Key $key added/updated\n";
}

sub dbmc::delete {
    die "Sorry, key `$key' doesn't exist!\n" unless $DB{$key};
    delete $DB{$key}, print "`$key' deleted\n";
}

sub dbmc::view {
    # Print the NoSQL table header.
    print "Key\tValue\n---\t-----\n" unless $no_hdr;
    print $key ? "$key\t$DB{$key}\n" : map { "$_\t$DB{$_}\n" if $DB{$_} } keys %DB;
}

sub dbmc::import {
    my $i = 0;
    while(defined($_ = <STDIN>) and chomp) {
    if( ++$i < 3 ) { next unless $no_ihdr; }    # Skip table header.
    ($key,$data) = split /\t/, $_, 2;
    dbmc->insert;
    }
}

########################################################################
# End of program.
########################################################################

