#!/usr/bin/perl -w
# vim: set sw=4 ts=4 si et:
# Copyright: GPL, Author: Guido Socher
# $Revision: $, last changed: $Date: $
#
use strict;
use vars qw($opt_a $opt_o $opt_g $opt_i $opt_h);
use Getopt::Std;
# global vars:
my $textdb="textdb.txt";
# prototypes:
sub indexsearch($$);
sub agrepsearch($$);
sub grepsearch($$);
sub print_entry($$);
sub help();
#
getopts("a:g:i:ho:")||die "ERROR: No such option. -h for help\n";
help() if ($opt_h);
$opt_o="n" unless($opt_o); # default is n=normal
if ($opt_i){
    indexsearch($opt_i,$opt_o);
}elsif($opt_g){
    grepsearch($opt_g,$opt_o);
}elsif($opt_a){
    agrepsearch($opt_a,$opt_o);
}else{
    help();
}
#
sub indexsearch($$){
    my $numbers=shift;
    my $format=shift;
    my %numhash=();
    for my $n (split(/\D/,$numbers)){
        $numhash{$n}=1;
    }
    my $forfastsearch;
    my @data;
    open(DB,"<$textdb")||die "ERROR: can not read $textdb\n";
    while(<DB>){
        next unless(/^art/);
        $forfastsearch=substr($_,0,25); # accellerate the split
        @data=split(/~/,$forfastsearch);
        if ($numhash{$data[1]}){
            chomp;
            print_entry($format,\$_);
        }
    }
    close DB;
}

#
sub agrepsearch($$){
    my $str=shift;
    my $format=shift;
    my $qstr="x";
    # disallow non printable characters:
    if ($str=~/([ -~]+)/){
        $qstr=quotemeta($1);
    }else{
        return 0;
    }
    open(DB,"agrep -i -1 $qstr $textdb |")||die;
    while(<DB>){
        next unless(/^art/);
        chomp;
        print_entry($format,\$_);
    }
    close DB;
}

#
sub grepsearch($$){
    my $str=shift;
    my $format=shift;
    $str=~s/%/./; # do not ruin our delimiter
    my @data;
    open(DB,"<$textdb")||die "ERROR: can not read $textdb\n";
    while(<DB>){
        next unless(/^art/);
        if (m%$str%oi){
            chomp;
            print_entry($format,\$_);
        }
    }
    close DB;
}


#
# formated printout, format can be one of s, n, l, 
sub print_entry($$){
    my $format=shift;
    my $dataref=shift; # pointer to data, ~ seperated
    my @data=split(/~/,$$dataref);
    # Issue numeric :: Issue :: article num :: Theme
    print "$data[4] :: $data[3] :: $data[1] :: $data[2]\n";
    # title
    print " $data[6]\n";
    goto ENDE if ($format eq "s");
    # personid :: Author name (ascii) :: Author email
    print "  $data[8] :: $data[10] :: $data[9]\n";
    goto ENDE if ($format eq "n");
    # Abstract:
    print "   $data[12]\n";
    ENDE:
    print "\n";
}
#
sub help(){
    print "querydb_pl -- print data from database
USAGE: querydb_pl [-o s|n|l] -i id1+id2+...
or
USAGE: querydb_pl [-o s|n|l] -g grep-searchstring
or
USAGE: querydb_pl [-o s|n|l] -a approx-grep-searchstring

OPTIONS: -h this help
         -g grep search through the db
         -a approximate grep search (string is allowed to mismatch by
         one character) through the db
         -i search by numerical id 
         -o output format. Can be one of 
            s: short, n: normal=default, l:long

EXAMPLE: querydb_pl -o s -i 222
\n";
exit;
}
__END__ 

