#!/bin/ash
# *********************************************************************
# userproc: apply custom processing to a NoSQL table.
#
# Copyright (c) 2006,2007,2009 Carlo Strozzi
#
# 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; version 2 dated June, 1991.
#
# 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.
# *********************************************************************

# Get local settings and apply defaults.
: ${TMPDIR:=/tmp} ${NOSQL_CHARSET:=utf-8}
: ${NOSQL_INSTALL:=@NOSQL_INSTALL@}
: ${SWU_INSTALL:=@SWU_INSTALL@}
: ${PATH:=@LOCAL_PATH@}

export NOSQL_INSTALL SWU_INSTALL PATH TMPDIR NOSQL_CHARSET

userproc=

while :
do
  case $1 in
    -i|--input)		shift; i_file="$1" ;;
    -o|--output)	shift; o_file="$1" ;;
    -f|--script)	shift; userproc="$userproc -f$1" ;;
    -h|--help)
       grep -v '^#' $NOSQL_INSTALL/help/userproc.txt
       exit 1
    ;;
    --show-copying)
       cat $NOSQL_INSTALL/doc/COPYING
       exit 1
    ;;
    --show-warranty)
       cat $NOSQL_INSTALL/doc/WARRANTY
       exit 1
    ;;
    -*) ;;				# ignore unknown options.
    *)
       break
    ;;
  esac
  shift
done

# Set default userproc.
[ "X$userproc" = X ] && userproc=-f$NOSQL_INSTALL/lib/userproc.awk

[ "X$i_file" = X ] || exec < "$i_file"

[ "X$o_file" = X ] || exec > "$o_file"

_awk -f $SWU_INSTALL/lib/BEGIN.awk -f $NOSQL_INSTALL/lib/BEGIN.awk \
     -f $SWU_INSTALL/lib/swulib.awk -f $NOSQL_INSTALL/lib/nosqllib.awk \
     -f $NOSQL_INSTALL/lib/mainloop.awk $userproc \
     -f $NOSQL_INSTALL/lib/END.awk --

#EOF
