#!/usr/bin/env ksh
# $Id: ereg.ksh,v 1.4 2009/11/27 23:06:04 ksb Exp $
# Regression test for efmd -- this is not so bad, really		(ksb)
# Like all of these this is not exhaustive.

: ${efmd_path:=${1:-efmd}}
unset M4_PATH

$efmd_path -V >/dev/null ||exit 1
$efmd_path -h >/dev/null ||exit 1

# we don't test all the xapply pass-throughs
EFDIR=/tmp/hrt$((RANDOM%1000)),$$
while ! mkdir -p $EFDIR ; do
	EFDIR=$EFDIR,$((RANDOM%100))
done
trap 'if [ -e core ] || [ -e efmd.core ] ; then
		file *core*
		ls -las `pwd`/*core*
		exit 80
	else
		cd /
		rm -rf $EFDIR
	fi' EXIT

cd $EFDIR
# There is a space on the end of the nostromo line below, and a tab on
# the end of the space line, all by intent. -- ksb
cat <<\! >two.cf
%HOST COLOR MAX			RAISE

localhost       .	10  	.	
nostromo	grey	.	. 
space		.	. 	localhost	
!
mkdir incs
cat <<\! >incs/gFunk.m4
define(MIN,9)dnl
!

cat <<\! >cmd.0
#!/bin/sh
# our usage -D HXMD_U_MERGED=path [m4-opts]
[ _-D = _"$1" ] || exit 1
eval $2
cat ${HXMD_U_MERGED?'not set!'}
exit 0
!
chmod +rx cmd.0

cat <<\! >MAX
file
!

# We don't check all the hostdb options -- hreg.ksh in hxmd did that.
# We need to check the command-line and m4 filter options -F, -o.

# one is the macro, the other the file
[ _"localhost 10 file nostromo MAX file space MAX file " = _"`$efmd_path      -Ctwo.cf 'HOST MAX' MAX | tr -s '\n ' '  '`" ] || exit 11
[ _"localhost 10 file nostromo MAX file space MAX file " = _"`$efmd_path -F 1 -Ctwo.cf 'HOST MAX' MAX | tr -s '\n ' '  '`" ] || exit 12
[ _"localhost 10 10 nostromo MAX MAX space MAX MAX " = _"`$efmd_path -F2 -Ctwo.cf 'HOST MAX' MAX | tr -s '\n ' '  '`" ] || exit 13
[ _"file localhost 10 file nostromo MAX file space MAX " = _"`$efmd_path -F -1 -Ctwo.cf MAX 'HOST MAX' | tr -s '\n ' '  '`" ] || exit 14

# We don't really check that -F failes with not enough arguments (it does).

# Check the we can see HXMD_U_MERGED in the m4 process, on the command-line.
[ _'%HOST MAX nostromo . ' = _"`M4_PATH=./cmd.0 $efmd_path -o 'MAX' -Ctwo.cf -BCOLOR '' | tr -s '\n ' '  '`" ] || exit 20

# Check -T
[ _'ksb wins' = _"`$efmd_path -T 'ksb wins' -Ctwo.cf -BLOST monkey`" ] || exit 30

# check for Cache.m4 processing
$efmd_path -V | grep 'cache recipe file:[ 	]*Cache.m4$' >/dev/null ||
	exit 37
cat >Cache.m4 <<\!
`nos'`tromo:
	echo "not from m4"
'dnl
!
# some m4's output #line for pastes, nothing I can do about that.
[ _"not from m4" = _"`$efmd_path -Ctwo.cf -BCOLOR -F0 . |grep -v '^#line'`" ] || exit 38
rm Cache.m4

 
# Check for jad's MACR0=. bug
[ _'10 0 0 ' = _"`$efmd_path -Ctwo.cf -DMAX=0 -Ctwo.cf MAX | tr -s '\n ' '  '`" ] || exit 50

exit 0
