#!/bin/bash

TEI_ROOT=$1

function wypluwkaLukasz {
    d=$1
    x=`grep -l '<f name="interpretation">' $d/ann_morphosyntax.xml`
    TMP=`mktemp`
    if [[ "$x" == "" ]]
    then
        ./modify-tei-segmentation.pl $d/ann_segmentation.xml $TMP && mv $TMP $d/ann_segmentation.xml &&
        ./modify-tei-morphosyntax.pl $d/ann_morphosyntax.xml $TMP && mv $TMP $d/ann_morphosyntax.xml &&
        if [ -e $d/ann_senses.xml ]; then ./modify-tei-senses.pl $d/ann_senses.xml $TMP && mv $TMP $d/ann_senses.xml; fi
        if [ $? -ne 0 ]; then exit 1; fi
    fi
}

for f in `find $TEI_ROOT -name 'ann_morphosyntax.xml'`
do
    wypluwkaLukasz `dirname $f`
done
