#!/bin/bash

## quick and dirty check of report configs, not intergated into ernad

# echo $IMPNA
if [ -z $IMPNA ]; then
    if [ -z $1 ]; then
        echo "I need an impna"
        exit
    fi
    cand_impna=$1
    if [ ! -d $HOME/ernad/style/$cand_impna ]; then
        echo "I do not see your impna $cand_impna"
        exit
    fi
    IMPNA=$cand_impna
fi

if [ $2 ]; then
    loop_mode=1
fi

lint_fufi=/tmp/lint.txt

## report configurations
rng_fufi=$HOME/ernad/schema/$IMPNA/report.rng.xml
if [ ! -f $rng_fufi ]; then
    echo "I don't see $rng_fufi"
    exit
fi
reports_dir=$HOME/ernad/etc/$IMPNA/reports/available


for fufi in `/usr/bin/ls $reports_dir/*.amf.xml | /usr/bin/shuf`; do
    /usr/bin/xmllint --relaxng $rng_fufi $fufi > /dev/null 2> $lint_fufi
    error=$?
    if [ $error -eq 0 ]; then
        # echo "$fufi done"
        continue
    fi
    emacs $lint_fufi $fufi
    rm $lint_fufi
    exit
done

#
## editor
rng_fufi=$HOME/ernad/schema/$IMPNA/editor.rng.xml
## needs the be generated with "schemas IMPNA"
# rng_fufi=$HOME/ernad/schema/editor.rng.xml
if [ ! -f $rng_fufi ]; then
    echo "I don't see $rng_fufi"
    exit
fi
## needs the be generated with "schemas IMPNA"
editors_dir=$HOME/ernad/etc/$IMPNA/editors
for fufi in "$editors_dir/"*.amf.xml; do
    /usr/bin/xmllint --relaxng $rng_fufi $fufi > /dev/null 2> $lint_fufi
    error=$?
    if [ $error -eq 0 ]; then
        # echo "$fufi done"
        # cat $lint_fufi
        continue
    fi
    emacs $lint_fufi $fufi
    rm $lint_fufi
    #if [ $loop_mode == 1 ]; then
    #    continue
    #fi
    exit
done
exit
