#!/bin/bash

set -e          # exit on error
set -o pipefail # exit on pipeline error
set -u          # treat unset variable as error

. /minioslib
. /minios_build.conf

SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
LNG="$(echo "$LOCALE" | cut -d_ -f1)"

console_colors

DEBCONF_SETTINGS+=(
    "wireshark-common        wireshark-common/install-setuid boolean true"
)
(
    for SETTING in "${DEBCONF_SETTINGS[@]}"; do
        echo "${SETTING}" | debconf-set-selections -v >/dev/null 2>&1
    done
) &
DEBCONF_PID="$!"
spinner "${DEBCONF_PID}" "Applying debconf settings"
wait "${DEBCONF_PID}"

/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

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

if [ -f "/usr/share/applications/display-im6.q16.desktop" ]; then
    rm -f /usr/share/applications/display-im6.q16.desktop
fi

if ([ "$PACKAGE_VARIANT" = "toolbox" ] || [ "$PACKAGE_VARIANT" = "ultra" ]) && ! pkg_is_installed "codium"; then
    if [ "$VERBOSITY_LEVEL" -ge 1 ]; then
        curl -LJO $(curl --silent "https://api.github.com/repos/VSCodium/vscodium/releases/latest" |
            grep '"browser_download_url":' | grep "$DISTRIBUTION_ARCH.deb"\" |
            sed -E 's/.*"([^"]+)".*/\1/')
        apt-get update
        apt-get -y install ./$(ls codium*$DISTRIBUTION_ARCH.deb)
    else
        run_with_spinner "Downloading VSCodium" bash -c '
            curl -sLJO $(curl --silent "https://api.github.com/repos/VSCodium/vscodium/releases/latest" |
                grep '"'"browser_download_url"'"': | grep "$DISTRIBUTION_ARCH.deb"\" |
                sed -E "s/.*\"([^\"]+)\".*/\1/")
        '
        run_with_spinner "Updating package lists" apt-get update --allow-releaseinfo-change
        run_with_spinner "Installing VSCodium" apt-get -y install ./$(ls codium*$DISTRIBUTION_ARCH.deb)
    fi
    if [ "$USE_APT_CACHE" = "true" ]; then
        cp -f ./$(ls codium*$DISTRIBUTION_ARCH.deb) /var/cache/apt/archives/
    fi
    rm -f ./$(ls codium*$DISTRIBUTION_ARCH.deb)
fi

if [ -f "/usr/share/applications/isomaster.desktop" ]; then
    sed -i "s,/usr/share/isomaster/icons/isomaster.png,isomaster,g" /usr/share/applications/isomaster.desktop
fi
if [ -f "/usr/share/applications/Hard_Disk_Sentinel_GUI.desktop" ]; then
    sed -i "s,/usr/share/icons/HDSentinel_GUI.ico,hdsentinel,g" /usr/share/applications/Hard_Disk_Sentinel_GUI.desktop
fi
if [ -f "/usr/share/applications/guymager.desktop" ]; then
    sed -i "s,guymager_128.png,guymager,g" /usr/share/applications/guymager.desktop
fi
if [ -f "/usr/share/applications/zulumount-gui.desktop" ]; then
    sed -i "s,zuluMount.xpm,zuluMount,g" /usr/share/applications/zulumount-gui.desktop
fi
if [ -f "/usr/share/applications/zulucrypt-gui.desktop" ]; then
    sed -i "s,zuluCrypt.xpm,zuluCrypt,g" /usr/share/applications/zulucrypt-gui.desktop
fi
if [ -f "/usr/share/applications/hardinfo.desktop" ]; then
    sed -i "s,/usr/share/hardinfo/pixmaps/logo.png,hardinfo,g" /usr/share/applications/hardinfo.desktop
fi
if [ -f "/usr/share/applications/zenmap.desktop" ]; then
    sed -i "s,Exec=zenmap %F,Exec=pkexec /usr/bin/zenmap %F,g" /usr/share/applications/zenmap.desktop
    sed -i "s,/usr/share/zenmap/pixmaps/zenmap.png,zenmap,g" /usr/share/applications/zenmap.desktop
fi
if [ -f "/usr/share/applications/zenmap-root.desktop" ]; then
    rm -f /usr/share/applications/zenmap-root.desktop
fi
if [ -f "/usr/share/applications/veracrypt.desktop" ]; then
    sed -i "s,VeraCrypt-48x48,veracrypt,g" /usr/share/applications/veracrypt.desktop
fi
#if [ -f "/usr/share/applications/org.wireshark.Wireshark.desktop" ]; then
#    sed -i 's,Exec=wireshark %f,Exec=pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY /usr/bin/wireshark %f,g' /usr/share/applications/org.wireshark.Wireshark.desktop
#fi
if [ "$LOCALE" != "en_US" ]; then
    if [ "$LOCALE" = "pt_BR" ] || [ "$LOCALE" = "zh_CN" ] || [ "$LOCALE" = "zh_TW" ]; then
        sed -i "s/doublecmd.en.po/doublecmd.$LOCALE.po/g" /etc/skel/.config/doublecmd/doublecmd.xml
    else
        sed -i "s/doublecmd.en.po/doublecmd.$LNG.po/g" /etc/skel/.config/doublecmd/doublecmd.xml
    fi
fi
if [ -f "/usr/share/applications/gnumeric.desktop" ]; then
    sed -i "s/Science;//g" /usr/share/applications/gnumeric.desktop
fi
if ! pkg_is_installed "clonezilla"; then
    rm -f /usr/sbin/clonezilla.sh
    rm -f /usr/share/applications/clonezilla.desktop
    rm -f /usr/share/pixmaps/ocslogo.png
fi
if pkg_is_installed "gnome-software"; then
    mkdir -p /var/cache/swcatalog/cache
fi
