#!/bin/bash

MODULES=("snd_soc_hobot_sound_duplex_host" "snd_soc_es8326" "aic8800_fdrv" "aic8800_bsp")
NUM_MODULES=${#MODULES[@]}

for MODULE in "${MODULES[@]}"; do
    if lsmod | grep -q "$MODULE"; then
        rmmod "$MODULE"
        echo "$(date): rmmod $MODULE" > /dev/kmsg
    fi
done

echo mem > /sys/power/state

sleep 1

systemctl restart lightdm

while true; do
    all_loaded=true
    for ((i=NUM_MODULES-1; i>=0; i--)); do
        MODULE="${MODULES[$i]}"
        if ! lsmod | grep -q "$MODULE"; then
            if modprobe "$MODULE"; then
                echo "$(date): Loaded $MODULE" > /dev/kmsg
            else
                echo "$(date): Failed to load $MODULE" > /dev/kmsg
                all_loaded=false
            fi
        fi
    done

    if $all_loaded; then
        echo "$(date): All modules loaded successfully. Exiting script." > /dev/kmsg
        exit 0
    fi
    
    sleep 2
done


