#!/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.
#
#  depend: test for ``functional dependency'' betwen two columns.
#
#  Copyright (c) 2001,2002,2003 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; 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.
#
# *********************************************************************
#  $Id: depend,v 1.3 2003/05/23 20:31:53 carlo Exp $

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

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

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

: ${TMPDIR:=/tmp}

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

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

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

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

# End of program.
