#!/bin/sh -e

if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-remove" ] ; then

    #test for live system, exit if found  (check used below)
    LIVE_CHECK=$(df -T / |tail -n1 |awk '{print $2}')
    
    # fix grub menu label
    if [ -e /etc/grub.d/10_linux ]; then
        sed -i s/OS\=\"\${GRUB_DISTRIBUTOR}\ GNU\\/Linux\"/OS\=\"\${GRUB_DISTRIBUTOR}\"/ /etc/grub.d/10_linux
    fi
    if [ -e /etc/default/grub ]; then
        sed -i s/lsb_release\ -i\ -s/lsb_release\ -d\ -s/ /etc/default/grub
        sed -i  -re 's%^(GRUB_DISTRIBUTOR=).*%\1"$(unset PRETTY_NAME; (. /etc/lsb-release; echo ${PRETTY_NAME:?}) 2>/dev/null || echo Debian)"%' /etc/default/grub
    fi
    
    if [ -e /usr/local/share/live-files/general-files/etc/default/grub ]; then
        sed -i  -re 's%^(GRUB_DISTRIBUTOR=).*%\1"$(unset PRETTY_NAME; (. /etc/lsb-release; echo ${PRETTY_NAME:?}) 2>/dev/null || echo Debian)"%' /usr/local/share/live-files/general-files/etc/default/grub
    fi
    
    UPDATE_GRUB_FLAG=false
    #update 20_memtest86+ file
    if [ -e /usr/local/share/live-files/files/etc/grub.d/20_memtest86+ ]; then
        if ! grep -sq 'mx-system 20.11.02+4' /usr/local/share/live-files/files/etc/grub.d/20_memtest86+; then
            cp /usr/share/mx-system/grub.d/20_memtest86+ /usr/local/share/live-files/files/etc/grub.d/20_memtest86+
        fi
    fi
    if [ -e /etc/grub.d/20_memtest86+ ]; then
        if ! grep -sq 'mx-system 20.11.02+4' /etc/grub.d/20_memtest86+; then
            cp /usr/share/mx-system/grub.d/20_memtest86+ /etc/grub.d/20_memtest86+
            UPDATE_GRUB_FLAG=true
        fi
    fi
    
    #update 30_uefi-firmware file
    if [ -e /usr/local/share/live-files/files/etc/grub.d/30_uefi-firmware ]; then
        if ! grep -sq 'mx-system 20.11.02+4' /usr/local/share/live-files/files/etc/grub.d/30_uefi-firmware; then
            cp /usr/share/mx-system/grub.d/30_uefi-firmware /usr/local/share/live-files/files/etc/grub.d/30_uefi-firmware
        fi
    fi
    if [ -e /etc/grub.d/30_uefi-firmware ]; then
        if ! grep -sq 'mx-system 20.11.02+4' /etc/grub.d/30_uefi-firmware; then
            cp /usr/share/mx-system/grub.d/30_uefi-firmware /etc/grub.d/30_uefi-firmware
            UPDATE_GRUB_FLAG=true
        fi
    fi
    
    #update 30_os-prober file
    if [ -e /usr/local/share/live-files/files/etc/grub.d/30_os-prober ]; then
        if ! grep -sq 'mx-system 20.11.02+4' /usr/local/share/live-files/files/etc/grub.d/30_os-prober; then
            cp /usr/share/mx-system/grub.d/30_os-prober /usr/local/share/live-files/files/etc/grub.d/30_os-prober
        fi
    fi
    if [ -e /etc/grub.d/30_os-prober ]; then
        if ! grep -sq 'mx-system 20.11.02+4' /etc/grub.d/30_os-prober; then
            cp /usr/share/mx-system/grub.d/30_os-prober /etc/grub.d/30_os-prober
            UPDATE_GRUB_FLAG=true
        fi
    fi

    #add a symlink to mx-version called antix-version, because some apps look for info from antix-version
    if [ -e /etc/mx-version ]; then
        if [ ! -e /etc/antix-version ]; then
        ln -frs /etc/mx-version /etc/antix-version
        fi
        if [ "$(readlink /etc/antix-version)" = "/etc/mx-version" ]; then
        ln -frs /etc/mx-version /etc/antix-version
        fi
    fi
    
    #add a sources.list if one doesn't exist
    if [ ! -e /etc/apt/sources.list ]; then
        touch /etc/apt/sources.list
        echo "#this file is empty by default.  Sources are under /etc/apt/souces.list.d" >/etc/apt/sources.list
    fi
    
    #update mx-version to latest, but only for PRE_RC, RC, and later releases
    #if [ -e /etc/mx-version ]; then
    #sed -i s/_RC1_/_/ /etc/mx-version
    #sed -i s/'PRETTY_NAME="MX 21 xxxxx"'/'PRETTY_NAME="MX 21.1 xxxxx"'/ /etc/lsb-release
    #sed -i s/'DISTRIB_DESCRIPTION="MX 21 xxxxxx"'/'DISTRIB_DESCRIPTION="MX 21.1 xxxxxx"'/ /etc/lsb-release
    #sed -i s/DISTRIB_RELEASE.*/'DISTRIB_RELEASE=21.1 '/ /etc/lsb-release
    #fi
      
    #update installed /etc/issue
    #check for live system
    #check for existence so that it doesn't do anything during initial build process
    if [ -e /etc/issue ]; then
        if [ "$LIVE_CHECK" = "overlay" ] || [ "$LIVE_CHECK" = "aufs" ]; then
            echo "running live, no change to /etc/issue"
        else
            #copy from /usr/share/antiX
            #only if it looks like the antiX/mx live issue file
            CHECK=$(sed '/cli-installer/!d' /etc/issue)
            if [ -n "$CHECK" ]; then
                cp /usr/share/antiX/issue /etc/issue
            fi
        fi
    fi
    
    ##add vmd to /etc/initramfs-tools/modules if its not already there
    VMD_CHECK=$(sed '/\<vmd\>/!d' /etc/initramfs-tools/modules)
    if [ ! -n "$VMD_CHECK" ]; then
		echo "adding vmd modules to /etc/initramfs-tools/modules"
		echo "vmd" >> /etc/initramfs-tools/modules
	fi
    
    #reconfigure debconf with DEBIAN_FRONTEND to Gnome for graphical dpkg use when X available
    #use debconf-kde-helper with KDE
    #will default back to dialog if X not available
    #on default MX KDE iso, check for debconf-kde-helper
    if [ ! -e /etc/mxsystem_frontend.chk ]; then
		if [ -e "/usr/bin/debconf-kde-helper" ]; then
			echo 'debconf debconf/frontend select Kde' | sudo debconf-set-selections
		else 
			echo 'debconf debconf/frontend select Gnome' | debconf-set-selections
        fi
			touch /etc/mxsystem_frontend.chk
    fi
     
     #add legacy libsane folders
     
    if [ ! -e /lib/sane ]; then
        mkdir -p /usr/lib/sane
    fi
    
	if [ "$(dpkg --print-architecture)" = "amd64" ]; then
		if [ ! -e /usr/lib64/sane ]; then
        mkdir -p /usr/lib64/sane
        fi
    fi 
    
	if [ -e "/usr/share/pixmaps/ndisgtk.svg" ] && [ -e "/usr/share/pixmaps/ndisgtk.png.png" ]; then
		cp /usr/share/pixmaps/ndisgtk.svg /usr/share/icons/hicolor/scalable/apps/ndisgtk.svg
		cp /usr/share/pixmaps/ndisgtk.png.png /usr/share/icons/hicolor/48x48/apps/ndisgtk.png.png
    fi
    
    if [ -e "/usr/share/pixmaps/gnome-ppp.png" ]; then
		cp /usr/share/pixmaps/gnome-ppp.png /usr/share/icons/hicolor/48x48/apps/gnome-ppp.png
    fi
    
    if [ -e "/usr/share/pixmaps/pppoeconf.png" ]; then
		cp /usr/share/pixmaps/pppoeconf.png /usr/share/icons/hicolor/48x48/apps/pppoeconf.png
    fi
    
	#/etc/xdg/autostart/at-spi-dbus-bus.desktop "fix"
	if [ -e /etc/xdg/autostart/at-spi-dbus-bus.desktop ]; then
		cp /etc/xdg/autostart/at-spi-dbus-bus.desktop /etc/xdg/autostart/zz-at-spi-dbus-bus.desktop
		rm /etc/xdg/autostart/at-spi-dbus-bus.desktop
	fi
	
	#update 98vboxadd_xclient
    if [ -e /etc/X11/Xsession.d/98vboxadd-xclient ];then
        cp /usr/share/mx-system/Xsession.d/98vboxadd-xclient /etc/X11/Xsession.d/98vboxadd-xclient
    fi
    
fi

#DEBHELPER#
