#!/bin/sh -e

if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-remove" ] ; then
  # 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
    fi
    # fix man init entry to reflect sysVinit instead of systemd
    #if [ -e /usr/share/man/man8/init.8.gz ]; then
    #ln -fs /usr/share/man/man8/init.8.gz /usr/share/man/man1/init.1.gz
    #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
    
    #mark localize-repo-mx as manually installed
    apt-mark manual localize-repo-mx
    
    #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/MX-18.*_/MX-18.3_/ /etc/mx-version
    sed -i s/MX-19RC1.*_/MX-19_/ /etc/mx-version
    #sed -i s/18.*\\s/'18.3 '/ /etc/lsb-release
    #sed -i s/DISTRIB_RELEASE=18.*/DISTRIB_RELEASE=18.3/ /etc/lsb-release
    fi
    
    ##add ahs repo to mx.list
    FILE="/etc/apt/sources.list.d/mx.list"
    # if the entry is present, don't do anything
    # add disabled in any case
    if [ -z "$(grep "mx" "$FILE" |grep buster |grep ahs)" ]; then
        echo "" >> "$FILE"
        echo "#ahs hardware stack repo" >> "$FILE"
        echo "#deb $(cat /etc/apt/sources.list.d/mx.list |grep "main" |cut -d" " -f2) buster ahs" >> "$FILE"
    fi 

    #update udev rules for optical devices (see install)
    udevadm control --reload
    udevadm trigger --attr-match=kernel="sr[0-9]*"
        
    #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
        #test for live system, exit if found
        LIVE_CHECK=$(df -T / |tail -n1 |awk '{print $2}')
        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
    #will default back to dialog if X not available
    if [ ! -e /etc/mxsystem_frontend.chk ]; then
        #debconf-set-selections <<< "debconf debconf/frontend select Gnome" 
        echo 'debconf debconf/frontend select Gnome' | debconf-set-selections
        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
   
fi

#DEBHELPER#
