#! /bin/sh -

BASEURI=http://www.astro.gla.ac.uk/users/norman/ivoa/vocabularies/rdf
DISTNAME=vocabularies-20090621

check_uri () {
    uri=$1
    header=$2
    if test -n "$3"; then accept="-H $3"; else accept=""; fi
    if test -z "$header"; then
        curl --head $accept $uri 2>/dev/null | sed -n "1s/HTTP[^ ]* *\([0-9]*\).*/status=\1/p"
    else
        curl --head $accept $uri 2>/dev/null | sed -n "1s/HTTP[^ ]* *\([0-9]*\).*/status=\1/p
s/^$header: *\([^[:space:]]*\).*/$header=\1/p"
    fi
}

check_status () {
    # $1 = URL to check
    # $2 = expected status
    eval `check_uri $1`
    if test "$status" = $2; then
        echo "OK: $1"
    else
        echo "Failed: $1 produced status=$status"
        echo "    expected status=$2"
        allok=false
    fi
}

check_status_and_location () {
    # $1 = URL to check
    # $2 = expected status
    # $3 = expected location
    # $4 = extra header for curl
    eval `check_uri $1 Location $4`
    if test "$status" = $2 -a "$Location" = $3; then
        echo "OK: $1 [$4]"
    else
        echo "Failed: $1 produced status=$status, Location=$Location"
        echo "    expected status=$2, Location=$3"
        allok=false
    fi
}
    

allok=:

# Check redirections (just do it for the AAkeys, since the 
# others should be identical)

check_status_and_location $BASEURI/AAkeys \
    303 \
    $BASEURI/$DISTNAME/AAkeys/AAkeys.html
check_status_and_location $BASEURI/AAkeys \
    303 \
    $BASEURI/$DISTNAME/AAkeys/AAkeys.html \
    accept:text/html
check_status $BASEURI/$DISTNAME/AAkeys/AAkeys.html 200

check_status_and_location $BASEURI/AAkeys \
    303 \
    $BASEURI/$DISTNAME/AAkeys/AAkeys.ttl \
    accept:text/rdf+n3
check_status_and_location $BASEURI/AAkeys \
    303 \
    $BASEURI/$DISTNAME/AAkeys/AAkeys.ttl \
    accept:text/turtle
check_status $BASEURI/$DISTNAME/AAkeys/AAkeys.ttl 200


check_status_and_location $BASEURI/AAkeys \
    303 \
    $BASEURI/$DISTNAME/AAkeys/AAkeys.rdf \
    accept:application/rdf+xml
check_status $BASEURI/$DISTNAME/AAkeys/AAkeys.rdf 200


# Mappings file (basically the same as the vocabularies files)
check_status_and_location $BASEURI/AAkeys2AVM \
    303 \
    $BASEURI/$DISTNAME/AAkeys2AVM/AAkeys2AVM.html
check_status_and_location $BASEURI/AAkeys2AVM \
    303 \
    $BASEURI/$DISTNAME/AAkeys2AVM/AAkeys2AVM.rdf \
    accept:application/rdf+xml
check_status $BASEURI/$DISTNAME/AAkeys2AVM/AAkeys2AVM.rdf 200
check_status_and_location $BASEURI/AAkeys2AVM \
    303 \
    $BASEURI/$DISTNAME/AAkeys2AVM/AAkeys2AVM.ttl \
    accept:text/turtle
check_status $BASEURI/$DISTNAME/AAkeys2AVM/AAkeys2AVM.ttl 200

# check location of tarball

check_status $BASEURI/$DISTNAME.tar.gz 200

# How'd we get on?

if $allok; then
    echo "All tests passed!"
    exit 0
else
    echo "Some tests failed.  Boo!"
    exit 1
fi
