#!/bin/sh
# Landed by msks-setup-macos-host: the GUI's dictate pill (#1216)
# calls PipeWire's pw-record, which does not exist on macOS; this
# shim answers the same contract — raw s16le mono at the asked
# rate on stdout — through ffmpeg's avfoundation capture. Replace
# it only through a re-run of the tool.
#
# The avfoundation audio device: the desktop's default input
# (`:default`), or an index — list the devices with
# `ffmpeg -f avfoundation -list_devices true -i ""` — set through
# PW_RECORD_AUDIO_INDEX, which stands above the GUI's Device
# setting — honored when it carries a number (a PipeWire node name
# has no avfoundation equivalent and reads as the default).
rate=16000
channels=1
device=""
pinned="${PW_RECORD_AUDIO_INDEX:-}"

while [ $# -gt 0 ]; do
    case "$1" in
        --rate) rate="$2"; shift 2 ;;
        --channels) channels="$2"; shift 2 ;;
        --format) shift 2 ;;   # s16 assumed (s16le is what we emit)
        --raw) shift ;;
        --target)
            case "$2" in
                ''|*[!0-9]*) ;;         # a name or blank: the default
                *) device="$2" ;;
            esac
            shift 2 ;;
        -) shift ;;            # stdout — the only output the client reads
        *) echo "pw-record shim: ignoring unrecognized argument: $1" >&2; shift ;;
    esac
done

# The environment's index stands above the setting, as every pin.
device="${pinned:-${device:-default}}"
exec ffmpeg -hide_banner -loglevel error -f avfoundation -i ":$device" \
    -ar "$rate" -ac "$channels" -f s16le -flush_packets 1 -
