#!/bin/sh
set -eu

ROOT="$(cd "$(dirname "$0")" && pwd)"
DEST="/Library/Audio/Plug-Ins/HAL/SiriPlusRemoteMic.driver"

run_as_root() {
  if [ "$(id -u)" -eq 0 ]; then
    "$@"
  else
    sudo "$@"
  fi
}

coreaudiod_pid() {
  /usr/bin/pgrep -x coreaudiod 2>/dev/null | /usr/bin/head -n 1
}

wait_for_coreaudiod_restart() {
  for _ in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do
    NEW_PID="$(coreaudiod_pid || true)"
    if [ -n "$NEW_PID" ] && [ "$NEW_PID" != "$OLD_PID" ]; then
      echo "Restarted CoreAudio (coreaudiod ${OLD_PID:-not running} -> $NEW_PID)."
      return 0
    fi
    /bin/sleep 0.25
  done
  return 1
}

restart_coreaudiod() {
  OLD_PID="$(coreaudiod_pid || true)"

  run_as_root /bin/launchctl kickstart -k system/com.apple.audio.coreaudiod 2>/dev/null || true
  if wait_for_coreaudiod_restart; then
    return 0
  fi

  if [ -n "$OLD_PID" ]; then
    run_as_root /bin/kill -TERM "$OLD_PID" 2>/dev/null || true
  fi
  run_as_root /usr/bin/killall -TERM coreaudiod 2>/dev/null || true
  if wait_for_coreaudiod_restart; then
    return 0
  fi

  if [ -n "$OLD_PID" ]; then
    run_as_root /bin/kill -KILL "$OLD_PID" 2>/dev/null || true
  fi
  run_as_root /usr/bin/killall -KILL coreaudiod 2>/dev/null || true
  run_as_root /bin/launchctl kickstart -k system/com.apple.audio.coreaudiod 2>/dev/null || true
  if wait_for_coreaudiod_restart; then
    return 0
  fi

  echo "Installed driver, but CoreAudio did not restart; coreaudiod PID is still ${OLD_PID:-unknown}." >&2
  return 1
}

PREBUILT_DRIVER="${1:-}"
if [ -n "$PREBUILT_DRIVER" ]; then
  BUILT_DRIVER="$PREBUILT_DRIVER"
else
  if [ "$(id -u)" -eq 0 ]; then
    TMP_ROOT="/tmp"
  else
    TMP_ROOT="${TMPDIR:-/tmp}"
  fi
  BUILD_DIR="$(/usr/bin/mktemp -d "${TMP_ROOT%/}/SiriPlusRemoteMicDriver-build.XXXXXX")"
  trap '/bin/rm -rf "$BUILD_DIR"' EXIT HUP INT TERM

  SIRIPLUS_REMOTE_MIC_BUILD_DIR="$BUILD_DIR" sh "$ROOT/build.sh"
  BUILT_DRIVER="$BUILD_DIR/SiriPlusRemoteMic.driver"
fi

BUILT_INFO="$BUILT_DRIVER/Contents/Info.plist"
BUILT_SOURCE_HASH="$(/usr/libexec/PlistBuddy -c "Print :SiriPlusDriverSourceSHA256" "$BUILT_INFO")"

run_as_root rm -rf "$DEST"
run_as_root cp -R "$BUILT_DRIVER" "$DEST"
run_as_root chown -R root:wheel "$DEST"
run_as_root chmod -R go-w "$DEST"
run_as_root xattr -dr com.apple.quarantine "$DEST" 2>/dev/null || true

INSTALLED_INFO="$DEST/Contents/Info.plist"
INSTALLED_SOURCE_HASH="$(/usr/libexec/PlistBuddy -c "Print :SiriPlusDriverSourceSHA256" "$INSTALLED_INFO")"
INSTALLED_BINARY_HASH="$(/usr/bin/shasum -a 256 "$DEST/Contents/MacOS/SiriPlusRemoteMic" | /usr/bin/awk '{print $1}')"
INSTALLED_IDENTITY_VERSION="$(/usr/libexec/PlistBuddy -c "Print :SiriPlusDriverIdentityVersion" "$INSTALLED_INFO")"

if [ "$INSTALLED_SOURCE_HASH" != "$BUILT_SOURCE_HASH" ]; then
  echo "Installed driver identity mismatch: expected $BUILT_SOURCE_HASH, found $INSTALLED_SOURCE_HASH" >&2
  exit 1
fi

if [ "$INSTALLED_IDENTITY_VERSION" != "content-v3" ]; then
  echo "Installed driver identity version mismatch: expected content-v3, found $INSTALLED_IDENTITY_VERSION" >&2
  exit 1
fi

if ! codesign --verify "$DEST" >/dev/null 2>&1; then
  echo "Installed driver code signature verification failed" >&2
  exit 1
fi

restart_coreaudiod

echo "Installed $DEST"
echo "Verified SiriPlus Remote Mic driver identity $INSTALLED_SOURCE_HASH"
echo "Installed signed binary hash $INSTALLED_BINARY_HASH"
echo "Sound Settings can reload SiriPlus Remote Mic."
