#!/bin/bash

# Outputs of varnishevent and varnishncsa for client formats are
# identical, except that varnishevent emits empty strings for data
# from headers that are empty, and varnishnca emits a '-'.
#
# Unfortunately an exact comparison cannot be made, because
# varnishncsa seems to have some errors (URL entries apparently not
# always cleanly cleared). So we check that the nummber of diff hunks
# is not greater than a threshold.

echo
echo "TEST: $0"
echo "... testing (near) equivalence of output with varnishncsa"

TMP=${TMPDIR:-/tmp}
EVENT="../varnishevent"
NCSA="../../varnishncsa/varnishncsa"

EVENT_LOG=$TMP/event.log
NCSA_LOG=$TMP/ncsa.log

DIFF_CMD="diff -U 0 $EVENT_LOG $NCSA_LOG"
HUNK_THRESH=1

echo "... default format"
$EVENT -r varnish.binlog | sed 's/-//g' > $EVENT_LOG
$NCSA -r varnish.binlog | sed 's/-//g' > $NCSA_LOG

HUNKS=$($DIFF_CMD | grep ^@ | wc -l)
rm $EVENT_LOG
rm $NCSA_LOG

if [ "$HUNKS" -gt "$HUNK_THRESH" ]; then
    echo "ERROR: outputs of no-arg varnishevent and varnishncsa differ"
    echo "$HUNKS diff hunks"
    exit 1
fi

echo "... -f format"
$EVENT -r varnish.binlog -f | sed 's/-//g' > $EVENT_LOG
$NCSA -r varnish.binlog -f | sed 's/-//g' > $NCSA_LOG

HUNKS=$($DIFF_CMD | grep ^@ | wc -l)
rm $EVENT_LOG
rm $NCSA_LOG

if [ "$HUNKS" -gt "$HUNK_THRESH" ]; then
    echo "ERROR: outputs of varnishevent/varnishncsa -f differ"
    echo "$HUNKS diff hunks"
    exit 1
fi

FORMAT="%b %H %h %{Host}i %{User-Agent}i %{Accept-Ranges}o %{Age}o %{Connection}o %{Content-Length}o %{Content-Type}o %{Date}o %{ETag}o %{Last-Modified}o %{Server}o %{Transfer-Encoding}o %{Vary}o %{Via}o %{X-Varnish}o %l %m %q %r %s %t %{%F-%T}t %U %u %{Varnish:time_firstbyte}x %{Varnish:hitmiss}x %{Varnish:handling}x %{VCL_Log:xid}x"

echo "... custom -F format"
$EVENT -r varnish.binlog -F $FORMAT | sed 's/-//g' > $EVENT_LOG
$NCSA -r varnish.binlog -F $FORMAT | sed 's/-//g' > $NCSA_LOG

HUNKS=$($DIFF_CMD | grep ^@ | wc -l)
rm $EVENT_LOG
rm $NCSA_LOG

if [ "$HUNKS" -gt "$HUNK_THRESH" ]; then
    echo "ERROR: outputs of varnishevent/varnishncsa -F differ"
    echo "$HUNKS diff hunks"
    exit 1
fi

exit 0
