#!/bin/sh
# PCP QA Test No. 853
# pmcpp -s testing
#
# Copyright (c) 2015 Ken McDonell.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

# handle these ...
# 1 "/tmp/853-3145.inc"
#
_filter()
{
    sed \
	-e "/^# [1-9]/s@$tmp@TMP@" \
    # end
}

status=1	# failure is the default!
$sudo rm -rf $tmp.* $seq.full
trap "cd $here; rm -rf $tmp.* $tmp; exit \$status" 0 1 2 3 15

# real QA test starts here
cat <<End-of-File >$tmp.inc
%define eek urk
%define mumble 'mumble and "fumble"'
text from .inc file
End-of-File
cat <<End-of-File >$tmp.in
%include "$tmp.inc"
Firstly, there is the mumble ...
%ifdef eek
what about eek ? expect this to be \u\r\k not \e\e\k
%endif
eek at start of line
at end of line eek
text from .in file
%ifndef test_else
not-defined if-part
%else
not-defined else-part
%endif
%ifdef test_else
defined if-part
%else
defined else-part
%endif
Text at end of file.
End-of-File

echo "=== simple cases ==="
echo "----------------------"
pmcpp -s <$tmp.in 2>&1 | _filter
echo "----------------------"
pmcpp -s --define=test_else=1 <$tmp.in 2>&1 | _filter
echo "----------------------"

echo
echo "=== expansion with -r ==="
echo "----------------------"
pmcpp -sr <$tmp.in 2>&1 | _filter
echo "----------------------"
cat <<End-of-File >$tmp.in2
%include "$tmp.inc"
Firstly, there is the mumble ...
%ifdef eek
what about %eek ? expect this to be \u\r\k not \e\e\k
and what about %{eek} ? expect this to be \u\r\k not \e\e\k
%endif
%eek <percent>eek at start of line
<percent>eek at end of line %eek
%{eek} <percent>{eek} at start of line
<percent>{eek} at end of line %{eek}
text from .in file
End-of-File
pmcpp -srd <$tmp.in2 2>&1 | _filter
echo "Check lines in and out ..."
echo "in: `wc -l <$tmp.in2 | sed -e 's/ //g'` + `wc -l <$tmp.inc | sed -e 's/ //g'`"
echo "out: `pmcpp -sr <$tmp.in2 | wc -l | sed -e 's/ //g'`"
echo "----------------------"

echo
echo "=== -D on command line cases ==="
echo "----------------------"
pmcpp -s -D text="blah=blah=blah=..." <$tmp.in 2>&1 | _filter
echo "----------------------"
pmcpp -s -D 'text="blah blah blah ..."' <$tmp.in 2>&1 | _filter
echo "----------------------"

# Note error case coverage is spotty ... real testing for errors
# is in qa/463 (without -s)
#
echo
echo "=== error cases ==="
echo "----------------------"
cat <<End-of-File >$tmp.in
foo
%ifdef eek
bar
End-of-File
pmcpp -s <$tmp.in 2>&1 | _filter
echo "----------------------"
cat <<End-of-File >$tmp.in
%macro value with embedded newline - bad
%define multi 'one line
two lines
three lines'
And is this multi multiple lines?
End-of-File
pmcpp -sr <$tmp.in 2>&1 | _filter
echo "----------------------"
cat <<End-of-File >$tmp.in
# exposed #else outside #if ... scope
test
#else
blah
#endif
End-of-File
pmcpp -r <$tmp.in 2>&1 | _filter
echo "----------------------"

echo
echo "=== macro name and corner-case expansion tests ==="
echo "----------------------"
cat <<End-of-File >$tmp.in
%define _underscore_all_over_ urk
What about _underscore_all_over_?  [expect urk]
End-of-File
pmcpp -s <$tmp.in 2>&1 | _filter
echo "----------------------"
cat <<End-of-File >$tmp.in
%define long_macro_name_and_short_value "!"
%define x '^a very long macro value to be sure, to be sure$'
some text %%{long_macro_name_and_short_value}%{long_macro_name_and_short_value}%{long_macro_name_and_short_value}% [expect %!!!%]
[expect ^...$^...$^...$] %x%x%x
End-of-File
pmcpp -d -rs <$tmp.in 2>&1 | _filter
echo "----------------------"
cat <<End-of-File >$tmp.in
#define Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch LLanfair
The town of Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch is a village in Wales and is famous for being apparently the longest place name in the world.  There are lots of web pages mentioning Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch.  Locally Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch is just too much of a mouthful and so it is truncated from "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch" to "Llanfair".  With one more mention of Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch, I think this line will be long enough.
Phew that was a long input line ... 593 chars!
End-of-File
pmcpp -d <$tmp.in 2>&1 | _filter
echo "----------------------"

echo
echo "=== conditionals, bells and whistles ==="
echo "----------------------"
cat <<End-of-File >$tmp.freq
%define dk_freq 1minute
%define cpu_freq '15 sec'
End-of-File
cat <<End-of-File >$tmp.in
%include "$tmp.freq"

log mandatory on %dk_freq {
    disk.dev
}

%ifdef want_cpu
%define cpu_pfx 'kernel.all.cpu.'
log mandatory on %cpu_freq {
    %{cpu_pfx}user
    %{cpu_pfx}sys
}
%endif
End-of-File
pmcpp -rs <$tmp.in 2>&1 | _filter
echo "----------------------"
pmcpp -rs -Dwant_cpu <$tmp.in  2>&1 | _filter
echo "----------------------"

echo
echo "=== -I tests ==="
mkdir $tmp
cd $tmp
cat <<End-of-File >a
#include <b>
Text from a.
End-of-File
cat <<End-of-File >b
Text from b.
End-of-File
echo "----------------------"
pmcpp -d a 2>&1 | _filter
mkdir sub
mv b sub
echo "----------------------"
pmcpp -d a 2>&1 | _filter
echo "----------------------"
pmcpp -d -I sub a 2>&1 | _filter
cat <<End-of-File >sub/b
#include "c-foobar"
Text from b.
End-of-File
mkdir sub/sub-sub
cat <<End-of-File >sub/sub-sub/c-foobar
Text from c-foobar.
End-of-File
echo "----------------------"
pmcpp -d -I sub -I sub/sub-sub a 2>&1 | _filter
echo "----------------------"
mv sub/sub-sub/c-foobar sub
mv a sub
pmcpp -d sub/a 2>&1 | _filter
echo "----------------------"
cd $here
echo "#include <Rebuild>" | pmcpp -P >$tmp.out
echo expect no diff lines ...
diff $tmp.out $PCP_VAR_DIR/pmns/Rebuild
echo "----------------------"

# success, all done
status=0

exit
