Description: Download the latest Homebrew package version from GitHub and import into Munki.
Identifier: com.github.nstrauss.munki.Homebrew
MinimumVersion: "2.3"
ParentRecipe: com.github.nstrauss.download.Homebrew

Input:
  NAME: Homebrew
  MUNKI_REPO_SUBDIR: apps/Homebrew
  pkginfo:
    catalogs:
      - testing
    description: The missing package manager for macOS.
    developer: Homebrew
    display_name: "%NAME%"
    name: "%NAME%"
    unattended_install: true
    unattended_uninstall: true
    uninstall_method: uninstall_script
    uninstall_script: |
      #!/bin/bash

      NONINTERACTIVE=1 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"

Process:
  - Processor: MunkiPkginfoMerger
    Arguments:
      additional_pkginfo:
        preinstall_script: |
          #!/bin/zsh

          subdirs=(
              ".git"
              "Library/Homebrew"
          )

          if [[ $(uname -m) == "x86_64" ]]; then
              brew_dir="/usr/local/Homebrew"
          else
              brew_dir="/opt/homebrew"
          fi

          for subdir in $subdirs; do
              if [[ -d "$brew_dir/$subdir" ]]; then
                  /bin/echo "Deleting $brew_dir/$subdir for clean upgrade..."
                  /bin/rm -rf $brew_dir/$subdir
              fi
          done

        installcheck_script: |
          #!/bin/zsh

          autoload is-at-least

          installed=1
          not_installed=0
          target_version="%version%"

          # optionally define an install user at /var/tmp/.homebrew_pkg_user.plist
          homebrew_pkg_user_plist="/var/tmp/.homebrew_pkg_user.plist"
          if [[ -f "${homebrew_pkg_user_plist}" ]] && [[ -n $(defaults read "${homebrew_pkg_user_plist}" HOMEBREW_PKG_USER) ]]; then
            homebrew_pkg_user=$(defaults read /var/tmp/.homebrew_pkg_user HOMEBREW_PKG_USER)
          # otherwise, get valid logged in user
          else
            homebrew_pkg_user=$(echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }')
          fi

          non_users=(_mbsetupuser loginwindow root)
          if [[ ${non_users[(r)$homebrew_pkg_user]} == "$homebrew_pkg_user" ]]; then
            /bin/echo "No valid Homebrew package user available or at the login window. Considering \"installed\" for now."
            exit "$installed"
          fi

          if [[ $(uname -m) == "x86_64" ]]; then
            brew="/usr/local/Homebrew/bin/brew"
          else
            brew="/opt/homebrew/bin/brew"
          fi

          if [[ ! -f "$brew" ]]; then
            /bin/echo "$brew doesn't exist. Not installed."
            exit "$not_installed"
          fi

          current_version=$(/usr/bin/su -l $homebrew_pkg_user -c "$brew --version" | /usr/bin/head -n 1 | /usr/bin/awk '{ print $2 }')
          if is-at-least "$target_version" "$current_version"; then
            /bin/echo "$current_version is greater than or equal to $target_version. Already installed."
            exit "$installed"
          else
            /bin/echo "$current_version isn't at least $target_version. Not installed."
            exit "$not_installed"
          fi

  - Processor: MunkiImporter
    Arguments:
      pkg_path: "%pathname%"
      repo_subdirectory: "%MUNKI_REPO_SUBDIR%"
