#!/bin/bash

# ==============================================================================
# Operator Audio Status Checker & Auto-Fix Script (Amlogic S905X3)
# ==============================================================================

if [ "$EUID" -ne 0 ]; then
  echo "[-] Error: Please run this script with root/sudo privileges!"
  echo "    Usage: sudo bash $0"
  exit 1
fi

echo "===================================================="
echo "          AUDIO POWER MANAGEMENT CHECK              "
echo "===================================================="

# Step 1: Check if -1 parameter is active in sysfs
CURRENT_PMDOWN="UNKNOWN"
if [ -f /sys/module/snd_soc_core/parameters/pmdown_time ]; then
    CURRENT_PMDOWN=$(cat /sys/module/snd_soc_core/parameters/pmdown_time)
fi

# Step 2: Check if systemd service is active
SERVICE_ACTIVE=$(systemctl is-active amlogic-audio-keepalive.service 2>/dev/null)

echo "[i] Current Kernel ASoC pmdown_time : $CURRENT_PMDOWN"
echo "[i] Keep-Alive Service Status       : ${SERVICE_ACTIVE:-not-installed}"
echo "----------------------------------------------------"

# Step 3: Decision Logic
if [ "$CURRENT_PMDOWN" = "-1" ] && [ "$SERVICE_ACTIVE" = "active" ]; then
    echo "[OK] CONFIGURATION IS ALREADY APPLIED (-1 active)."
    echo "[OK] No action required. This system is locked against audio drops."
    echo "===================================================="
    exit 0
else
    echo "[!] FIX REQUIRED: System is NOT fully configured!"
    if [ "$CURRENT_PMDOWN" != "-1" ]; then
        echo "    -> Reason: pmdown_time is set to '$CURRENT_PMDOWN' (Expected: -1)"
    fi
    if [ "$SERVICE_ACTIVE" != "active" ]; then
        echo "    -> Reason: Keep-alive service is '${SERVICE_ACTIVE:-not installed}'"
    fi
    echo "----------------------------------------------------"
    
    # Auto-apply the fix
    echo "[+] Applying fix configuration now..."

    # 1. Write Modprobe Rule
    cat << 'EOF' > /etc/modprobe.d/amlogic_audio_fix.conf
# Global ASoC Power-Down Override (-1 = Disabled / Never suspend routes)
options snd_soc_core pmdown_time=-1
EOF

    # 2. Live Kernel Parameter Injection
    if [ -f /sys/module/snd_soc_core/parameters/pmdown_time ]; then
        echo -1 > /sys/module/snd_soc_core/parameters/pmdown_time
    fi

    # 3. Create & Start Systemd Service
    cat << 'EOF' > /etc/systemd/system/amlogic-audio-keepalive.service
[Unit]
Description=Amlogic S905X3 AXG Audio Pipeline Keep-Alive
After=sound.target

[Service]
Type=simple
# Amlogic ACODEC ana kanalını Unmute eder
ExecStartPre=-/usr/bin/amixer -c 0 sset "ACODEC" unmute
ExecStart=/usr/bin/aplay -D plughw:0,0 -f S16_LE -c 2 -r 48000 /dev/zero
Restart=always
RestartSec=2

[Install]
WantedBy=multi-user.target
EOF

    systemctl daemon-reload >/dev/null 2>&1
    systemctl enable --now amlogic-audio-keepalive.service >/dev/null 2>&1

    echo "----------------------------------------------------"
    echo "[SUCCESS] Fix applied successfully!"
    echo "[SUCCESS] pmdown_time is now -1 and keep-alive service is active."
    echo "===================================================="
fi
