#!/bin/bash

. /minioslib
. /minios_build.conf

SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"

/condinapt -l "${SCRIPT_DIR}/packages.list" -c "${SCRIPT_DIR}/minios_build.conf" -m "${SCRIPT_DIR}/condinapt.map"
if [ $? -ne 0 ]; then
    echo "Failed to install packages."
    exit 1
fi

rm -f /usr/share/applications/chromium-flux.desktop
rm -f /usr/share/applications/compton.desktop
rm -f /usr/share/applications/panel-preferences.desktop

rm -f /etc/skel/.fluxbox/fbpotgen.sh
rm -f /etc/skel/.fluxbox/menu.pot
rm -rf /etc/skel/.config/openbox

if [ -f /usr/share/applications/yad-icon-browser.desktop ]; then
    rm -f /usr/share/applications/yad-icon-browser.desktop
fi

# For each .desktop file in the /usr/share/applications directory
for FILE in /usr/share/applications/*.desktop; do

    # If the file contains a translated name for the current language layout
    if grep -q "Name\[$LAYOUTID\]" $FILE; then

        # Find and store the translated name, generic name, and comment
        TRANSLATED_NAME=$(grep "Name\[$LAYOUTID\]" $FILE | head -1 | cut -d= -f2-)
        TRANSLATED_GENERIC_NAME=$(grep "GenericName\[$LAYOUTID\]" $FILE | head -1 | cut -d= -f2-)
        TRANSLATED_COMMENT=$(grep "Comment\[$LAYOUTID\]" $FILE | head -1 | cut -d= -f2-)

        # If the translated name is not empty, replace the original name with it
        [ ! -z "$TRANSLATED_NAME" ] && sed -i -e "s/^Name=.*/Name=$TRANSLATED_NAME/g" $FILE

        # If the generic name is not empty, replace the original generic name with it
        [ ! -z "$TRANSLATED_GENERIC_NAME" ] && sed -i -e "s/^GenericName=.*/GenericName=$TRANSLATED_GENERIC_NAME/g" $FILE

        # If the comment is not empty, replace the original comment with it
        [ ! -z "$TRANSLATED_COMMENT" ] && sed -i -e "s/^Comment=.*/Comment=$TRANSLATED_COMMENT/g" $FILE

        # Remove the lines with translated names, generic names, and comments
        sed -i -e "/Name\[/d;/GenericName\[/d;/Comment\[/d" $FILE
    fi
done
