#!/bin/zsh
set -euo pipefail

TARGET_VOLUME="${3:-/}"
DRIVER_DESTINATION="${TARGET_VOLUME%/}/Library/Audio/Plug-Ins/HAL/MiRemoteV2ch.driver"
DRIVER_PLIST="$DRIVER_DESTINATION/Contents/Info.plist"
APP_DESTINATIONS=(
  "${TARGET_VOLUME%/}/Applications/SayAll.app"
  "${TARGET_VOLUME%/}/Applications/Remote Mic.app"
  "${TARGET_VOLUME%/}/Applications/无线麦.app"
)
APP_LABELS=("SayAll.app" "Remote Mic.app" "无线麦.app")
BUNDLE_IDENTIFIER="com.hd838a.RemoteMic"
DRIVER_BUNDLE_IDENTIFIER="com.hd838a.MiRemoteV2ch"
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"
HCI_SERVICE_STATE="${TARGET_VOLUME%/}/Library/Application Support/RemoteMic/AppleRemoteHCI"
UNINSTALL_TOKEN="$(/bin/date -u +%Y%m%dT%H%M%SZ)-$$"
ITEM_SOURCES=()
ITEM_LABELS=()
ITEM_KINDS=()
ITEM_DESTINATIONS=()
MOVED_COUNT=0
DRIVER_MOVED=0
HCI_SERVICE_OWNED=0

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
}

item_bundle_identifier() {
  /usr/bin/plutil -extract CFBundleIdentifier raw -o - "$1" 2>/dev/null || true
}

queue_owned_app() {
  local app_path="$1"
  local app_label="$2"
  local app_plist="$app_path/Contents/Info.plist"

  if [[ ! -e "$app_path" && ! -L "$app_path" ]]; then
    return
  fi
  if [[ ! -f "$app_plist" ]] || \
     [[ "$(item_bundle_identifier "$app_plist")" != "$BUNDLE_IDENTIFIER" ]]; then
    installer_message \
      "$app_label 不属于无线麦SayAll.app，卸载器已保留它不作处理。" \
      "$app_label does not belong to SayAll and was left untouched."
    return
  fi
  ITEM_SOURCES+=("$app_path")
  ITEM_LABELS+=("$app_label")
  ITEM_KINDS+=("app")
}

queue_owned_driver() {
  if [[ ! -e "$DRIVER_DESTINATION" && ! -L "$DRIVER_DESTINATION" ]]; then
    return
  fi
  if [[ ! -f "$DRIVER_PLIST" ]] || \
     [[ "$(item_bundle_identifier "$DRIVER_PLIST")" != "$DRIVER_BUNDLE_IDENTIFIER" ]]; then
    installer_message \
      "MiRemoteV2ch.driver 无法确认为无线麦兼容麦克风，卸载器已保留它不作处理。" \
      "MiRemoteV2ch.driver could not be verified as the SayAll compatibility microphone and was left untouched."
    return
  fi
  ITEM_SOURCES+=("$DRIVER_DESTINATION")
  ITEM_LABELS+=("MiRemoteV2ch.driver")
  ITEM_KINDS+=("driver")
}

