#!/bin/sh
#-----------------------------------------------------------------------------
# affirm: Determine the Boolean value of a set of arguments
#-----------------------------------------------------------------------------
#
#   Copyright (C) 1993,1994
#   Associated Universities, Inc. Washington DC, USA.
#
#   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.
#
#   Correspondence concerning AIPS++ should be addressed as follows:
#          Internet email: aips2-request@nrao.edu.
#          Postal address: AIPS++ Project Office
#                          National Radio Astronomy Observatory
#                          520 Edgemont Road
#                          Charlottesville, VA 22903-2475 USA
#
#-----------------------------------------------------------------------------
# Usage: affirm [-a | -o ] [-s] [string1 [string2...]]
#-----------------------------------------------------------------------------
# affirm tests all of its arguments for truth or falsehood.  The arguments
# can be of the form "true"/"false", "t"/"f", "yes"/"no", "y"/"n", "on"/"off",
# "1"/"0" and are case insensitive.  It will print either "true" or "false"
# on stdout with a status return to match.
#
# Options:
#   -a     Produce the logical AND of all arguments (default).
#   -o     Produce the logical  OR of all arguments.
#   -s     Silent, just produce the exit status.
#
# Status return values correspond to the various phases of the operation
#    0:  true
#    1:  false
#    2:  unrecognized argument
#
# Notes:
#    1) The "-a" option returns immediately if it encounters a false argument;
#       remaining arguments are not checked for validity.  It can best be
#       thought of as searching for a "false" argument.  Even if the argument
#       list contains an unrecognized value the search still continues for a
#       "false" value.  If none are found and there were no unrecognized
#       arguments then it returns "true".
#
#       The "-o" option behaves similarly except that it searches for a "true"
#       argument.  Hence, to test the logical value of an environment variable
#       which may be undefined use
#
#          affirm -a $VAR
#
#       (or just 'affirm $VAR') to get a true result if VAR is blank, and use
#
#          affirm -o $VAR
#
#       to get a false result if VAR is blank.
#
#    2) The "-a" and "-o" options may be used together, for example:
#
#          affirm -o $A1 $A2 -a $A3 $A4
#
#       returns true if either of A1 or A2 is true, or if A3 and A4 are both
#       true or both blank.
#
# Original: 1993/09/01 by Mark Calabretta, ATNF
# $Id$
#=============================================================================
  MODE=and
  BADARG=false
  SILENT=""

  while [ "$#" -gt 0 ]
  do
     case $1 in
        -a)
           MODE=and
           ;;

        -o)
           MODE=or
           ;;
        -s)
           SILENT=1
           ;;

         *)
           ARG=`echo $1 | tr [A-Z] [a-z]`

           case $ARG in
           true | t | yes | y | on | 1)
              if [ "$MODE" = or ]
              then
                 [ "$SILENT" = "" ] && echo true
                 exit 0
              fi
              ;;

           false | f | no | n | off | 0)
              if [ "$MODE" = and ]
              then
                 [ "$SILENT" = "" ] && echo false
                 exit 1
              fi
              ;;

           *)
              BADARG=true
              ;;
           esac

           ;;
     esac

     [ "$2" = "" ] && break

     shift
  done


  if [ "$BADARG" = true ]
  then
     [ "$SILENT" = "" ] && echo "affirm: Unrecognized argument." 1>&2
     exit 2
  elif [ "$MODE" = or ]
  then
     [ "$SILENT" = "" ] && echo false
     exit 1
  else
     [ "$SILENT" = "" ] && echo true
     exit 0
  fi
