#! /bin/bash -s --

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

BIG_SUR_UPDATE_LABEL="macOS Big Sur"
SU_TOOL='/usr/sbin/softwareupdate'

WriteResult() { echo "$@"; }

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

IgnoreBigSur()
{
    local tmpfile="$tmpdir/su.txt"
    rm -f "$tmpfile"

    "$SU_TOOL" --ignore "$BIG_SUR_UPDATE_LABEL" >"$tmpfile" 2>&1
    if [ $? -ne 0 ]; then
        WriteLog "Failed to add $BIG_SUR_UPDATE_LABEL to ignore list:"
        WriteLog $(<"$tmpfile")
        return 1
    fi

    sleep 5 # Wait for saving SU preferences to disk
    return 0
}

IgnoreBigSur && WriteLog "$BIG_SUR_UPDATE_LABEL is added to ignore list"