#!/bin/bash

SCRIPT_VER="1.4"

APP="Cinnamon Desktop"
APP_CATEGORY="Desktop Environments"
APP_SCOPE="system"
APP_ENABLED="yes"
SYS_PKG_NAME="cinnamon"
STORED_PASSWORD_SERVICE="user"
SUPPORTED_ON="Ubuntu20.04,Ubuntu24.04"
MEMORY_K_MIN="4000000"
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

    APT_INSTALL \
        "cinnamon-desktop-environment" \
        "cinnamon" \
      || break

    if [ ! -d "${USERHOME}/.config/autostart" ] ; then
        MKDIR "${USERHOME}/.config/autostart" || break
        CHOWN_DIR "${USERNAME}":"${USERNAME}" "${USERHOME}/.config/autostart" || break
    fi
    if [ ! -d "${USERHOME}/bin" ] ; then
        MKDIR "${USERHOME}/bin" || break
        CHOWN_DIR "${USERNAME}":"${USERNAME}" "${USERHOME}/bin" || break
    fi

    for _as_file in \
        "blueman" \
        "dropbox" \
    ; do
        [ -e "${USERHOME}/.config/autostart/${_as_file}.desktop" ] \
            && RM_FILES "${USERHOME}/.config/autostart/${_as_file}.desktop"
    done

    [ -e "/etc/profile.d/LANG_en_US.UTF-8.sh" ] \
        || echo "export LANG=en_US.UTF-8" > "/etc/profile.d/LANG_en_US.UTF-8.sh"

#    # Reset to default settings
#    RUN_LOG "sudo -u ${USERNAME} gsettings reset-recursively org.cinnamon"

    # Install/Update themes
    for _theme in \
        "Windows-10" \
        "Windows-10-Dark" \
        "Windows-10-Metro" \
    ; do
        if [ -d "/usr/share/themes/${_theme}" ] ; then
            RUN_LOG "GIT_DIR=/usr/share/themes/${_theme}/.git git pull https://github.com/B00merang-Project/${_theme}.git"
        else
            GIT_CLONE \
                "https://github.com/B00merang-Project/${_theme}.git" \
                "/usr/share/themes/${_theme}"
        fi
    done

    # Install/Update icons
    if [ -d "/usr/share/icons/Windows-10" ] ; then
        RUN_LOG "GIT_DIR=/usr/share/icons/Windows-10/.git git pull https://github.com/B00merang-Artwork/Windows-10"
    else
        GIT_CLONE "https://github.com/B00merang-Artwork/Windows-10" "/usr/share/icons/Windows-10"
    fi

    # Install default theme:
    _default_theme="Windows-10-Metro"
    _default_icon_theme="Windows-10"

#    # Set window borders.
#    RUN_LOG "sudo -u ${USERNAME} gsettings set org.cinnamon.desktop.wm.preferences theme '${_default_theme}'" \
#    RUN_LOG "su -l ${USERNAME} -c 'gsettings set org.cinnamon.desktop.wm.preferences theme \"${_default_theme}\"'" \
#        || break
#    # Set icons.
##    RUN_LOG "sudo -u ${USERNAME} gsettings set org.cinnamon.desktop.interface icon-theme '${_default_theme}'" \
#    RUN_LOG "su -l ${USERNAME} -c 'gsettings set org.cinnamon.desktop.interface icon-theme \"${_default_theme}\"'" \
#        || break
#    # Set controls.
##    RUN_LOG "sudo -u ${USERNAME} gsettings set org.cinnamon.desktop.interface gtk-theme '${_default_theme}'" \
#    RUN_LOG "su -l ${USERNAME} -c 'gsettings set org.cinnamon.desktop.interface gtk-theme \"${_default_theme}\"'" \
#        || break
#    # Set mouse pointer.
##    RUN_LOG "sudo -u ${USERNAME} gsettings set org.cinnamon.desktop.interface cursor-theme '${_default_theme}'" \
#    RUN_LOG "su -l ${USERNAME} -c 'gsettings set org.cinnamon.desktop.interface cursor-theme \"${_default_theme}\"'" \
#        || break
#    # Set Desktop theme.
##    RUN_LOG "sudo -u ${USERNAME} gsettings set org.cinnamon.theme name '${_default_theme}'" \
#    RUN_LOG "su -l ${USERNAME} -c 'gsettings set org.cinnamon.theme name \"${_default_theme}\"'" \
#        || break
#
#    [ -e "${USERHOME}/Images/rapidseedbox-wp-template.jpg" ] \
#        && RUN_LOG "su -l ${USERNAME} -c 'gsettings set org.cinnamon.desktop.background picture-uri \"file://${USERHOME}/Images/rapidseedbox-wp-template.jpg\"'"

    cat <<-EOF > "${USERHOME}/.config/autostart/cinnamon-settings.desktop"
	[Desktop Entry]
	Type=Application
	Name=Cinnamon settings
	Exec=${USERHOME}/bin/cinnamon-settings.sh
	Terminal=false
	EOF
    CHOWN_FILE "${USERNAME}":"${USERNAME}" "${USERHOME}/.config/autostart/cinnamon-settings.desktop"

    cat <<-EOF > "${USERHOME}/bin/cinnamon-settings.sh"
	#!/bin/bash
	[ "\${XDG_SESSION_DESKTOP}" = "cinnamon" ] || exit
	# Set window borders.
	gsettings set org.cinnamon.desktop.wm.preferences theme "${_default_theme}"
	# Set icons.
	gsettings set org.cinnamon.desktop.interface icon-theme "${_default_icon_theme}"
	# Set controls.
	gsettings set org.cinnamon.desktop.interface gtk-theme "${_default_theme}"
	# Set mouse pointer.
	gsettings set org.cinnamon.desktop.interface cursor-theme "${_default_theme}"
	# Set Desktop theme.
	gsettings set org.cinnamon.theme name "${_default_theme}"
	# Set wallpaper.
	[ -e "${USERHOME}/Images/rapidseedbox-wp-template.jpg" ] \
	    && gsettings set org.cinnamon.desktop.background picture-uri "file://${USERHOME}/Images/rapidseedbox-wp-template.jpg"
	EOF
    CHOWN_FILE "${USERNAME}":"${USERNAME}" "${USERHOME}/bin/cinnamon-settings.sh"
    CHMOD_FILE 700 "${USERHOME}/bin/cinnamon-settings.sh"

#    if [ -e "/usr/NX/etc/node.cfg" ] ; then
#        RUN_LOG "sed -i \
#                   -e 's|^.*DefaultDesktopCommand.*\$|DefaultDesktopCommand \"/usr/bin/cinnamon-session\"|' \
#                   '/usr/NX/etc/node.cfg'"
#        SVC_STATUS "nxserver" && SVC_RESTART  "nxserver"
#    fi


    main_rc=0
    break

done


# Cleanup if error
if [ ${main_rc} -ne 0 -a ${main_rc} -ne 111 -a ${main_rc} -ne 117 ] ; then
    ERROR "'${APP}': Installation failed."
    # Cleanup tasks here
    APT_REMOVE "cinnamon-desktop-environment" "cinnamon"
    APT_AUTOREMOVE
fi

SCRIPTEXIT
