#!/bin/bash
### pov: run purchase-order validator

### Copyright (c) 2004, 2005 World Wide Web Consortium, 
### (Massachusetts Institute of Technology, European Research 
### Consortium for Informatics and Mathematics, Keio University). 
### All Rights Reserved. This work is distributed under the 
### W3C(TM) Software License [1] 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.

### [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231


### 1 check argument count, usage
if [ $# -lt 1 -o $1 = '?' -o $1 = '-?' ] ; then

   echo "pov: purchase-order validator, a conforming implementation of XML Schema 1.0"
   echo "Usage: pov INPUT [OPTION ...]"
   echo "where "
   echo "INPUT   is the filename or URL of the purchase order to be validated"
   echo "OPTION  is one of the known options:"
   echo "    --language LANG     specifies what language messages should be in, one of:"
   echo "                        en, de, fr"
   echo "    --messages MSGLVL   specifies how verbose messages should be: MSGLVL is one of:"
   echo "                        verbose, terse, silent"
   echo "    --psvi PSVI_SET     specifies how much of the PSVI to write out:"
   echo "           red          no output:  [validity], [validation attempted], and"
   echo "                           [error code] on validation root only, conveyed"
   echo "                           by error messages and return code"
   echo "           yellow       [validity], [validation attempted], [error code], "
   echo "                           [notation system], and [notation public]"
   echo "                           wherever applicable"
   echo "           blue         yellow, plus information about [element declaration], "
   echo "                           [attribute declaration], [type definition], "
   echo "                           [member type definition], and [schema-normalized value]"
   echo "                           wherever applicable"
   echo "           indigo       yellow, plus [nil], [type definition name], "
   echo "                           [type definition namespace], [type definition type], "
   echo "                           [type definition anonymous], [member type definition name], "
   echo "                           [member type definition namespace], "
   echo "                           [member type definition type], "
   echo "                           [member type definition anonymous], [schema-normalized value], "
   echo "                           [schema default], [schema supplied], and"
   echo "                           [schema information], wherever applicable"
   echo "           violet       everything the processor has"
   echo "           none         same as 'red'"
   echo "           full         same as 'violet'"
   echo "    --output FILENAME   specifies a file to which output should be written."
   echo "                        Default: - (i.e. standard output stream)"
   echo "    --test              specifies that test-related predicates should be loaded."
   echo ""
   echo 'The return code is (1 * validity) + (4 * validation_attempted)), where'
   echo "valid = 0, notKnown = 1, invalid = 2, full = 0, partial = 1, none = 2"
   echo "So:  return code of 0 = fully validated, valid"
   echo "                    2 = fully validated, invalid"
   echo "                    4 = partly validated, valid"
   echo "                    5 = partly validated, validity notKnown"
   echo "                    6 = partly validated, invalid"
   echo "                    9 = not validated, validity notKnown"
   echo "N.B. return codes of 1, 8, and 10 denote combinations of the validity"
   echo "and validation attempted properties which will not occur."
   echo ""
   exit

fi


### 2 set verbosity option
trigger=0
fTest=0
for i do
   if [ "$i" = "--messages" ] ; then
      trigger=1
   elif [ $trigger -eq 1 ] ; then
      fVerbose="$i"
      break
   elif [ "$i" = "--test" ] ; then
      fTest=1
      break
   else
      trigger=0
   fi
done


### 3 invoke Prolog with the appropriate arguments
PROLOGDIR="/home/cmsmcq/2004/schema/dctg/Prolog"
if [ $fTest -eq 0 ] ; then
   PROG="$PROLOGDIR/load_2l.pl"
   GOAL="pov"
else
   PROG="$PROLOGDIR/test_2l.pl"
   GOAL="run_test"
fi

pl -q -f $PROG -g $GOAL -t 'halt(13)' -- $* ;   
RC=$?


### 4 say something, if we need to
### if --messages verbose, then say what the result was

if [ "$fVerbose" = "verbose" ] ; then 
   VALCODE=[$RC%4]
   ATTCODE=[$RC/4]
   case "$VALCODE" in
      0 ) VALSTR="valid";;
      1 ) VALSTR="notKnown";;
      2 ) VALSTR="invalid";;
      3 ) VALSTR="error, this value should be impossible";;
   esac
   case "$ATTCODE" in
      0 ) ATTSTR="full";;
      1 ) ATTSTR="partial";;
      2 ) ATTSTR="none";;
      3 ) ATTSTR="error, this value should be impossible";;
   esac

   echo "pov: Return code $RC: [validation = $VALSTR],[validation attempted = $ATTSTR]" >&2
fi


### 5 exit
exit $RC

