#!/bin/sh
set -e

PROFILE=/usr/share/osrm/profiles/car.lua
PORT=15000

TESTDIR=$(dirname "$(readlink -f "$0")")
PBF="$TESTDIR/monaco-260425.osm.pbf"
GOLDEN="$TESTDIR/golden"

TMPDIR=$(mktemp -d)
cleanup() {
    [ -n "$OSRM_PID" ] && kill "$OSRM_PID" 2>/dev/null || true
    rm -rf "$TMPDIR"
}
trap cleanup EXIT

BASE="$TMPDIR/monaco.osrm"

echo "--- osrm-extract (car profile, Monaco) ---"
osrm-extract -p "$PROFILE" -o "$BASE" "$PBF"

echo "--- osrm-contract (CH) ---"
osrm-contract "$BASE"

echo "--- osrm-routed trial mode ---"
osrm-routed --algorithm CH --trial=1 "$BASE"

echo "--- starting osrm-routed ---"
osrm-routed --algorithm CH --port "$PORT" --threads 2 "$BASE" &
OSRM_PID=$!

# Wait up to 30 seconds for the server to start accepting connections
i=0
until curl -s "http://127.0.0.1:$PORT/" > /dev/null 2>&1; do
    i=$((i + 1))
    if [ "$i" -ge 30 ]; then
        echo "ERROR: osrm-routed did not become ready within 30 seconds"
        exit 1
    fi
    sleep 1
done
echo "Server ready after ${i}s"

check_response() {
    local label="$1"
    local actual_file="$2"
    local golden_file="$GOLDEN/$label.json"
    local response
    response=$(cat "$actual_file")

    echo "Response [$label]: $response"

    if ! echo "$response" | grep -q '"code":"Ok"'; then
        echo "ERROR [$label]: expected \"code\":\"Ok\""
        exit 1
    fi

    if ! diff -u "$golden_file" "$actual_file"; then
        echo "ERROR [$label]: response differs from golden file $golden_file"
        exit 1
    fi
    echo "OK [$label]: matches golden"
}

check_code_ok() {
    local label="$1"
    local response="$2"
    echo "Response [$label]: $response"
    if ! echo "$response" | grep -q '"code":"Ok"'; then
        echo "ERROR [$label]: expected \"code\":\"Ok\""
        exit 1
    fi
    echo "OK [$label]"
}

echo "--- route: Casino de Monte-Carlo -> Palais Princier ---"
# Coordinates are lon,lat:  Casino 7.4272,43.7384  Palace 7.4200,43.7309
curl -s "http://127.0.0.1:$PORT/route/v1/driving/7.4272,43.7384;7.4200,43.7309?overview=false" \
    > "$TMPDIR/route.json"
check_response route "$TMPDIR/route.json"

echo "--- nearest: snap coordinate to road network ---"
curl -s "http://127.0.0.1:$PORT/nearest/v1/driving/7.4272,43.7384?number=1" \
    > "$TMPDIR/nearest.json"
check_response nearest "$TMPDIR/nearest.json"

echo "--- table: duration matrix for three Monaco waypoints ---"
curl -s "http://127.0.0.1:$PORT/table/v1/driving/7.4272,43.7384;7.4200,43.7309;7.4250,43.7350" \
    > "$TMPDIR/table.json"
check_response table "$TMPDIR/table.json"

echo "--- route with steps ---"
resp=$(curl -s "http://127.0.0.1:$PORT/route/v1/driving/7.4272,43.7384;7.4200,43.7309?steps=true&overview=false")
check_code_ok "route-steps" "$resp"
if ! echo "$resp" | grep -q '"maneuver"'; then
    echo "ERROR [route-steps]: no step maneuvers found in response"
    exit 1
fi
echo "OK [route-steps]: steps returned"

echo "--- match: GPS trace map-matching ---"
# Three points near Casino de Monte-Carlo; radiuses give matching slack
resp=$(curl -s "http://127.0.0.1:$PORT/match/v1/driving/7.4272,43.7384;7.4250,43.7360;7.4200,43.7309?timestamps=0;20;40&radiuses=50;50;50&overview=false")
check_code_ok "match" "$resp"
if ! echo "$resp" | grep -q '"matchings"'; then
    echo "ERROR [match]: no matchings array in response"
    exit 1
fi
echo "OK [match]: matchings returned"

echo "--- trip: travelling salesman for three Monaco waypoints ---"
resp=$(curl -s "http://127.0.0.1:$PORT/trip/v1/driving/7.4272,43.7384;7.4200,43.7309;7.4250,43.7350?overview=false")
check_code_ok "trip" "$resp"
if ! echo "$resp" | grep -q '"trips"'; then
    echo "ERROR [trip]: no trips array in response"
    exit 1
fi
echo "OK [trip]: trips returned"

echo "--- tile: vector tile covering central Monaco (zoom 15) ---"
# Tile coordinates for Monaco at zoom 15: x=17059, y=11949, z=15
# URL format: /tile/v1/{profile}/tile(x,y,z).mvt
http_code=$(curl -s -o "$TMPDIR/tile.mvt" -w "%{http_code}" \
    "http://127.0.0.1:$PORT/tile/v1/driving/tile(17059,11949,15).mvt")
if [ "$http_code" != "200" ]; then
    echo "ERROR [tile]: expected HTTP 200, got $http_code"
    exit 1
fi
tile_size=$(wc -c < "$TMPDIR/tile.mvt")
if [ "$tile_size" -eq 0 ]; then
    echo "ERROR [tile]: received empty MVT response"
    exit 1
fi
echo "OK [tile]: received ${tile_size} bytes of MVT data"

echo "--- all routing checks passed ---"
