#!/bin/ash
# *********************************************************************
#  Written by and copyright Carlo Strozzi <carlos@linux.it>.
#
#  filter: runs standard utilities, such as grep(1), sed(1), etc.,
#  on a NoSQL table passed via STDIN.
#  Copyright (C) 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-03-10 Beta code
#  2001-04-20 Added inline help
#  2001-06-07 Added options '-i' and '-o'
#
#  $Id$
# *********************************************************************

EUSAGE='Usage: filter [options --] command [args]'

[ $# = 0 ] && { echo "$EUSAGE"; exit 1; }

while :
do
  case $1 in
    -N|--no-header)	no_hdr=1 ;;
    -i|--input)		shift; i_file="$1" ;;
    -o|--output)	shift; o_file="$1" ;;
    -h|--help)
       grep -v '^#' @NOSQLPATH@/nosql/help/filter.txt
       exit 1
    ;;
    --)	;;
    *)			break ;;
  esac
  shift
done

if [ $# = 0 ]
then
   echo "$EUSAGE"
   exit 1
fi

[ "$i_file" = "" ] || exec < "$i_file"
[ "$o_file" = "" ] || exec > "$o_file"

read headline
read dashline

if [ "$no_hdr" = "" ]
then
   echo "$headline"
   echo "$dashline"
fi

"$@"			# Run the requested command.
			# Note: double quotes are mandatory!!

exit $?

#
# End of program.
#
