#! /bin/sh

. ${srcdir:=.}/t.frame

# t.passthrough-hb
#
#	test for correct detection of line separating header and body

$BOGOFILTER -e -p -C < "$srcdir/inputs/headerbody.txt" > "$TMPDIR/output"
$AWK '/^X-Bogosity:/ { bl=NR } \
      /^Summary:/    { sl=NR } \
      END            { if (sl > bl) { exit 1; } }' \
  < "$TMPDIR"/output

CFG="$TMPDIR/test.cf"

cat >> "$CFG" <<EOF
header_format = %h: %c, tests=bogofilter, spamicity=%p
EOF

# test two additional scenarios
#	- one with a body
#	- one with no body

cat > "$TMPDIR"/reference.2 <<EOF
Header: body and empty line
 head2
Another-Header: head3
 
after non-empty blank -- X-Bogosity should be next
X-Bogosity: Unsure, tests=bogofilter, spamicity=0.520000

after empty line
EOF

cat > "$TMPDIR/reference.3" <<EOF
Header: no body
 head2
Another-Header: head3 -- X-Bogosity should be next
X-Bogosity: Unsure, tests=bogofilter, spamicity=0.520000

after non-empty blank
 
no body
EOF

cat <<EOF |tr "^" "\r" > "$TMPDIR"/reference.4
Header: no body^
 head2^
Another-Header: head4 -- X-Bogosity should be next^
X-Bogosity: Unsure, tests=bogofilter, spamicity=0.520000^
^
after non-empty blank^
 ^
no body^
EOF

printf "Header: body and empty line\n head2\nAnother-Header: head3\n \nafter non-empty blank -- X-Bogosity should be next\n\nafter empty line" > "$TMPDIR/input.2"
$BOGOFILTER -e -p -c "$CFG" < "$TMPDIR"/input.2 > "$TMPDIR/results.2"

printf "Header: no body\n head2\nAnother-Header: head3 -- X-Bogosity should be next\n \nafter non-empty blank\n \nno body" > "$TMPDIR/input.3"
$BOGOFILTER -e -p -c "$CFG" < "$TMPDIR"/input.3 > "$TMPDIR/results.3"

printf "Header: no body\r\n head2\r\nAnother-Header: head4 -- X-Bogosity should be next\r\n \r\nafter non-empty blank\r\n \r\nno body" > "$TMPDIR/input.4"
$BOGOFILTER -e -p -c "$CFG" < "$TMPDIR"/input.4 > "$TMPDIR/results.4"

for f in 2 3 4 ; do
    if  [ $verbose -eq 0 ]; then
	cmp "$TMPDIR/reference.$f" "$TMPDIR/results.$f"
    else
	set +e
	diff $DIFF_BRIEF "$TMPDIR"/reference.$f "$TMPDIR/results.$f"
    fi
done
