#!/bin/bash
LOG_LEVEL=${LOG_LEVEL:-DEBUG}

set -e  # Abort on error
if [ "$LOG_LEVEL" = "DEBUG" -o "$LOG_LEVEL" = "TRACE" ]; then
  set -x
fi


remove_filegrp() {
  # Remove the given file group from the workspace

  filegrp_use=$1
  mets=$2

  xmlstarlet ed --inplace \
    -N mets=http://www.loc.gov/METS/ \
    -d "//mets:fileGrp[@USE='$filegrp_use']" $mets

  # XXX See also https://github.com/OCR-D/core/issues/245
  # XXX This should also delete the files (after checking if they are indeed inside the workspace) and the directory
}

do_validate() {
  ocrd workspace validate --skip pixel_density --page-strictness lax --page-coordinate-consistency off mets.xml
  # XXX ocrd-tesserocr INCONSISTENCY in TextRegion → use "--page-strictness lax" for now
  # XXX INVALIDITY in Glyph ID etc. in GT → --page-coordinate-consistency off

  # XXX
  if test -d TEMP; then
    echo "TEMP exists!"
    rm -rf TEMP
  fi
}

do_binarization() {
  # Binarize the images

  ocrd_olena_binarize_paramters='{"impl": "sauvola-ms-split"}'
  remove_filegrp OCR-D-IMG-BINPAGE mets.xml
  remove_filegrp OCR-D-IMG-BIN mets.xml
  ocrd-olena-binarize -l $LOG_LEVEL \
    -m mets.xml -I OCR-D-IMG -O OCR-D-IMG-BINPAGE \
    -p "$ocrd_olena_binarize_paramters"
}

do_linesegmentation_tesserocr() {
  # Segment the lines in the binarized images

  remove_filegrp OCR-D-SEG-REGION mets.xml
  remove_filegrp OCR-D-SEG-LINE mets.xml
  #ocrd-ocropy-segment -l $LOG_LEVEL \
  #  -m mets.xml -I OCR-D-IMG-BINPAGE -O OCR-D-SEG-LINE
  # XXX ocrd-ocropy-segment throws an exception for buerger_gedichte_1778.ocrd

  ocrd-tesserocr-segment-region -l $LOG_LEVEL \
    -m mets.xml -I OCR-D-IMG-BINPAGE -O OCR-D-SEG-REGION
  ocrd-tesserocr-segment-line -l $LOG_LEVEL \
    -m mets.xml -I OCR-D-SEG-REGION -O OCR-D-SEG-LINE

  # XXX compare ocrd-tesserocr-segment* vs tesseract native
}

do_linesegmentation_sbb() {
  # Segment the lines in the images
  # TODO: Check that this works with the RGB images

  ocrd_sbb_textline_detector_parameters='{"model": "/var/lib/textline_detection"}'
  remove_filegrp OCR-D-SEG-REGION mets.xml
  remove_filegrp OCR-D-SEG-LINE mets.xml
  ocrd-sbb-textline-detector -l $LOG_LEVEL \
    -m mets.xml -I OCR-D-IMG-BINPAGE -O OCR-D-SEG-LINE \
    -p "$ocrd_sbb_textline_detector_parameters"
}

do_ocr() {
  # Perform OCR on the segmented lines

  ocrd_tesserocr_recognize_parameters='{ "model": "GT4HistOCR_2000000" }'  # TODO mods:language + fontident → model
  remove_filegrp OCR-D-OCR-TESS mets.xml
  ocrd-tesserocr-recognize -l $LOG_LEVEL \
    -m mets.xml -I OCR-D-SEG-LINE -O OCR-D-OCR-TESS \
    -p "$ocrd_tesserocr_recognize_parameters"
}

do_ocr_calamari() {
  ocrd_calamari_recognize_parameters='{
    "checkpoint": "/var/lib/calamari-models/GT4HistOCR/2019-07-22T15:49+0200/*.ckpt.json",
    "textequiv_level": "line"
  }'
  remove_filegrp OCR-D-OCR-CALAMARI mets.xml
  ocrd-calamari-recognize -l $LOG_LEVEL \
    -m mets.xml -I OCR-D-SEG-LINE -O OCR-D-OCR-CALAMARI \
    -p "$ocrd_calamari_recognize_parameters"
}

page_validate_xml() {
  # Validate all PAGE XML against the XML schema

  filegrp=$1

  local file
  for file in `ocrd workspace find -G $filegrp`; do
    XSD_DIR=`dirname $0`/xsd
    if [ ! -d "$XSD_DIR" ]; then
      XSD_DIR=/usr/share/xml
    fi
    xmllint --noout --schema $XSD_DIR/pagecontent.2019-07-15.xsd $file
  done
}

page_downgrade_to_2018() {
  # Not used anymore, but kept if needed in the future
  filegrp=$1

  local file
  for file in `ocrd workspace find -G $filegrp`; do
    sed -i 's#pagecontent/[0-9-]*#pagecontent/2018-07-15#g' $file
  done
}

page_upgrade_to_2019() {
  filegrp=$1

  local file
  for file in `ocrd workspace find -G $filegrp`; do
    sed -i 's#pagecontent/[0-9-]*#pagecontent/2019-07-15#g' $file
  done
}

pip3 list


do_binarization
do_validate


do_linesegmentation_sbb
page_upgrade_to_2019             OCR-D-SEG-LINE
page_validate_xml                OCR-D-SEG-REGION
page_validate_xml                OCR-D-SEG-LINE
do_validate


do_ocr_calamari


do_ocr


for ocr_filegrp in OCR-D-OCR-CALAMARI OCR-D-OCR-TESS; do

  page_validate_xml           $ocr_filegrp
  do_validate

  page_validate_xml           $ocr_filegrp
  do_validate

  # XXX This seems to be causing new problems with validation
  # https://github.com/OCR-D/core/issues/176
  #page_fix_image_references   $ocr_filegrp
  #do_validate

  if ocrd workspace list-group | grep -q OCR-D-GT-PAGE; then
    remove_filegrp $ocr_filegrp-EVAL mets.xml
    ocrd-dinglehopper -m mets.xml -I OCR-D-GT-PAGE,$ocr_filegrp -O $ocr_filegrp-EVAL
  fi

done

# vim:tw=120: