#!/bin/ash
# *********************************************************************
# Note: part of this code has been freely adapted from examples
# provided in the book "Unix Relational Database Management", so
# credits are due to the original authors.
#
# deptable: test for ``functional dependency'' betwen two columns.
#
# Copyright (c) 2001,2006 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.
#
# *********************************************************************
#  $Id: deptable,v 1.3 2006/03/10 11:26:13 carlo Exp $

# Get local settings and apply defaults.
: ${NOSQL_INSTALL:=/usr/local/nosql}

if [ "$1" = -h ] || [ "$1" = --help ]
then
   grep -v '^#' $NOSQL_INSTALL/help/deptable.txt
   exit 1
fi

if [ "$1" = --show-copying ]
then
   cat $NOSQL_INSTALL/doc/COPYING
   exit 1
fi

if [ "$1" = --show-warranty ]
then
   cat $NOSQL_INSTALL/doc/WARRANTY
   exit 1
fi

if [ $# != 2 ]
then
   echo Usage: deptable column_1 column_2 >&2
   exit 1
fi

: ${TMPDIR:=/tmp}

f_tmp=`mktemp $TMPDIR/nosql.XXXXXX` || exit $?

trap "rm -f $f_tmp" 0 
trap "exit 2" 1 2 3 15

getcolumn "$@" | sorttable | uniq > $f_tmp || exit $?

if [ "`wc -l < $f_tmp`" = "`getcolumn -i $f_tmp $1 | uniq | wc -l`" ]
then
   echo true
   exit 0
else
   echo false
   exit 1
fi

# End of program.
