#!/bin/zsh --no-rcs

# Support App Extension - App Auto-Patch Pending Updates Count
#
# Populates a Root3 Support App Extension with the number of apps that have
# an update pending, read from App Auto-Patch 4's patch status file,
# /Library/Application Support/AppAutoPatch/compliance.json. Its pendingCount
# takes the place of the ItemsToInstall array in 3.x's
# xyz.techitout.appAutoPatchReport.plist.
#
# REQUIREMENTS:
# - App Auto-Patch 4.0.0 (105) or later, with its root helper approved (the
#   helper writes compliance.json)
# - Deployed as a Privileged Script referenced by a Support App Extension's
#   OnAppearAction, via a Support App Configuration Profile
#
# Intended to run every time the Support App popover appears, so the count is
# always current. It only reads compliance.json, which the root helper checks
# once a minute and rewrites when something changes. See the companion script,
# aap_pending_apps_dialog.zsh, for the action that opens App Auto-Patch's
# dialog and then runs this script again to refresh the count.

# ------------------    edit the variables below this line    ------------------

# Extension ID - must match the ExtensionID configured for this Extension in
# the Support App Configuration Profile
extension_id="aap_pending_updates"

# Text shown when there are zero pending updates
uptodate_text="Up to Date"

# Text shown before App Auto-Patch has checked for updates on this Mac
notchecked_text="Not Checked Yet"

# ---------------------    do not edit below this line    ----------------------

# Support App preference plist
preference_file_location="/Library/Preferences/nl.root3.support.plist"

# App Auto-Patch's patch status, written by its root helper
compliance_file="/Library/Application Support/AppAutoPatch/compliance.json"

# Start spinning indicator
defaults write "${preference_file_location}" "${extension_id}_loading" -bool true

# Show placeholder while loading
defaults write "${preference_file_location}" "${extension_id}" -string "KeyPlaceholder"

# pendingCount leaves out labels the settings ignore and apps App Auto-Patch
# has installed since its last check. With no file yet (the helper hasn't run)
# or no check yet, say so rather than "Up to Date".
aap_status=""
pending_count=""
if [[ -f "${compliance_file}" ]]; then
    aap_status=$(/usr/bin/plutil -extract status raw -o - "${compliance_file}" 2> /dev/null)
    pending_count=$(/usr/bin/plutil -extract pendingCount raw -o - "${compliance_file}" 2> /dev/null)
fi
[[ "${pending_count}" == <-> ]] || pending_count=0

# Build the display string
if [[ ! -f "${compliance_file}" || "${aap_status}" == "Not checked" ]]; then
    display_text="${notchecked_text}"
elif [[ "${pending_count}" -eq 0 ]]; then
    display_text="${uptodate_text}"
elif [[ "${pending_count}" -eq 1 ]]; then
    display_text="1 Update Pending"
else
    display_text="${pending_count} Updates Pending"
fi

# Set output value
defaults write "${preference_file_location}" "${extension_id}" -string "${display_text}"

# Trigger the warning badge whenever there's at least one pending update
if [[ "${pending_count}" -gt 0 ]]; then
    defaults write "${preference_file_location}" "${extension_id}_alert" -bool true
else
    defaults write "${preference_file_location}" "${extension_id}_alert" -bool false
fi

# Stop spinning indicator
defaults write "${preference_file_location}" "${extension_id}_loading" -bool false
