#!/bin/bash

SCRIPT_VER="1.5"

APP="IceWM Desktop"
APP_CATEGORY="Desktop Environments"
APP_SCOPE="system"
APP_ENABLED="yes"
SYS_PKG_NAME="icewm"
#><#APP_CHECK_VER_CMD="${PKG_VER} icewm"
STORED_PASSWORD_SERVICE="user"
SUPPORTED_ON="Ubuntu20.04,Ubuntu22.04,Ubuntu24.04"
LIBRARY="functions4.sh"

if [ "${SUPPORT-}" = '' ]; then
    SUPPORT="https://installers.rapidseedbox.com/scripts/whmcs_client_installers/support"
fi

LOGFILE="/var/log/whmcs_client_installers/${0}.log"
[ ! -d "/var/log/whmcs_client_installers" ] && mkdir -p "/var/log/whmcs_client_installers"

wget -4 -nc -q --no-check-certificate --timeout=20 --tries=5 "${SUPPORT}/${LIBRARY}" >> "${LOGFILE}" 2>&1
if [ ! -f "./${LIBRARY}" ] ; then
    echo "55"
    echo "Error downloading \"${SUPPORT}/${LIBRARY}\"" >> "${LOGFILE}"
    exit 55
fi

# shellcheck source=support/functions4.sh
. "./${LIBRARY}"
SCRIPTENTRY

while : ; do

    main_rc=1

    CHECK_APP ; _rc=$?
    if [ ${_rc} -ne 0 ] ; then
        main_rc=${_rc}
        break
    fi

    [ -d "${USERHOME}" ] || break

    APT_INSTALL "icewm" "feh" || break

    # Fetch config files
    if WGET "${SUPPORT}/icewm/icewm-config.tar.gz" \
        && TAR_ZXF "${TMP_DIR}/icewm-config.tar.gz" "${USERHOME}/"
    then
        # Process wallpaper
        if APT_INSTALL "imagemagick" ; then
            INFO "Let's make new wallpaper..."
            RUN_LOG "convert '${USERHOME}/Images/${LSB_VENDOR_L}.jpg' \
                       -resize 128x128\> '${TMP_DIR}/logo.jpg' 2>&1"
            RUN_LOG "composite -gravity northeast \
                       '${TMP_DIR}/logo.jpg' \
                       '${USERHOME}/Images/rapidseedbox-wp-icewm-template.jpg' \
                       '${USERHOME}/Images/rapidseedbox-wp-icewm.jpg' 2>&1"
            # Version info
            RUN_LOG "convert '${USERHOME}/Images/rapidseedbox-wp-icewm.jpg' \
                      -gravity northwest \
                      -pointsize 16 \
                      -fill yellow -annotate +2+0 '${LSB_DESCRIPTION}' \
                    '${USERHOME}/Images/rapidseedbox-wp-icewm.jpg' 2>&1"
        else
            CP_FILE "${USERHOME}/Images/rapidseedbox-wp-icewm-template.jpg" \
                    "${USERHOME}/Images/rapidseedbox-wp-icewm.jpg"
        fi


        MKDIR "${USERHOME}/.icewm"
        cat <<-EOF > "${USERHOME}/.icewm/startup" 2>> "${LOGFILE}"
		#feh --bg-fill ${USERHOME}/Images/rapidseedbox-wp-icewm.jpg &
		feh --bg-scale ${USERHOME}/Images/rapidseedbox-wp-icewm.jpg &
		EOF
        CHMOD_FILE 0755 "${USERHOME}/.icewm/startup"
        CHOWN_DIR "${USERNAME}":"${USERNAME}" "${USERHOME}/.icewm"
    fi

    main_rc=0
    break

done


# Cleanup if error
if [ "${main_rc}" -ne 0 ] && [ "${main_rc}" -ne 111 ] && [ "${main_rc}" -ne 117 ] ; then
    ERROR "'${APP}': Installation failed."
    # Cleanup tasks here
    APT_REMOVE "icewm" "feh"
    APT_AUTOREMOVE
    RM_DIRS "${USERHOME}/.icewm"
    RM_FILES "${USERHOME}/Images/rapidseedbox-wp-icewm-template.jpg" \
             "${USERHOME}/Images/rapidseedbox-wp-icewm.jpg"
fi

SCRIPTEXIT
