#!/bin/sh
# Run as the signed-in test user in the isolated N270/HDA/Xfce guest.
set -eu
export LC_ALL=C
export XDG_RUNTIME_DIR="/run/user/$(id -u)"
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
echo NEWWORLD_HARDWARE_BEGIN
dpkg --print-architecture
uname -m
lscpu | grep -E 'Model name|Architecture'
dpkg-query -W alsa-utils avahi-daemon cups cups-filters guvcview ipp-usb pulseaudio-module-bluetooth \
    systemd-timesyncd v4l-utils wireless-regdb wpasupplicant
/usr/sbin/wpa_supplicant -v
lpstat -r
nmcli --terse --fields STATE general
timedatectl show -p Timezone -p CanNTP -p NTP
aplay -l
arecord -l
pactl info | grep -E 'Server Name|Default Sink|Default Source'
arecord -q -D pulse -c 1 -r 16000 -f S16_LE -t wav -d 1 "$tmp/microphone.wav"
test "$(wc -c < "$tmp/microphone.wav")" -ge 32000
aplay -q -D pulse "$tmp/microphone.wav"
echo EMULATED_HDA_CAPTURE_AND_PLAYBACK_OK
sudo modprobe vivid n_devs=1
udevadm settle
v4l2-ctl --list-devices
device=
for entry in /sys/class/video4linux/video*; do
    case "$(cat "$entry/name")" in
        *vid-cap*|*Video*Capture*) device="/dev/${entry##*/}"; break ;;
    esac
done
test -n "$device"
v4l2-ctl -d "$device" --set-fmt-video=width=320,height=240,pixelformat=YUYV \
    --stream-mmap=3 --stream-count=3 --stream-to="$tmp/camera.raw"
test "$(wc -c < "$tmp/camera.raw")" -ge 460800
echo VIRTUAL_V4L2_CAPTURE_OK
echo NEWWORLD_HARDWARE_FINISHED
