#!/usr/bin/perl
# *********************************************************************
#  Original code: indextbl,v 1.13 1994/06/20 10:46:33 hobbs
#
#  Adapted to NoSQL by Carlo Strozzi <carlos@linux.it>.
#
#  index: NoSQL table index builder.
#  Copyright (C) 1998-2003 Carlo Strozzi <carlos@linux.it>
# 
#  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.
#
# *********************************************************************
#  $Id: index,v 1.2 2003/03/17 10:10:46 carlo Exp $

# Get local settings and set defaults.

$NOSQL_INSTALL = $ENV{'NOSQL_INSTALL'};
$NOSQL_INSTALL = "/usr/local/nosql" if not $NOSQL_INSTALL;

$0 =~ s-.*/-- ;
while ( $ARGV[0] =~ /^-/ ){             # Get args
    $_ = shift ;
    if( /^-a.*/ || /^--allup$/ ){ $MTBL = shift ; next ; }
    if( /^-u.*/ || /^--update$/ ){ $UPD++ ; next ; }
    if( /^-x.*/ || /^--debug$/ ){ $xbug++ ; next ; }
    if( /^-X.*/ || /^--extension$/ ){ $EXT = shift ; next ; }
    if( /-h.*/ || /^--help$/ ){
	$HelpInfo = `grep -v '^#' $NOSQL_INSTALL/help/index.txt`;
	print "$HelpInfo" ;
	exit 1;
    }
    die "\n$0: unknown option: $_\n" ; 
}
if( $UPD ){
    &do_upd ;
}
else{
    die "\n$0: not enough info given.\n", "For help type \"$0 --help\".\n"
    unless @ARGV >= 2 ;
    $tbl = shift ;
    # ($base = $tbl) =~ s/\.rdb$// ;
    $base = $tbl;
    @COLS = @ARGV ;
    $dbx = "$base.x." . join( ".", @COLS ) ;
    &do_ndx ;
}
sub do_ndx {        # generate index file $dbx from $tbl on columns @COLS
    warn "gen ndx file $dbx ($tbl) on: @COLS\n" ;
    open( RR, $tbl ) || die "$0: can't open table: $tbl\n" ;
    @ndx = () ; @XFN = () ; @XFD = () ;
    $lln = 0 ;
    while( <RR> ){                  # read rdbtbl header
    #next if /^\s*#/ ;       # comment 
    chop ;
    if( ++$lln == 1 ){
	$_ =~ s/\001//g;	    # Remove SOH markers
        @CN = split( /\t/, $_ );    # col names
    #   next ; }
    #@CD = split( /\t/, $_ );    # col definitions/dashline.
    last ; } }
    $lnpos = tell ;         # read point of first row
    for $col (@COLS){       # get, chk column name ndx
    for( $k=-1, $i=0 ; $i < @CN ; $i++ ){
        if( $col eq $CN[$i] ){
        $k = $i ;
        push( @ndx, $i ) ;      # index in $tbl
        push( @XFN, $col ) ;    # new index file, col name
        #push( @XFD, $CD[$i] ) ; # new index file, col definition/dashline
        last ; } }
    die "$0: column name no match: $col\n" if $k == -1 ;
    }
    push( @XFN, "lpos" ) ;
    #push( @XFD, "6n Line pos in $tbl" ) ;
    return if $xbug ;
    $command = "| _index $lnpos @ndx ".
	       "| sorttable @XFN > $dbx$EXT";
    # warn "$0: starting \"$command\".\n";
    open( SS, $command ) || die "$0: can't open pipe\n" ;
    # print SS "# NoSQL Index file for: $tbl\n" ;
    #print SS join( "\t", @XFN ), "\n", join( "\t", @XFD ), "\n" ;
    print SS "\001", join( "\t\001", @XFN ), "\n" ;
    fflush ;			# Make sure the header is printed first.
    $rcnt = 0 ;
    warn "Reading $tbl ...\n" ;
    while( read RR, $buffer, 65536 ) {
	print SS $buffer; # send to _index
    }
    warn "Finishing sorting ...\n" ;
    close SS ;
}

# get index file names, tbl names, col names, call do_ndx for each.
sub do_upd {
    unless( @ARGV ){
    opendir( DD, "." ) || die "$0: can't open curr dir\n" ;
    @ARGV = grep( /\.x\./, readdir( DD )) ; }
    for $dbx (@ARGV){
    next if ($dbx =~ /\.lock$/);
    if ($EXT && $dbx =~ /$EXT$/) { next; }
    ($base = $dbx) =~ s/\.x\..*$// ;
    # $tbl = "$base.rdb" ;
    $tbl = "$base" ;
    next if $MTBL && $MTBL ne $tbl ;
    open( XF, $dbx ) || die "$0: can't open input: $dbx\n" ;
    while( <XF> ){
        #next if /^\s*#/ ;   # skip comments
        @COLS = split( /\t/, $_ ) ;
        pop( @COLS ) ;  # remove line pos col
        close XF ;
        last ; }
    # warn "$tbl, $dbx, @COLS\n" ; #<<<<<<<<<<<<
    &do_ndx ;
    }
}

# End of program.
