#!/bin/bash

set -u

freq='87.9M'

if ! lsusb -d '0bda:2838' > /dev/null; then
    echo "UNKNOWN - no RTL-SDR found" 2> /dev/null
    exit 3
fi

# TODO: save all output in a tempfile and output it after first line
# dovremmo cercare .ps, ma non trovandolo cerchiamo .pi che esce piu spesso
lines=$(rtl_fm -M fm -l 0 -A std -p 0 -s '171k' -g 20 -F 9 -f "$freq" 2> /dev/null | \
        /usr/bin/timeout -s INT 10 redsea | \
        jq -r 'select(.pi != null) | .pi'|uniq|wc -l)

if [[ "$lines" -gt 0 ]]; then
    echo "OK: rds $lines times"
    exit 0
else
    echo "CRITICAL: no rds found"
    exit 2
fi
