#!/bin/ash
# *********************************************************************
# frametable: visual aid to display table contents.
#
# Copyright (c) 2006,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

awk_prog='

# Remove leading blanks/tabs from column names.
NR==1 { gsub(/[ \t]*/,"") }

{
  # Remove leading blanks/tabs from rightmost column.
  gsub(/[ \t]*$/,"")

  nf = split($0,a,/[ \t]*\|/)

  # strip leading blanks/tabs from numeric fields.
  for (i=1; i<=nf; i++) {
      if (a[i] ~ /^[ \t]+[-+]?[0-9]+(\.[0-9]+)?$/) sub(/^[ \t]+/,"",a[i])
      printf("%s",a[i])

      # Restore NoSQL standard separators.
      if (i<nf) printf("\t")
      else printf("\n")
  }
}'

reframe=
unframe=
paging=1
edit=
quiet=
hush=

while [ $# -gt 0 ]
do
   case $1 in
	-r|--reframe)	reframe=1 ;;
	-u|--unframe)	unframe=1 ;;
	-q|--quiet)	quiet=1 ;;
	-n|--no-paging)	paging= ;;
	-e|--edit)	edit='--verbose --edit'; quiet=1; hush=1 ;;
	-H|--hush)	hush=1; quiet=1 ;;
	-h|--help)
	     grep -v '^#' $NOSQL_INSTALL/help/frametable.txt
	     exit 0
	;;
	--show-copying)
	     cat $NOSQL_INSTALL/doc/COPYING
	     exit 1
	;;
	--show-warranty)
	     cat $NOSQL_INSTALL/doc/WARRANTY
	     exit 1
	;;
	*) break ;;	
   esac
   shift
done

TMP1=`mktemp $TMPDIR/frametable.XXXXXX` || exit $?
TMP2=`mktemp $TMPDIR/frametable.XXXXXX` || exit $?

# Set-up trapping on signals.
trap "rm -f $TMP1 $TMP2" 0 
trap "exit 2" 1 2 3 15

cat > $TMP1

if [ -n "$unframe" ]
then
   _awk -- "$awk_prog" $TMP1 | rdbtotable > $TMP2
   istable $edit < $TMP2
   if [ $? -ne 0 ]
   then
      if [ -z "$quiet" ]
      then
	 echo 'frametable: input is not a proper framed table (wait ...)' >&2
	 sleep 2
      fi
      [ -n "$hush" ] || cat $TMP1		# print input unchaged.
      exit 1
   fi
   [ -n "$hush" ] || cat $TMP2
   exit
fi

if [ -n "$reframe" ]
then
   frametable --unframe < $TMP1 > $TMP2 || exit $?
   cp $TMP2 $TMP1
else
   istable $edit < $TMP1
   if [ $? -ne 0 ]
   then
      if [ -z "$quiet" ]
      then
	 echo 'frametable: input is not a valid table (wait ...)' >&2
	 sleep 2
      fi
      [ -n "$hush" ] || cat $TMP1		# print input unchaged.
      exit 1
   fi
fi

[ -n "$hush" ] && exit

# compute max. justified line length.
set - $(widest -H -R -s \| -a 2 -i $TMP1)

if [ $1 -lt 60 ]
then
   set - ""
else
   set - -l$1
fi

if [ -n "$paging" ]
then
   prtable -b0 $1 -s\| < $TMP1
else
   paging=$(wc -l < $TMP1)
   paging=$(expr $paging + 2)		    # add column names and dashline.
   prtable -b0 $1 -p$paging -s\| < $TMP1
fi

# End of program.
