#!/bin/sh
# Checks that a server left running for a walk was started with errors
# shown. A sandbox with errors off hides what Chris's machine prints, and
# a warning standing in front of a body that should be JSON stops that
# body parsing, so a screen reading it shows nothing at all. Three
# browser walks passed with errors off while the contact box on his
# machine offered no names.
#
# With no argument it looks at any php server running on this machine.
# With an argument it reads that address and refuses when what comes
# back does not begin the way a body of that kind should.
found=0
fail=0
for line in `ps ax -o args= 2>/dev/null | grep "[i]ndex.php" | \
    tr ' ' '\034'`; do
    found=1
    echo "$line" | tr '\034' ' ' | grep -q "display_errors=1" || {
        echo "check_errors_shown: FAIL a server is running without"
        echo "  errors shown. Start it with -d display_errors=1"
        echo "  -d error_reporting=E_ALL so a warning cannot hide."
        fail=1
    }
done
if [ -n "$1" ]; then
    body=`curl -s --max-time 8 "$1" 2>/dev/null`
    first=`printf '%s' "$body" | cut -c1`
    case "$first" in
        '{'|'['|'<'|'')
            ;;
        *)
            echo "check_errors_shown: FAIL what came back begins with"
            echo "  '$first' rather than a body. A warning ahead of a"
            echo "  body stops it being read:"
            printf '%s' "$body" | head -2 | sed 's/^/    /'
            fail=1
            ;;
    esac
fi
if [ "$fail" -eq 0 ]; then
    if [ "$found" -eq 0 ] && [ -z "$1" ]; then
        echo "check_errors_shown: no server is running to look at"
    else
        echo "check_errors_shown: PASS errors are shown and nothing"
        echo "  stands in front of a body"
    fi
fi
exit $fail
