#!/usr/bin/env bash
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (C) 2026 Shea's Tech, LLC — Learn Linux for Work
# https://mac.learnlinuxforwork.com
#
# validate-mac-audio.sh — Linux-on-Mac baseline: built-in speakers.
#
# Validates a used Intel Mac running Linux against the Linux-on-Mac audio
# baseline: genuine Apple hardware, a supported model with the Cirrus CS8409
# audio chip, an OS the install script supports, and (after install) the Apple
# speaker driver active. Read-only: changes nothing. No root needed (sudo shows
# the kernel-log check too).
#
# Prints PASS:/FAIL:/INFO: lines. Exit code: 0 = baseline met, 1 = a check failed,
# 2 = this machine is outside the baseline (not an Apple Mac / not a CS8409 Mac).
# Original work of Shea's Tech, LLC. Credits are printed at the end.

set -u

fail=0
pass() { echo "PASS: $*"; }
failc() { echo "FAIL: $*"; fail=1; }
info() { echo "INFO: $*"; }

dmi() { cat "/sys/class/dmi/id/$1" 2>/dev/null || echo unknown; }

echo "Linux-on-Mac baseline: built-in speakers"
echo "----------------------------------------"

# 1. Apple Mac hardware only.
vendor=$(dmi sys_vendor)
model=$(dmi product_name)
if [ "$vendor" != "Apple Inc." ]; then
  echo "FAIL: hardware vendor is '$vendor', not Apple Inc."
  echo "This baseline is for Apple Mac hardware only."
  exit 2
fi
pass "Apple Mac hardware ($model)"

# 2. A Mac model in the baseline (Intel Macs with the CS8409 audio chip).
case "$model" in
  iMac18,1|iMac18,2|iMac18,3|iMac19,1|MacBookPro13,1|MacBookPro13,2|MacBookPro13,3|MacBookPro14,1|MacBookPro14,2|MacBookPro14,3)
    pass "model $model is in the Linux-on-Mac audio baseline" ;;
  *)
    failc "model $model is not in the audio baseline (iMac 18,x / 19,1, MacBook Pro 13,x / 14,x)" ;;
esac

# 3. The CS8409 chip, with an Apple subsystem ID this fix covers.
codec_file=""
for f in /proc/asound/card*/codec#*; do
  [ -r "$f" ] && grep -q 'Cirrus Logic CS8409' "$f" && { codec_file=$f; break; }
done
if [ -z "$codec_file" ]; then
  echo "FAIL: no Cirrus Logic CS8409 audio chip found"
  echo "This Mac's speakers are not covered by this guide."
  exit 2
fi
subsys=$(awk '/^Subsystem Id:/{print $3; exit}' "$codec_file")
case "$subsys" in
  0x106b0e00|0x106b0f00|0x106b1000|0x106b3300|0x106b3600|0x106b3900)
    pass "Cirrus Logic CS8409 with Apple subsystem ID $subsys" ;;
  *)
    failc "CS8409 subsystem ID $subsys is not one this fix covers" ;;
esac

# 4. An OS the install script supports, with a matching kernel source package.
# shellcheck disable=SC1091
. /etc/os-release 2>/dev/null || true
case "${ID:-} ${ID_LIKE:-}" in
  *ubuntu*) pass "OS is ${PRETTY_NAME:-Ubuntu} (supported by install-cs8409-driver.sh)" ;;
  *)        failc "OS is ${PRETTY_NAME:-unknown}; install-cs8409-driver.sh supports Ubuntu only" ;;
esac
kver_full=$(uname -r); kver=${kver_full%%-*}
if command -v apt-cache >/dev/null 2>&1; then
  cand=$(apt-cache policy "linux-source-$kver" 2>/dev/null | awk '/Candidate:/{print $2}')
  if [ -n "$cand" ] && [ "$cand" != "(none)" ]; then
    pass "kernel $kver_full has a matching linux-source-$kver package"
  else
    failc "no linux-source-$kver package for kernel $kver_full (HWE/custom kernels aren't supported)"
  fi
fi

# 5. Driver state: which module loads, and is its Apple setup active?
mod_path=$(modinfo -n snd_hda_codec_cs8409 2>/dev/null || echo "")
case "$mod_path" in
  */updates/dkms/*)
    pass "Apple speaker driver is installed ($mod_path)"
    if journalctl -k -b --no-pager 2>/dev/null | grep -q 'trying APPLE'; then
      pass "Apple speaker setup is active this boot"
    elif journalctl -k -b --no-pager >/dev/null 2>&1; then
      failc "driver installed but its Apple setup isn't active this boot (reboot after installing)"
    else
      info "run with sudo to check the kernel log for the Apple setup"
    fi ;;
  */kernel/sound/*)
    info "stock kernel driver in use: built-in speakers will be silent until you run install-cs8409-driver.sh" ;;
  *)
    failc "snd_hda_codec_cs8409 module not found" ;;
esac

# 6. Output not muted.
if command -v wpctl >/dev/null 2>&1; then
  vol=$(wpctl get-volume @DEFAULT_AUDIO_SINK@ 2>/dev/null || true)
  case "$vol" in
    *MUTED*) failc "default output is muted ($vol)" ;;
    "")      info "no default audio output found" ;;
    *)       pass "default output is not muted (${vol#Volume: })" ;;
  esac
fi

echo "----------------------------------------"
if [ "$fail" -eq 0 ]; then echo "RESULT: baseline met"; else echo "RESULT: baseline not met (see FAIL lines)"; fi

cat <<'CREDITS'

Credits
  Speaker driver: snd_hda_macbookpro by davidjo and contributors,
  GPL-2.0. https://github.com/davidjo/snd_hda_macbookpro
  This script is original Learn Linux for Work work; it contains no code from
  that project. Apple, iMac and MacBook Pro are trademarks of Apple Inc.
  Cirrus Logic is a trademark of Cirrus Logic, Inc.
CREDITS

exit "$fail"
