#!/bin/bash

unset DISPLAY;
unset LUI;

if [ -z "${LOGONROOT}" ]; then
  #
  # determine the LOGON root directory, assuming this script resides in the
  # top-level directory of the tree.
  #
  path=$(dirname $0)
  if [ "${path#./}" != "${path}" ]; then
    path="$(pwd)/${path#./}"
  fi
  if [ "${path#/}" = "${path}" ]; then
    if [ "${path}" = "." ]; then
      path="$(pwd)";
    else 
      path="$(pwd)/${path}"
    fi
  fi
  LOGONROOT="${path}";
  export LOGONROOT
fi

#
# include a shared set of shell functions and global parameters, including the
# architecture identifier .LOGONOS.
#
. ${LOGONROOT}/etc/library.bash;

date=$(date "+%y-%m-%d");

source="--source";

while [ $# -gt 0 -a "${1#-}" != "$1" ]; do
  case ${1} in
    --binary)
      source="";
      shift 1;
    ;;
    --source)
      source="--source";
      shift 1;
    ;;
    --cat)
      cat="--cat";
      shift 1;
    ;;
    --compare)
      action=compare;
      shift 1;
    ;;
    --filter)
      action=filter;
      shift 1;
    ;;
    *)
      echo "analyze: invalid option \`${1}'; exit.";
      exit 1;
    ;;
  esac
done

if [ -z "${LOGONLOG}" ]; then
  LOGONLOG=${LOGONROOT}/log;
fi
LOG=${LOGONLOG}/analyze.${action}.${date}.log;

case "${action}" in
  compare)
  {
    for file in $*; do
      echo [${file}]; echo;
      awk \
        '/^(\[[0-9:]+\] )?compare-in-detail\(\):$/ \
           { output=1; } \
         /^(\[[0-9:]+\] )?compare-in-detail\(\): [0-9]+ differences?.$/ \
           { print; output=0; } \
         { if(output) print; }' ${file};
      echo; echo;
    done
  } 2>&1 | tee ${LOG}
  ;;
  filter)
  {
    for file in $*; do
      echo [${file}]; echo;
      awk \
        '/^\[[0-9:]+\] result-filter\(\): `.+ on <.+>:$/ \
	  { output=1; } \
         /^\[[0-9:]+\] result-filter\(\): [0-9]+ item.+$/ \
	  { print; output=0; } \
         { if(output) print; }' ${file};
      echo; echo;
    done
  } 2>&1 | tee ${LOG}
  ;;
esac