#!/bin/bash
#  $HeadURL: http://dione.no-ip.org/svn/ade/tags/1.7.6/lib/tests/bin/ADESTDTEST200_docs_manpages_exist.sh $ $LastChangedRevision: 3840 $
set -e
PROGNAME=`basename $0`
SANDPIT=`pwd`
. $(ade-config ade_include_prefix)/adetestsupport.sh

echo "going to top ..."
cd $ADETEST_MODROOT
#echo MODROOT=$ADETEST_MODROOT
#echo SANDPIT=$SANDPIT

echo "copying sources to temporary location in order to compile them for the sandpit ..."
cp -a $ADETEST_MODROOT $SANDPIT/sources

echo "installing into sandpit ..."
OLDPWD=$(pwd)
cd $SANDPIT/sources
#  Fix <app>-config.sh (but not ade-app-config.sh)
APPCONFIGFILE=$(ls $SANDPIT/sources/bin/*-config.sh | fgrep -v ade-app-config.sh)
UCAPP=$(echo $APPCONFIGFILE | sed -e 's/.*\/\([^-]*\)-.*/\1/' -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/')
#echo "UCAPP=$UCAPP"
echo "modifying <app>-config file to install into sandpit ..."
perl -pi -e "s@^(${UCAPP}_(?:LIB|DOC|BIN|INCLUDE|MAN|MAN1|MAN2|MAN3|MAN4|MAN5|MAN6|MAN7|MAN8|ETC|STATE|LOCK|LOG)_PREFIX)=.*@\$1=@; s@(^${UCAPP}_PREFIX)=@\$1=$SANDPIT@;" $APPCONFIGFILE

#echo "appconfig file contains:"
#cat $APPCONFIGFILE

#  These following makes will produce some output which
#  varies from package to package, yet we have a package-
#  independent test reference file so we need to suppress
#  *all* out from these commands
echo "cleaning ..."
make clean distclean > /dev/null 2>&1
#make clean distclean

echo "making ..."
make > /dev/null 2>&1
#make 

echo "installing ..."
make install > /dev/null 2>&1
#make install
echo "installing done"
cd $OLDPWD

echo "checking all commands have man pages ..."
#  Assume all commands have man pages until proven otherwise
ALL_HAVE_MAN_PAGES=true
#  Commands are in bin or sbin
for CMD_DIR in bin sbin; do
    for CMD_FILE in $(ls $SANDPIT/$CMD_DIR 2>/dev/null); do
        #  Man page directory and extension vary accordingly
        case $CMD_DIR in
            bin)  MAN_EXT=1 ;;
            sbin) MAN_EXT=8 ;;
        esac
        #  Only report commands without man pages (as we want the
 	#  same test output to be applicable to all modules so we
  	#  want to ensure we don't see commands which do have man
	#  pages *and* whose names differ between modules (which of
	#  course they all do)).
        [ -f $SANDPIT/share/man/man$MAN_EXT/$CMD_FILE.$MAN_EXT ] || {
            echo "$CMD_FILE: has no man page"
            ALL_HAVE_MAN_PAGES=false
        }
    done
done

echo "all done, setting appropriate exit code ..."

#  Set a suitable exit code
$ALL_HAVE_MAN_PAGES
