#!/bin/sh
set -ex

echo "=== Verifying cuttlefish-base and cuttlefish-user executables ==="
for bin in \
    /usr/bin/cvd \
    /usr/bin/cvd_host_orchestrator \
    /usr/lib/cuttlefish-common/bin/operator \
    /usr/lib/cuttlefish-common/bin/operator_proxy \
    /usr/lib/cuttlefish-common/bin/webRTC; do
    if [ ! -x "$bin" ]; then
        echo "ERROR: $bin is not executable or does not exist"
        exit 1
    fi
done

echo "=== Verifying WebRTC and operator static/symlinked assets ==="
for asset in \
    /usr/lib/cuttlefish-common/usr/share/webrtc/assets/client.html \
    /usr/lib/cuttlefish-common/usr/share/webrtc/assets/js/jquery.min.js \
    /usr/lib/cuttlefish-common/usr/share/webrtc/assets/css/material-design-icons-no-codepoints.css \
    /usr/lib/cuttlefish-common/usr/share/webrtc/assets/css/fonts/MaterialIcons-Regular.woff2 \
    /usr/share/cuttlefish-common/operator/static/index.html \
    /usr/share/cuttlefish-common/operator/static/main.js \
    /usr/share/cuttlefish-common/operator/static/polyfills.js \
    /usr/share/cuttlefish-common/operator/static/indigo-pink.css \
    /usr/share/cuttlefish-common/operator/static/css/material-design-icons-no-codepoints.css \
    /usr/share/cuttlefish-common/operator/static/css/fonts/MaterialIcons-Regular.woff2 \
    /usr/share/cuttlefish-common/operator/intercept/js/server_connector.js; do
    if [ ! -s "$asset" ]; then
        echo "ERROR: $asset is missing, unreadable, or empty"
        exit 1
    fi
done

echo "=== Verifying no remote CDN/Google Fonts URLs remain in HTML ==="
for html in \
    /usr/lib/cuttlefish-common/usr/share/webrtc/assets/client.html \
    /usr/share/cuttlefish-common/operator/static/index.html; do
    if grep -E 'fonts\.googleapis\.com|fonts\.gstatic\.com|ajax\.googleapis\.com' "$html"; then
        echo "ERROR: remote CDN/font URL found in $html"
        exit 1
    fi
done

echo "=== Preparing minimal Cuttlefish config for subprocess logging ==="
cf_dir="${AUTOPKGTEST_TMP:-/tmp}/cf-smoke"
mkdir -p "$cf_dir/instances/cvd-1/logs"
cat > "$cf_dir/cuttlefish_config.json" <<EOF
{"root_dir": "$cf_dir", "instances": {"1": {"run_as_daemon": false}}}
EOF
export CUTTLEFISH_CONFIG_FILE="$cf_dir/cuttlefish_config.json"
export CUTTLEFISH_INSTANCE=1

echo "=== Testing webRTC --help (verifies libtgowt/OpenSSL/Wayland dynamic linkage) ==="
webrtc_help=$(/usr/lib/cuttlefish-common/bin/webRTC --help 2>&1 || true)
printf '%s\n' "$webrtc_help" | grep -q -- '-client_dir'

echo "=== Testing operator_proxy --help ==="
proxy_help=$(/usr/lib/cuttlefish-common/bin/operator_proxy --help 2>&1 || true)
printf '%s\n' "$proxy_help" | grep -q -- '-operator_port'

echo "=== Testing operator -help ==="
op_help=$(/usr/lib/cuttlefish-common/bin/operator -help 2>&1 || true)
printf '%s\n' "$op_help" | grep -q -- '-socket_path'

echo "=== Testing cvd_host_orchestrator -help ==="
ho_help=$(/usr/bin/cvd_host_orchestrator -help 2>&1 || true)
printf '%s\n' "$ho_help" | grep -q -- '-operator_http_port'

echo "=== test-smoke passed successfully! ==="
