#!/bin/sh

# 03.06.2006 Volker Quetschke
# Percent target definitions were not handled properly before.
# (issue 64869)

: ${DMAKEPROG:=dmake}
file1="mfile1.mk"
file2="mfile2.mk"
tmpfiles="$file1 $file2"

trap '{ echo "trapped signal - removing temporary files" ; rm -rf $tmpfiles ; }' 1 2 3 15

# Remove files from prior failed run
rm -rf $tmpfiles

# Remember to quote variables in generated makefiles( $ -> \$ ).
# Testcase 1 - This segfaulted before 4.5
cat > $file1 <<EOT
SHELL*:=/bin/sh 
SHELLFLAGS*:=-ce

allnew : all

%.x all  : %.c
	+true $@

qqq.c :
	+echo OK

EOT

# Testcase 2 - earlier than dmake 4.5 the second %-target was defined
# without the prerequisites, i.e. qqq.c was not build
cat > $file2 <<EOT
SHELL*:=/bin/sh 
SHELLFLAGS*:=-ce

allnew : qqq.y

%.x %.y  : %.c
	+true "building %.y"

qqq.c :
	+echo building qqq.c

EOT

output1=`eval ${DMAKEPROG} -r -f $file1 2>&1 `
result1=$?
output2=`eval ${DMAKEPROG} -r -f $file2 2>&1 `
result2=$?

if echo $output1 | grep 'Error: -- A non-%' > /dev/null 2>&1 ; then
  #echo "Test 1: Right result: $output1"
  result1=0
else
  echo "Test 1: Wrong result: $output1"
  echo
  result1=1
fi

if echo $output2 | grep -v 'building qqq.c' > /dev/null 2>&1 ; then
  echo "Test 2: Wrong result: $output2"
  echo
  result2=1
fi

if test $result1 -eq 0 -a $result2 -eq 0; then
  echo "Success - Cleaning up"
  rm -rf $tmpfiles
exit
  result=0
else
  echo "Failure!"
  result=1
fi
exit $result