#!/usr/bin/env bash
# @file Ensure Auto-Update
# @brief Configures macOS to automatically check for system updates
# @description
#     This script adds a macOS LaunchDaemon for the root user that checks for updates and automatically
#     applies them. It will run if the file is available at `$HOME/Library/LaunchDaemons/com.apple.automatedupdates.plist`.

{{ includeTemplate "universal/profile" }}
{{ includeTemplate "universal/logg" }}

### Enable automated updates on macOS
if [ -d /Applications ] && [ -d /System ]; then
    if [ -f "$HOME/Library/LaunchDaemons/com.apple.automatedupdates.plist" ] && [ ! -f "/Library/LaunchDaemons/com.apple.automatedupdates.plist" ]; then
        logg info 'Configuring macOS to automatically apply system updates'
        sudo mkdir -p /Library/LaunchDaemons
        sudo cp -f "$HOME/Library/LaunchDaemons/com.apple.automatedupdates.plist" "/Library/LaunchDaemons/com.apple.automatedupdates.plist"
        logg info 'Loading /Library/LaunchDaemons/com.apple.automatedupdates.plist'
        sudo launchctl load "/Library/LaunchDaemons/com.apple.automatedupdates.plist" && logg success 'launchctl load successful'
    fi
fi
