#!/bin/bash

set -e


case "$1" in
    configure|upgrade|update|reconfigure)
        # Add live menus in grub when needed
        FLE='/etc/grub.d/10_linux'
        SH='/usr/lib/solydxk/scripts/boot-isos.sh'
        if [ -e "$FLE" ] && [ -e "$SH" ]; then
            if ! grep -q "$SH" "$FLE"; then
                sed -i "s#echo '}'#if [ -e "$SH" ]; then bash "$SH"; fi; echo '}'#" "$FLE"
            else
                # Fix bad format
                sed -i "s/fi; echo '}}'/fi; echo '}'/" "$FLE"
            fi
        fi

        # Fix Grub
        GRUB='/etc/default/grub'
        USR_THEMES='/usr/share/grub/themes/solyd'
        if [ -e "$GRUB" ] && [ -d /boot/grub/themes/ ]; then
            if grep -q "$USR_THEMES" /etc/default/grub; then
                sed -i "s|$USR_THEMES|/boot/grub/themes/solyd|" "$GRUB"
                update-grub
            fi
        fi

        # Adjust settings
        python3 /usr/lib/solydxk/system/adjust.py

        # Enable lightdm-user-permission.service only if lightdm is installed
        if [ -x "$(which lightdm)" ]; then
            if [ -x /bin/systemctl ]; then
                systemctl --no-reload enable lightdm-user-permission.service >/dev/null 2>&1 || true
            fi
        else
            # Disable the service if lightdm is not present
            if [ -x /bin/systemctl ]; then
                systemctl --no-reload disable lightdm-user-permission.service >/dev/null 2>&1 || true
            fi
        fi
        
        DT="/usr/share/applications/solydxk_forums.desktop"
        if [ -e "$DT" ]; then
            rm "$DT"
        fi

        # Update the apt cache to refresh appstream icons
        apt-get update -qq

        ;;
        
    abort-upgrade|abort-remove|abort-deconfigure)
        ;;
        
    triggered)
        python3 /usr/lib/solydxk/system/adjust.py
        ;;
        
    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

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

#DEBHELPER#

exit 0
