#! /bin/bash -s --

tmpdir=$(mktemp -d -t com.parallels.pmm.IgnoreBigSur.discovery)
trap "rm -rf $tmpdir" 0 2 3 15

BIG_SUR_UPDATE_LABEL="macOS Big Sur"

WriteResult() { echo "$@"; }

WriteLog() { echo >&2 "$@"; }

IsBigSurIgnored()
{
    local suPrefs="/Library/Preferences/com.apple.SoftwareUpdate.plist"

    let i=0
    while true; do
        local label 
        label=$(/usr/libexec/PlistBuddy -c "Print InactiveUpdates:$i" "$suPrefs" 2>/dev/null)
        if [ $? -ne 0 ]; then
            return 1 # No more labels
        fi

        if [[ $label == $BIG_SUR_UPDATE_LABEL ]]; then
            return 0 # Found
        fi
        let i++
    done
}


if IsBigSurIgnored; then
    WriteLog "$BIG_SUR_UPDATE_LABEL is already ignored"
    WriteResult "Yes"
else
    WriteLog "$BIG_SUR_UPDATE_LABEL is not yet ignored"
    WriteResult "No"
fi
