#!/bin/bash
#
# my-fix-bookworm-padsp.sh — Persistent padsp patch for Debian bookworm ONLY
# Fixes the \$$LIB placeholder bug in /usr/bin/padsp
# Survives pulseaudio-utils updates via /usr/local/bin
#

set -e

# ============================================================
# 0. Bookworm guard — refuse to run on any other release
# ============================================================
echo "=== 0. Checking Debian release ==="

if [ ! -r /etc/os-release ]; then
    echo "ERROR: /etc/os-release not found. Cannot verify distribution."
    exit 1
fi

. /etc/os-release

if [ "${ID:-}" != "debian" ]; then
    echo "ERROR: This script only runs on Debian (detected ID='${ID:-unknown}')."
    exit 1
fi

if [ "${VERSION_CODENAME:-}" != "bookworm" ]; then
    echo "ERROR: This script only runs on Debian bookworm."
    echo "       Detected VERSION_CODENAME='${VERSION_CODENAME:-unknown}'."
    exit 1
fi

if [ -r /etc/debian_version ]; then
    DEBVER=$(cut -d. -f1 /etc/debian_version)
    if [ "$DEBVER" != "12" ]; then
        echo "ERROR: /etc/debian_version reports '$DEBVER', expected '12'."
        exit 1
    fi
fi

echo "OK: running on Debian bookworm (12)."

# ============================================================
# 1. Pre-flight checks
# ============================================================
echo
echo "=== 1. Pre-flight checks ==="

if [ ! -f /usr/bin/padsp ]; then
    echo "ERROR: /usr/bin/padsp not found. Is pulseaudio-utils installed?"
    exit 1
fi

# Detect the real multiarch path instead of hardcoding amd64
LIBPULSEDSP=""
for p in /usr/lib/*/pulseaudio/libpulsedsp.so \
         /usr/lib/pulseaudio/libpulsedsp.so ; do
    if [ -f "$p" ]; then
        LIBPULSEDSP="$p"
        break
    fi
done

if [ -z "$LIBPULSEDSP" ]; then
    echo "ERROR: libpulsedsp.so not found."
    echo "Search for it with: find /usr/lib -name libpulsedsp.so"
    exit 1
fi

echo "OK: /usr/bin/padsp present"
echo "OK: libpulsedsp.so present at $LIBPULSEDSP"

# ============================================================
# 2. Initial state — show the bug
# ============================================================
echo
echo "=== 2. Initial state ==="
grep "LD_PRELOAD=" /usr/bin/padsp || true

# ============================================================
# 3. Patch /usr/bin/padsp (using the detected path)
# ============================================================
echo
echo "=== 3. Patching /usr/bin/padsp ==="
sudo sed -i "s|/usr/\\\\\\\\\\\$\\\$LIB/pulseaudio/libpulsedsp.so|$LIBPULSEDSP|g" /usr/bin/padsp
grep "LD_PRELOAD=" /usr/bin/padsp

# ============================================================
# 4. Persistent copy in /usr/local/bin
# ============================================================
echo
echo "=== 4. Persistent copy in /usr/local/bin ==="
sudo cp /usr/bin/padsp /usr/local/bin/padsp
sudo chmod 755 /usr/local/bin/padsp
ls -l /usr/local/bin/padsp

# ============================================================
# 5. Check that root uses the right padsp
# ============================================================
echo
echo "=== 5. Checking root's PATH ==="
sudo which padsp

# ============================================================
# 6. Functional tests
# ============================================================
echo
echo "=== 6. Functional tests ==="
echo "--- user test ---"
padsp true && echo "OK user" || echo "FAIL user"

echo "--- root test ---"
sudo padsp true && echo "OK root" || echo "FAIL root"

# ============================================================
# 7. Survival test after package reinstall
# ============================================================
echo
echo "=== 7. Survival test (reinstalling pulseaudio-utils) ==="
sudo apt install --reinstall -y pulseaudio-utils >/dev/null 2>&1

echo "--- /usr/bin/padsp after reinstall ---"
grep "LD_PRELOAD=" /usr/bin/padsp || true

echo "--- /usr/local/bin/padsp after reinstall ---"
grep "LD_PRELOAD=" /usr/local/bin/padsp || true

echo "--- which padsp (root) ---"
sudo which padsp

echo "--- root test after reinstall ---"
sudo padsp true && echo "OK root after reinstall" || echo "FAIL root after reinstall"

# ============================================================
# 8. Final summary
# ============================================================
echo
echo "=== 8. Summary ==="
echo "File                     State"
echo "-----------------------  ------------------------------"
if grep -q 'LIB/pulseaudio/libpulsedsp.so' /usr/bin/padsp 2>/dev/null; then
    echo "/usr/bin/padsp           buggy (will be overwritten by apt upgrade)"
else
    echo "/usr/bin/padsp           patched"
fi
if grep -q "$LIBPULSEDSP" /usr/local/bin/padsp 2>/dev/null; then
    echo "/usr/local/bin/padsp     patched (persistent)"
else
    echo "/usr/local/bin/padsp     MISSING or buggy"
fi
echo
echo "Root will always use /usr/local/bin/padsp (higher priority in PATH)."
echo "The bug is permanently neutralized on Debian bookworm."
echo "adressed to:"
apt-cache show pulseaudio-utils | grep -E "^(Maintainer|Original-Maintainer|Homepage)"
apt moo
