#! /bin/bash
# Last edited on 2024-03-30 18:30:13 by stolfi

# Usage: $0 <outprefix> <checkfiles>

# Collects all ".checked-good", ".checked-bad", and ".checked-dunno" word lists 
#   that result from splitting the given <checkfiles>, producing the
#   consolidated files <outprefix>.good, <outprefix>.dunno, and <outprefix>.bad. 
# The <checkfiles> should be given in order of decreasing authority.
# Each file <f>.checked-good is compared against the union of all 
#   .checked-bad and .checked-dunno lists, up to the present ones;
#   any conflicts are saved in the files <f>.checked-good-bad
#   and <f>.checked-good-dunno.  Each file <f>.checked-bad is similarly 
#   compared against the .checked-good and checked-dunno lists.  
#   Processing stops after the first checkfile with conflicts.

if [[ $# -lt 2 ]]; then
  echo "** usage: $0 <outprefix> <checkfiles>" 1>&2
  exit 1
fi

outprefix="$1"; shift
checkfiles=( "$@" )

tmp="/tmp/$$"

( rm -i ${outprefix}.{bad,dunno,good} )

for ext in bad dunno good ; do 
  if [[  -e ${outprefix}.${ext} ]]; then
    echo "** file ${outprefix}.${ext} exists; aborted" 1>&2
    exit 1
  fi
done
touch ${outprefix}.{bad,dunno,good}

for f in ${checkfiles[@]} ; do
  echo "=== $f ===" 1>&2
  prefix=${f%.*}
  dicio_split_check_list ${f} ${prefix}
  
  # Compute unions up to current list:
  for ext in bad dunno good ; do
    cat ${prefix}.checked-${ext} ${outprefix}.${ext} | sort | uniq > ${tmp}.${ext}
    mv ${tmp}.${ext} ${outprefix}.${ext}
  done
  wc ${outprefix}.{bad,dunno,good} 1>&2
  
  # Check consistency of current lists with previous ones:
  anybugs=0
  for aext in bad good ; do
    for bext in bad dunno good ; do
      if [[  ".${aext}" != ".${bext}"  ]]; then 
        ofile="${prefix}.checked-${aext}-${bext}"
        bool 1.2 ${prefix}.checked-${aext} ${outprefix}.${bext} > ${ofile}
        if [[ ( -f ! ${ofile} ) && ( ! ( -s ${ofile} ) ) ]]; then
          rm -v ${ofile}
        elif [[  ".${bext}" != ".dunno" ]]; then
          anybugs=1
        fi
      fi
    done
  done
  
  # Stop if any inconsistencies were found:
  if [[ ${anybugs} -ne 0 ]]; then
    echo "** inconsistencies found:" 1>&2
    wc ${prefix}.checked-{bad,good}-{bad,dunno,good}* 1>&2
    exit 1
  fi
done

echo ' ' 1>&2
wc ${outprefix}.{bad,dunno,good} 1>&2
