#!/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
#
# install-cs8409-driver.sh — Linux-on-Mac baseline: built-in speakers.
#
# Brings a used Intel Mac running Ubuntu up to the Linux-on-Mac audio baseline.
# It checks that this is Apple Mac hardware with the Cirrus CS8409 chip, installs
# the build tools and Ubuntu's kernel source, fetches the third-party speaker
# driver at the commit Learn Linux for Work reviewed, keeps it in a root-owned
# folder, and has DKMS build and install it. Run with sudo, then reboot.
# Re-run it after a kernel update if the speakers go silent again.
#
# Original work of Shea's Tech, LLC. It contains no code from the driver
# project: it downloads that project and runs its own installer. Credits are
# printed at the end.

set -euo pipefail

DRIVER_URL="https://github.com/davidjo/snd_hda_macbookpro.git"
REVIEWED_COMMIT="1931615"
DEST="/opt/snd_hda_macbookpro"
HERE=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)

die() { echo "error: $*" >&2; exit 1; }
step() { echo; echo "==> $*"; }

credits() {
  cat <<'CREDITS'

Credits
  Speaker driver: snd_hda_macbookpro by davidjo and contributors, GPL-2.0.
  https://github.com/davidjo/snd_hda_macbookpro
  This installer 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
}

[ "$(id -u)" -eq 0 ] || die "run this with sudo: sudo $0"

step "Checking this machine against the Linux-on-Mac audio baseline"
[ "$(cat /sys/class/dmi/id/sys_vendor 2>/dev/null)" = "Apple Inc." ] \
  || die "this is not Apple Mac hardware: Linux-on-Mac covers Apple Macs only"
grep -qs 'Cirrus Logic CS8409' /proc/asound/card*/codec#* \
  || die "no Cirrus Logic CS8409 audio chip found: this fix does not apply to this Mac"
# shellcheck disable=SC1091
. /etc/os-release
case "${ID:-} ${ID_LIKE:-}" in
  *ubuntu*) ;;
  *) die "this installer supports Ubuntu (and Ubuntu-based distros) only" ;;
esac
echo "Apple $(cat /sys/class/dmi/id/product_name) with Cirrus CS8409 on ${PRETTY_NAME:-Ubuntu}: OK"

kver_full=$(uname -r)
kver=${kver_full%%-*}

step "Installing build tools, kernel headers and Ubuntu's kernel source ($kver)"
apt-get update
apt-get install -y build-essential git dkms "linux-headers-$kver_full" \
  || die "could not install the build tools or kernel headers"
apt-get install -y "linux-source-$kver" \
  || die "no linux-source-$kver package: HWE and custom kernels aren't supported"

step "Fetching the speaker driver at reviewed commit $REVIEWED_COMMIT into $DEST"
if [ -d "$DEST/.git" ]; then
  git -C "$DEST" fetch --quiet origin
else
  git clone --quiet "$DRIVER_URL" "$DEST"
fi
git -C "$DEST" -c advice.detachedHead=false checkout --quiet "$REVIEWED_COMMIT"
chown -R root:root "$DEST"
chmod -R go-w "$DEST"

step "Building and installing with DKMS (the first step can take several minutes)"
( cd "$DEST" && ./install.cirrus.driver.sh -i )

echo
echo "Done. Reboot to load the driver:  sudo reboot"
echo "Then validate the baseline:       $HERE/validate-mac-audio.sh"
credits
