#!/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-2001 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.
#
#  2001-01-03 Ported to NoSQL v3
#  2001-01-13 Minor bug stating table names with option '-u'
#  2001-02-25 Changed naming convention on index files
#  2001-03-06 Now '-u' correctly skips lock files.
#  2001-04-18 Added inline help.
#
#  $Id$
# *********************************************************************

$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( /-h.*/ || /^--help$/ ){
	$HelpInfo = `grep -v '^#' @NOSQLPATH@/nosql/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 ){
        @CN = split( /\t/, $_ );    # col names
        next ; }
    @CD = split( /\t/, $_ );    # col definitions
    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
        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";
    # 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" ;
    $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$/ ;
    ($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.
