#!/bin/sh

TMPDIR="${TMPDIR:-/tmp}"

(cd $1/stdlib; ls -1 *.mli) | sed -e 's/\.mli//' >$TMPDIR/stdlib-$$-files
cut -c 1 $TMPDIR/stdlib-$$-files | tr a-z A-Z >$TMPDIR/stdlib-$$-initials
cut -c 2- $TMPDIR/stdlib-$$-files \
| paste -d '\0' $TMPDIR/stdlib-$$-initials - >$TMPDIR/stdlib-$$-modules

exitcode=0
for i in `cat $TMPDIR/stdlib-$$-modules`; do
  case $i in
    Stdlib | Camlinternal* | *Labels | Obj | In_channel | Out_channel) continue;;
    Std_exit*) continue;;
  esac
  grep -q -e '"'$i'" & p\.~\\stdpageref{'$i'} &' $1/manual/src/library/stdlib-blurb.etex || {
    echo "Module $i is missing from the module description in library/stdlib-blurb.etex." >&2
    exitcode=2
  } &&
  grep -q -e '\\stddocitem{'$i'}' $1/manual/src/library/stdlib-blurb.etex || {
    echo "Module $i is missing from the linklist in library/stdlib-blurb.etex." >&2
    exitcode=2
  }
done

rm -f $TMPDIR/stdlib-$$-*

if [ $exitcode -eq 0 ]; then
  echo "All Standard Library modules are referenced"
fi

exit $exitcode
