#!/bin/bash

set -e
# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

case "$1" in
    configure)
        if [ ! -d /boot/grub ]; then
            mkdir -p /boot/grub
        fi
        update-grub
        if id -u gdm >/dev/null 2>&1; then
            # Grant Xorg permissions
            usermod -a -G video gdm >/dev/null 2>&1
        else
            echo "the user doesn't exist: gdm"
        fi
        if ischroot; then
            echo "chroot detected; skipping postinst"
            exit 0
        fi
        # WA: It's for jammy only. Please remove it when uploading the package to noble
        if [ -f /proc/device-tree/mt8195-sound/compatible ]; then
            BOARD=$(tr -d '\0' < /proc/device-tree/mt8195-sound/compatible)
            if [[ $BOARD == *"mt8395-evk"* ]]; then
                alsactl init
                echo "Rebooting is required to apply configuration changes to fix an audio issue"
                if [ -d /var/run ]; then
                    echo "*** System restart required ***" > /var/run/reboot-required
                    if ! grep -q "^$DPKG_MAINTSCRIPT_PACKAGE$" /var/run/reboot-required.pkgs 2> /dev/null ; then
                        echo "$DPKG_MAINTSCRIPT_PACKAGE" >> /var/run/reboot-required.pkgs
                    fi
                fi
            fi
        fi
    ;;
esac

exit 0