queue_owned_hci_service() {
  if [[ -e "$HCI_SERVICE_DESTINATION" || -L "$HCI_SERVICE_DESTINATION" ]]; then
    if [[ ! -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" ]]; then
      installer_message \
        "Apple Remote 系统服务无法确认为无线麦组件，卸载器已保留它。" \
        "The Apple Remote system service could not be verified as a SayAll component and was left untouched."
    else
      HCI_SERVICE_OWNED=1
      ITEM_SOURCES+=("$HCI_SERVICE_DESTINATION")
      ITEM_LABELS+=("SayAllAppleRemoteHCIService.helper")
      ITEM_KINDS+=("hci_service")
    fi
  fi

  if [[ -e "$HCI_SERVICE_PLIST" || -L "$HCI_SERVICE_PLIST" ]]; then
    if [[ ! -f "$HCI_SERVICE_PLIST" ]] || \
       [[ "$(/usr/bin/plutil -extract Label raw -o - "$HCI_SERVICE_PLIST" 2>/dev/null || true)" != \
         "$HCI_SERVICE_LABEL" ]]; then
      installer_message \
        "Apple Remote 系统服务配置无法确认归属，卸载器已保留它。" \
        "The Apple Remote service configuration could not be verified and was left untouched."
    else
      ITEM_SOURCES+=("$HCI_SERVICE_PLIST")
      ITEM_LABELS+=("$HCI_SERVICE_LABEL.plist")
      ITEM_KINDS+=("hci_plist")
    fi
  fi

  if [[ "$HCI_SERVICE_OWNED" -eq 1 && -d "$HCI_SERVICE_STATE" && \
        ! -L "$HCI_SERVICE_STATE" ]]; then
    ITEM_SOURCES+=("$HCI_SERVICE_STATE")
    ITEM_LABELS+=("AppleRemoteHCI.state")
    ITEM_KINDS+=("hci_state")
  fi
}

prepare_trash_root() {
  local console_user console_uid user_home trash_created=0

  console_user="$(/usr/bin/stat -f '%Su' /dev/console)"
  if [[ "$console_user" != "root" && "$console_user" != "loginwindow" ]] && \
     console_uid="$(/usr/bin/id -u "$console_user" 2>/dev/null)"; then
    user_home="$(/usr/bin/dscl . -read "/Users/$console_user" NFSHomeDirectory \
      2>/dev/null | /usr/bin/sed -n 's/^NFSHomeDirectory: //p')"
  else
    console_uid=0
    user_home=""
  fi

  if [[ "$TARGET_VOLUME" == "/" && "$user_home" == /* && "$user_home" != "/" && \
        "$user_home" != *'/../'* && "$user_home" != *'/..' ]]; then
    TRASH_ROOT="${TARGET_VOLUME%/}$user_home/.Trash"
  else
    TRASH_ROOT="${TARGET_VOLUME%/}/.Trashes/$console_uid"
  fi
  if [[ "$TRASH_ROOT" != /* || "$TRASH_ROOT" == "/" || "$console_uid" != <-> ]]; then
    return 1
  fi
  if [[ ! -d "$TRASH_ROOT" ]]; then
    /bin/mkdir -p -- "$TRASH_ROOT" || return 1
    trash_created=1
  fi
  if [[ "$trash_created" -eq 1 ]]; then
    /usr/sbin/chown "$console_uid" "$TRASH_ROOT" || return 1
    /bin/chmod 700 "$TRASH_ROOT" || return 1
  fi
}

trash_destination_for_item() {
  local label="$1"
  local stem extension candidate counter=0

  case "$label" in
    *.app) stem="${label%.app}"; extension=".app" ;;
    *.driver) stem="${label%.driver}"; extension=".driver" ;;
    *.helper) stem="${label%.helper}"; extension=".helper" ;;
    *.plist) stem="${label%.plist}"; extension=".plist" ;;
    *.state) stem="${label%.state}"; extension=".state" ;;
    *) return 1 ;;
  esac
  candidate="$TRASH_ROOT/$stem (uninstalled $UNINSTALL_TOKEN)$extension"
  while [[ -e "$candidate" || -L "$candidate" ]]; do
    counter=$((counter + 1))
    candidate="$TRASH_ROOT/$stem (uninstalled $UNINSTALL_TOKEN-$counter)$extension"
  done
  print -r -- "$candidate"
}

rollback_moved_items() {
  local index
  for (( index=MOVED_COUNT; index>=1; index-- )); do
    if ! /bin/mv -n -- "${ITEM_DESTINATIONS[$index]}" "${ITEM_SOURCES[$index]}"; then
      installer_message \
        "无法自动恢复 ${ITEM_LABELS[$index]}；它仍保留在废纸篓 ${ITEM_DESTINATIONS[$index]} 中。" \
        "${ITEM_LABELS[$index]} could not be restored automatically and remains in Trash at ${ITEM_DESTINATIONS[$index]}." >&2
    fi
  done
}

for (( index=1; index<=${#APP_DESTINATIONS[@]}; index++ )); do
  queue_owned_app "${APP_DESTINATIONS[$index]}" "${APP_LABELS[$index]}"
done
queue_owned_driver
queue_owned_hci_service

if [[ "${#ITEM_SOURCES[@]}" -eq 0 ]]; then
  installer_message \
    "未发现可安全卸载的无线麦SayAll.app 或兼容麦克风。" \
    "No SayAll app or compatible microphone installation that could be safely uninstalled was found."
  exit 0
fi

if ! prepare_trash_root; then
  installer_message \
    "卸载已停止：无法安全使用废纸篓，现有 App 和兼容麦克风均已保留。" \
    "Uninstallation stopped because Trash could not be used safely. The existing app and compatibility microphone were left intact." >&2
  exit 2
fi

for label in "${ITEM_LABELS[@]}"; do
  if [[ "$label" == *.app && "$TARGET_VOLUME" == "/" ]]; then
    /usr/bin/pkill -x RemoteMic 2>/dev/null || true
    break
  fi
done

if [[ "$TARGET_VOLUME" == "/" ]]; then
  /bin/launchctl bootout "system/$HCI_SERVICE_LABEL" >/dev/null 2>&1 || true
  if [[ "$HCI_SERVICE_OWNED" -eq 1 ]]; then
    "$HCI_SERVICE_DESTINATION" --restore >/dev/null 2>&1 || true
  fi
fi

for (( index=1; index<=${#ITEM_SOURCES[@]}; index++ )); do
  ITEM_DESTINATIONS+=("$(trash_destination_for_item "${ITEM_LABELS[$index]}")")
  if /bin/mv -n -- "${ITEM_SOURCES[$index]}" "${ITEM_DESTINATIONS[$index]}" && \
     [[ ! -e "${ITEM_SOURCES[$index]}" && ! -L "${ITEM_SOURCES[$index]}" ]] && \
     [[ -e "${ITEM_DESTINATIONS[$index]}" || -L "${ITEM_DESTINATIONS[$index]}" ]]; then
    MOVED_COUNT="$index"
    [[ "${ITEM_KINDS[$index]}" == "driver" ]] && DRIVER_MOVED=1
  else
    installer_message \
      "卸载 ${ITEM_LABELS[$index]} 失败；正在恢复本次已移动的项目。" \
      "Failed to uninstall ${ITEM_LABELS[$index]}; restoring items moved during this run." >&2
    rollback_moved_items
    exit 2
  fi
done

# The recognized items are already in Trash, so a failed audio-service restart
# must not turn a successful uninstall into an installer error, and a real
# failure must be reported honestly instead of swallowed by `|| true`.
restart_audio_service() {
  if ! /usr/bin/pgrep -qx coreaudiod; then
    installer_message \
      "系统音频服务当前未运行，无需重启；驱动移除即刻生效。" \
      "The system audio service is not running; no restart was needed and the driver removal takes effect immediately."
  elif /usr/bin/killall coreaudiod; then
    installer_message \
      "已重启系统音频服务。" \
      "Restarted the system audio service."
  else
    installer_message \
      "驱动已移除，但重启系统音频服务失败；请重启 Mac 完成卸载。" \
      "The driver was removed, but the system audio service could not be restarted; restart your Mac to finish."
  fi
}

if [[ "$DRIVER_MOVED" -eq 1 && "$TARGET_VOLUME" == "/" ]]; then
  restart_audio_service
fi
installer_message \
  "已将无线麦SayAll.app 的已识别安装项移到废纸篓，需要时可恢复；BlackHole 和本地设置未被修改。" \
  "Recognized SayAll installation items were moved to Trash and can be restored. BlackHole and local settings were not changed."
