#! /bin/bash
# Last edited on 2017-05-14 03:32:12 by stolfilocal

# Usage: "$0 {adir} {bdir}"

# Checks whether the dates and times of the files in the "{adir}" subdir
# contain in the dates and times of the files in the "{bdir}" and
# "{bdir}/JUNK" subdirs, for each combination of {ex}, {cr} and {tu}.

adir="$1"; shift;
bdir="$1"; shift;

if [[ ! -d ${adir} ]]; then  echo "** no directory ${adir}" 1>&2 ; exit 1 ; fi
if [[ ! -d ${bdir} ]]; then  echo "** no directory ${bdir}" 1>&2 ; exit 1 ; fi

tmp="/tmp/$$"
rtags="${tmp}-r.txt" # Unsorted {ex}-{cr}-{tu} tags.
stags="${tmp}-s.txt" # Sorted tags.
ajoin="${tmp}-a.txt" # Concatenation of all {adir} files.

# Collect all "{ex}-{cr}-{tu}" tags in file "${stags}":
rm -f ${rtags} ${stags}
for dir in ${adir} ${bdir} ${bdir}/JUNK ; do 
  ( shopt -s nullglob; cd ${dir} && echo 20[0-9][0-9]-*.txt | tr ' ' '\012' ) >> ${rtags}  
done
cat ${rtags} \
  | sed -e 's:^20[-0-9]*::g' -e 's:[.]txt::g' \
  | sort | uniq \
> ${stags}

# Test the coverage for each tag:
for tg in `cat ${stags}` ; do 
  echo "=== ${tg} ===" 
  # Collect and merge all files in {adir} with this tag:
  afiles=( `shopt -s nullglob; echo ${adir}/20*-${tg}.txt` )
  rm -f ${ajoin}; touch ${ajoin}
  if [[ ${#afiles[@]} -gt 0 ]]; then 
    for af in "${afiles[@]}" ; do 
      echo "  ${af}"
      cat ${af} >> ${ajoin}
    done
  else
    echo "  no files in ${adir}" 
  fi   
  printf "  total %s lines\n" "`cat ${ajoin} | wc -l`"
 
  # Now list all files in ${bdir}:
  bfiles=( `shopt -s nullglob; echo ${bdir}{,/JUNK}/20*-${tg}.txt` )
  if [[ ${#bfiles[@]} -gt 0 ]]; then 
    ../check_redundant_dates.sh "${ajoin}" "${bfiles[@]}" 
  else
    echo "  no files in ${bdir}" 
  fi
done

rm -f ${tmp}-*

    
