#!/bin/zsh
set -euo pipefail

TARGET_VOLUME="${3:-/}"
SCRIPT_DIR="${0:A:h}"
RELEASE_CONFIG="$SCRIPT_DIR/release-variant.plist"
HCI_SERVICE_LABEL="com.hd838a.SayAll.AppleRemoteHCIService"
HCI_SERVICE_DESTINATION="${TARGET_VOLUME%/}/Library/PrivilegedHelperTools/$HCI_SERVICE_LABEL"
HCI_SERVICE_PLIST="${TARGET_VOLUME%/}/Library/LaunchDaemons/$HCI_SERVICE_LABEL.plist"

uses_chinese_locale() {
  local locale="${LC_ALL:-${LC_MESSAGES:-${LANG:-}}}"
  [[ "$locale" == zh* ]]
}

installer_message() {
  if uses_chinese_locale; then
    print "$1"
  else
    print "$2"
  fi
}

hci_service_is_owned() {
  [[ -x "$HCI_SERVICE_DESTINATION" ]] &&
    /usr/bin/codesign --verify --strict "$HCI_SERVICE_DESTINATION" >/dev/null 2>&1 &&
    [[ "$(/usr/bin/codesign -dvv "$HCI_SERVICE_DESTINATION" 2>&1 | \
      /usr/bin/sed -n 's/^Identifier=//p')" == "$HCI_SERVICE_LABEL" ]]
}

hci_service_plist_is_owned() {
  [[ -f "$HCI_SERVICE_PLIST" ]] &&
    [[ "$(/usr/bin/plutil -extract Label raw -o - "$HCI_SERVICE_PLIST" 2>/dev/null || true)" == \
      "$HCI_SERVICE_LABEL" ]] &&
    [[ "$(/usr/libexec/PlistBuddy -c "Print :ProgramArguments:0" \
      "$HCI_SERVICE_PLIST" 2>/dev/null || true)" == \
      "$HCI_SERVICE_DESTINATION" ]]
}

hardware_architecture() {
  if [[ "$(/usr/sbin/sysctl -in hw.optional.arm64 2>/dev/null || print 0)" == "1" ]]; then
    print "arm64"
  else
    print "x86_64"
  fi
}

test -f "$RELEASE_CONFIG"
EXPECTED_ARCHITECTURE="$(/usr/bin/plutil -extract ExpectedArchitecture raw -o - "$RELEASE_CONFIG")"
MINIMUM_SYSTEM_MAJOR="$(/usr/bin/plutil -extract MinimumSystemMajor raw -o - "$RELEASE_CONFIG")"
CURRENT_ARCHITECTURE="$(hardware_architecture)"
CURRENT_SYSTEM_MAJOR="$(/usr/bin/sw_vers -productVersion | /usr/bin/cut -d. -f1)"

if [[ "$CURRENT_ARCHITECTURE" != "$EXPECTED_ARCHITECTURE" ]]; then
  if [[ "$EXPECTED_ARCHITECTURE" == "arm64" ]]; then
    installer_message \
      "未安装 Siri Remote 支持：此安装包仅适用于 Apple 芯片 Mac。" \
      "Siri Remote support was not installed: this package is for Apple silicon Macs." >&2
  else
    installer_message \
      "未安装 Siri Remote 支持：此安装包仅适用于 Intel Mac。" \
      "Siri Remote support was not installed: this package is for Intel Macs." >&2
  fi
  exit 2
fi
if [[ "$CURRENT_SYSTEM_MAJOR" -lt "$MINIMUM_SYSTEM_MAJOR" ]]; then
  installer_message \
    "未安装 Siri Remote 支持：此安装包需要 macOS $MINIMUM_SYSTEM_MAJOR 或更高版本。" \
    "Siri Remote support was not installed: this package requires macOS $MINIMUM_SYSTEM_MAJOR or later." >&2
  exit 2
fi

if [[ "$TARGET_VOLUME" == "/" ]]; then
  if [[ -e "$HCI_SERVICE_DESTINATION" || -L "$HCI_SERVICE_DESTINATION" ]] && \
     ! hci_service_is_owned; then
    installer_message \
      "未安装 Siri Remote 支持：PrivilegedHelperTools 中已有无法确认归属的服务。" \
      "Siri Remote support was not installed: an unrecognized helper already exists." >&2
    exit 2
  fi
  if [[ -e "$HCI_SERVICE_PLIST" || -L "$HCI_SERVICE_PLIST" ]] && \
     ! hci_service_plist_is_owned; then
    installer_message \
      "未安装 Siri Remote 支持：LaunchDaemons 中已有无法确认归属的配置。" \
      "Siri Remote support was not installed: an unrecognized LaunchDaemon already exists." >&2
    exit 2
  fi
  /bin/launchctl bootout "system/$HCI_SERVICE_LABEL" >/dev/null 2>&1 || true
  if hci_service_is_owned; then
    "$HCI_SERVICE_DESTINATION" --restore >/dev/null 2>&1 || true
  fi
fi
