#!/bin/sh
set -eu

echo "Pantum M7 driverless scan check"
echo "================================"

check_command() {
    name="$1"
    if command -v "$name" >/dev/null 2>&1; then
        echo "OK   $name: $(command -v "$name")"
        return 0
    fi
    echo "MISS $name"
    return 1
}

check_command scanimage || true
check_command airscan-discover || true
check_command ipp-usb || true
check_command avahi-browse || true
check_command curl || true

echo
echo "SANE devices:"
if command -v scanimage >/dev/null 2>&1; then
    scanimage -L || true
else
    echo "scanimage is not installed"
fi

echo
echo "AirScan discovery:"
if command -v airscan-discover >/dev/null 2>&1; then
    airscan-discover || true
else
    echo "airscan-discover is not installed"
fi

echo
echo "ipp-usb service:"
if command -v systemctl >/dev/null 2>&1; then
    systemctl is-active ipp-usb 2>/dev/null || true
else
    echo "systemctl is not available"
fi

echo
echo "Local ipp-usb eSCL endpoints:"
if command -v curl >/dev/null 2>&1; then
    for port in 60000 60001 60002 60003 60004; do
        url="http://127.0.0.1:$port/eSCL/ScannerCapabilities"
        if curl -fsS --max-time 2 "$url" >/dev/null 2>&1; then
            echo "OK   $url"
        else
            echo "MISS $url"
        fi
    done
else
    echo "curl is not installed"
fi

echo
echo "If no airscan device appears, the device may not expose eSCL/AirScan."
echo "Pantum private USB scan still needs the amd64 vendor SANE backend or a native backend."
