#!/bin/tcsh

# General purpose tool to create an Iso TEI file from a basic BPF file

# annotConv version >= 1.15

# this script requires a saxon9 installation commandline XSLT tool in SAXON9

set SAXON9 = saxonb-xslt

set SCRIPT = `readlink -f "$0"`
set SOURCE = `dirname "$SCRIPT"`  # location where the script is stored 
                           # (even if we start via a symbolic link)
setenv LANG en_US.UTF-8  # defines the behavior of text processing, sorting etc.

set SCHEMA = ""

set v = 0
set validate = FALSE
set EXB2ISOXSLT = $SOURCE/exmaralda2isotei.xsl
set INP = ""
set OUT = ""

set DATE = `date '+%s'`
set TEMP = /tmp/$$_$DATE

# Actually do the argument parsing here

while ( "$1" != "" )
        switch ("$1")
        case *=*:
                set key = `echo $1 | cut -d= -f1`
                #check if option is known (set)
                eval set checkoption = '$?'$key
                if ( $checkoption == 0 ) then
                  echo "ERROR: unknown option $key - exiting" > /dev/stderr
                  exit 1
                endif
                set val = `echo $1 | cut -d= -f2`
                eval "set $key "= \'"$val"\'
                unset key val
                shift
                breaksw
        default:
                break
        endsw
end

# end option parser

# boolean variable check; define all boolean input parameters here

set bool = ( validate )
foreach booleanvariable ( $bool )
  eval set val = '$'$booleanvariable
  switch ( $val )
  case true:
    eval set $booleanvariable = TRUE
    breaksw
  case True:
    eval set $booleanvariable = TRUE
    breaksw
  case TRUE:
    eval set $booleanvariable = TRUE
    breaksw
  case 1:
    eval set $booleanvariable = TRUE
    breaksw
  case yes:
    eval set $booleanvariable = TRUE
    breaksw
  case Yes:
    eval set $booleanvariable = TRUE
    breaksw
  case YES:
    eval set $booleanvariable = TRUE
    breaksw
  case false:
    eval set $booleanvariable = FALSE
    breaksw
  case False:
    eval set $booleanvariable = FALSE
    breaksw
  case FALSE:
    eval set $booleanvariable = FALSE
    breaksw
  case 0:
    eval set $booleanvariable = FALSE
    breaksw
  case no:
    eval set $booleanvariable = FALSE
    breaksw
  case No:
    eval set $booleanvariable = FALSE
    breaksw
  case NO:
    eval set $booleanvariable = FALSE
    breaksw
  default:
    echo "Boolean $booleanvariable=$val is not a boolean value. Use either '0,1,true,false,yes,no'"
    exit 1
  endsw
end

# preliminary checks
if ( $INP == "" || $OUT == "" ) then 
  echo "usage: $0 [SCHEMA=IsoTeiSchema.xsd][validate=TRUE] INP=input.par OUT=output.xml" > /dev/stderr
  echo "       transforms MAUS generated BPFs *.par into Iso TEI *.xml" > /dev/stderr
  echo "       if validate=TRUE, the resulting *.xml are validated against XSD in SCHEMA" > /dev/stderr
  exit 1
endif
if ( ! -e "$INP" ) then
  echo "ERROR: ${0:t} : cannot find input $INP - exiting"
  exit 1
endif
touch "$OUT"
if ( $status != 0 ) then
  echo "ERROR: ${0:t} : cannot write to output $OUT - exiting"
  exit 1
endif
which $SAXON9 >& /dev/null
if ( $status != 0 ) then
  echo "ERROR: ${0:t} : XSLT converter $SAXON9 not found - exiting"
  exit 1
endif


# first transform the input BPF into Exmaralda
touch ${TEMP}_mausbpf2exb.xml
if ( $v > 0 ) echo "DEBUG: ${0:t} $SOURCE/../mausbpf2exb/mausbpf2exb $INP to ${TEMP}_mausbpf2exb.xml"
$SOURCE/../mausbpf2exb/mausbpf2exb "$INP" >! ${TEMP}_mausbpf2exb.xml
set err_code = $status
if ( $err_code != 0 ) then
  echo "ERROR: ${0:t} : helper mausbpf2exb.py reports error $err_code - exiting"
  exit $err_code
endif

# then transform the Exmaralda to Iso TEI
if ( $v > 0 ) echo "DEBUG: ${0:t} $SAXON9 ${TEMP}_mausbpf2exb.xml $EXB2ISOXSLT"
# this is to suppress the warning message of JDK
unsetenv JDK_JAVA_OPTIONS 
"$SAXON9" ${TEMP}_mausbpf2exb.xml "$EXB2ISOXSLT" >! "$OUT"
set err_code = $status
if ( $err_code != 0 ) then
  echo "ERROR: ${0:t} : XSLT transform of EXB to ISO failed with error $err_code - exiting"
  exit $err_code
endif

# validate
# TO DO


rm -rf ${TEMP}*
exit 0


