#!/command/with-contenv bash
# shellcheck shell=bash disable=SC1091

source /scripts/common
s6wrap=(s6wrap --quiet --prepend="$(basename "$0")" --timestamps --args)
# If troubleshooting:
if chk_enabled "$DEBUG_LOGGING"; then
    set -x
fi

# make sure that it will also work with dump978 settings:
READSB_DEVICE_TYPE="${DUMP978_DEVICE_TYPE:-${READSB_DEVICE_TYPE}}"
READSB_ENABLE_BIASTEE="${DUMP978_ENABLE_BIASTEE:-${READSB_ENABLE_BIASTEE}}"
READSB_RTLSDR_DEVICE="${DUMP978_RTLSDR_DEVICE:-${READSB_RTLSDR_DEVICE}}"

# Note -- This is the (desired) behavior of the READSB_ENABLE_BIASTEE parameter:
# READSB_ENABLE_BIASTEE == on --> switch on BiasTee
# READSB_ENABLE_BIASTEE == off --> switch off BiasTee
# READSB_ENABLE_BIASTEE is unset --> do nothing

if [[ "$READSB_DEVICE_TYPE" == "rtlsdr" ]]; then
    "${s6wrap[@]}" echo "Device type is rtlsdr, checking for bias tee..."
    if chk_enabled "$READSB_ENABLE_BIASTEE" || chk_disabled "$READSB_ENABLE_BIASTEE"; then
        # Prepare temp file
        biast_tempfile=$(mktemp)
        # Attempt to get devices, use random serial so it's very unlikely we claim an SDR used by someone else
        rtl_biast -d "$RANDOM$RANDOM" > "$biast_tempfile" 2>&1 || true
        # Get number of devices
        num_devices=$(sed -n 's/^Found\s\+\([0-9]\+\)\s\+device(s):\s*$/\1/p' "$biast_tempfile")
        # If we have more than one device, we need a serial number
        if [[ "$num_devices" -gt 1 ]]; then
            "${s6wrap[@]}" echo "Found $num_devices devices, checking for serial number..."
            if [[ -n "$READSB_RTLSDR_DEVICE" ]]; then
                # For each line...
                while read -r line; do
                    # get device id for specific serial
                    # first try v3 sdrs
                    if echo "$line" | grep -oP '^([0-9]+):\s+(\w+),\s+(\w+),\s+SN:\s+([0-9a-zA-Z]{1,8})\s*$' > /dev/null 2>&1; then
                        device_id=$(echo "$line" | sed -n 's/^\([0-9]\+\):\s\+\(\w\+\),\s\+\(\w\+\),\s\+SN:\s\+\([0-9a-zA-Z]\{1,8\}\)\s*$/\1/p')
                        device_manufacturer=$(echo "$line" | sed -n 's/^\([0-9]\+\):\s\+\(\w\+\),\s\+\(\w\+\),\s\+SN:\s\+\([0-9a-zA-Z]\{1,8\}\)\s*$/\2/p')
                        device_model=$(echo "$line" | sed -n 's/^\([0-9]\+\):\s\+\(\w\+\),\s\+\(\w\+\),\s\+SN:\s\+\([0-9a-zA-Z]\{1,8\}\)\s*$/\3/p')
                        device_serial=$(echo "$line" | sed -n 's/^\([0-9]\+\):\s\+\(\w\+\),\s\+\(\w\+\),\s\+SN:\s\+\([0-9a-zA-Z]\{1,8\}\)\s*$/\4/p')

                        if [[ "$device_serial" == "$READSB_RTLSDR_DEVICE" ]]; then
                            "${s6wrap[@]}" echo "Enabling bias tee for device $device_id:  $device_manufacturer, $device_model, SN: $device_serial"
                            # shellcheck disable=SC2016
                            "${s6wrap[@]}" rtl_biast -d "$device_id" -b 1
                        fi
                        # if not, lets try for v4
                    elif echo "$line" | grep -oP '^([0-9]+):\s+(\w+,?\s?\w+\s?\w+),\s+SN:\s+([0-9a-zA-Z]{1,8})\s*$' > /dev/null 2>&1; then
                        device_id=$(echo "$line" | sed -n 's/\([0-9]\+\):\s\+\(\w\+\),\s\+\(\w\+\s\+\w\+\),\s\+SN:\s\+\([0-9a-zA-Z]\{1,8\}\)\s*$/\1/p')
                        device_manufacturer=$(echo "$line" | sed -n 's/\([0-9]\+\):\s\+\(\w\+\),\s\+\(\w\+\s\+\w\+\),\s\+SN:\s\+\([0-9a-zA-Z]\{1,8\}\)\s*$/\2/p')
                        device_model=$(echo "$line" | sed -n 's/\([0-9]\+\):\s\+\(\w\+\),\s\+\(\w\+\s\+\w\+\),\s\+SN:\s\+\([0-9a-zA-Z]\{1,8\}\)\s*$/\3/p')
                        device_serial=$(echo "$line" | sed -n 's/\([0-9]\+\):\s\+\(\w\+\),\s\+\(\w\+\s\+\w\+\),\s\+SN:\s\+\([0-9a-zA-Z]\{1,8\}\)\s*$/\4/p')

                        if [[ "$device_serial" == "$READSB_RTLSDR_DEVICE" ]] && chk_enabled "$READSB_ENABLE_BIASTEE"; then
                            "${s6wrap[@]}" echo "Enabling bias tee for device $device_id:  $device_manufacturer, $device_model, SN: $device_serial"
                            # shellcheck disable=SC2016
                            "${s6wrap[@]}" rtl_biast -d "$device_id" -b 1
                        elif [[ "$device_serial" == "$READSB_RTLSDR_DEVICE" ]] && chk_disabled "$READSB_ENABLE_BIASTEE"; then
                            "${s6wrap[@]}" echo "Disabling bias tee for device $device_id:  $device_manufacturer, $device_model, SN: $device_serial"
                            "${s6wrap[@]}" rtl_biast -d "$device_id" -b 0
                        fi

                    fi
                done < "$biast_tempfile"
            fi
        else
            if chk_enabled "$READSB_ENABLE_BIASTEE"; then
                "${s6wrap[@]}" echo "Enabling bias tee..."
                "${s6wrap[@]}" rtl_biast -b 1
            elif chk_disabled "$READSB_ENABLE_BIASTEE"; then
                "${s6wrap[@]}" echo "Disabling bias tee..."
                "${s6wrap[@]}" rtl_biast -b 0
            fi
        fi
        rm "$biast_tempfile"
    else
        "${s6wrap[@]}" echo "READSB_ENABLE_BIASTEE is not set to either ON or OFF, skipping..."
    fi
else
    "${s6wrap[@]}" echo "Device type is not rtlsdr, skipping..."
fi