#!/bin/csh -f
#
#   chkdoc
#
#   compare the routines contained in a binary with the documentation
#   for it.  prints a list to stdout of all routines that are in one
#   or the other, but not both.  assumes a number of naming conventions,
#   hence is only useful for BWE system packages.
#   SYNTAX:
#          chkdoc doc binary [home]
#
#   default home is "/pro/bwe"

if ( $1 == DOGREP ) goto dogrep    # alternate entry point

onintr cleanup

set cmd = $0
set cmd = $cmd:t
if ( $#argv < 2 ) then
   echo usage: $cmd 'doc_file binary_file [sys_home]'
   exit 1
endif

if ( $#argv == 3 ) then
   set sys = $3
else
   set sys = /pro/bwe
endif

set doc = $1
if ( ! -e $doc ) then             # try to guess pathnames if not fully given
   set doc = $sys/doc/$doc
   if ( ! -e $doc) then
      echo $cmd':' cannot find $1
      exit 1
   endif
endif
set bin = $2
if ( ! -e $bin ) then
   set bin = $sys/$bin:r/lib/$bin
   if ( ! -e $bin) then
      echo $cmd':' cannot find $2
      exit 1
   endif
endif

set binout = /tmp/$cmd$$BIN     # like "/tmp/chkdoc734BIN" 
set docout = /tmp/$cmd$$DOC

#
#   create a list of routines contained in the binary, and a list of
#   routines mentioned in the doc.  For the binary, the names must
#   be flagged with a T by nm(1), and abide by BWE naming conventions.
#   The documentation should use the BWEMAC troff macros, which make
#   locating routine names trivial.
#
#   machine dependencies: apollo symbols are not prefixed by an underscore
#   so processing is abit easier.  for other machines, we must strip the
#   leading underscore from the binary list.
#
if ( $?SYSTYPE ) then    # are we on an apollo?
  nm $bin | awk '/^[0-9a-fA-F]+ T [A-Z]+[a-z0-9]/ { print $3 }' > $binout
  awk '/^\.R[ 	][A-Z]+[a-z0-9]/ { print $2 }' $doc > $docout
else
  nm $bin | awk '/^[0-9a-fA-F]+ T _[A-Z]+[a-z0-9]/ { print $3 }' |\
            sed -e 's/^_//' > $binout
  awk '/^\.R[ 	][A-Z]+[a-z0-9]/ { print $2 }' $doc > $docout
endif

#
#   use the alternate entry to this routine to run the code below
#   to search for routines that do not appear in both lists.
#
$0 DOGREP $docout BIN < $binout
$0 DOGREP $binout DOC < $docout

cleanup:
rm -f $binout $docout
exit 0

#
#   dogrep -- this is conceptually a seperate command.  a given file
#   is repeatedly grep'd for each pattern appearing on stdin.  If the
#   pattern DOES NOT appear, it is printed.
#
dogrep:

while ( 1 )
   set rtn = $<
   if ( x$rtn == x ) exit 0
   set cnt = `grep -c $rtn $2`
   if ( $cnt == 0 ) echo $3 only: $rtn
end
