#!/bin/sh

check_referenced()
{
  ( cd ${1}/yref; ls | sed -ne 's/\.xml$//p' ) \
  | while read term
  do
    find ${1} -type f -name '*.xml' \
    | xargs grep "\&yref\.${term};" > /dev/null \
    || echo "Not referenced: ${1}/yref/${term}.xml"
  done
}

check_points()
{
  sed -ne "s#^<!ENTITY yref\.\([a-z0-9.]*\) SYSTEM .*#\1#p" \
	  ${1}/yref.xml \
  | while read term
  do
    [ -r "${1}/yref/${term}.xml" ] \
    || echo "Entity ${term} from ${1}/yref.xml points to nowhere."
  done
}

find intro/yref platform/yref -type f ! -name '*.bak' ! -name '*~' \
| xargs perl -p -i.bak -e 's/\n//'

check_referenced intro
check_referenced platform
check_points intro
check_points platform
