#!/bin/bash

SCRIPT_VER="1.5"

APP="OpenBox Desktop"
APP_CATEGORY="Desktop Environments"
APP_SCOPE="system"
APP_ENABLED="yes"
STORED_PASSWORD_SERVICE="user"
SUPPORTED_ON="CentOS7,CentOS8,CentOSStream8,Debian9,Debian10,Ubuntu16.04,Ubuntu18.04,Ubuntu20.04,Ubuntu22.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

    case "${LSB_VENDOR}" in
      "CentOS"|"CentOSStream")
          if [ "${LSB_RELEASE}" = "8" ] ; then
              YUM_GROUPINSTALL "Server with GUI" || break
          else
              YUM_GROUPINSTALL "X Window System" || break
          fi
          ;;
      "Ubuntu")
          if [ "${LSB_RELEASE}" = "20.04" ] || [ "${LSB_RELEASE}" = "22.04" ]
          then
              APT_INSTALL \
              "build-essential" \
              "python" \
              "pkg-config" \
              "libpango1.0-dev" \
              "libglib2.0-dev" \
              "libxml2-dev" \
              "libxcursor-dev" \
              "libimlib2-dev" \
              "libstartup-notification0-dev" \
              "libxext-dev" \
              "x11proto-randr-dev" \
              "openbox" \
              "obconf" \
              "feh" \
              "tint2" \
              "menu" \
              "menu-xdg" \
              || break

           		# -- We need to grab the "obmenu" package from source if not present already
              if [ ! -e /usr/local/bin/obmenu ] ; then
                  WGET "http://prdownloads.sourceforge.net/obmenu/obmenu-1.0.tar.gz" "${TMP_DIR}/obmenu-1.0.tar.gz" || break
                  TAR_ZXF "${TMP_DIR}/obmenu-1.0.tar.gz" "${TMP_DIR}" || break
                  oWD=$( pwd )
                  cd "${TMP_DIR}/obmenu-1.0"
                  RUN_LOG "python setup.py install 2>&1" || break
                  cd "${oWD}"
              fi
          # -- In case of Ubuntu16-18, we have the package in the repos
          else ${PKG_INSTALL} \
              "openbox" \
              "obmenu" \
              "obconf" \
              "feh" \
              "tint2" \
              || break
          fi
    esac
    
        
    # Build 'feh' on CentOS
    if [ "${LSB_VENDOR}" = "CentOS" ] ; then
        ${PKG_INSTALL} "git-core" "gcc" "gcc-c++" "make" \
                       "libX11-devel" "libXinerama-devel" \
                       "imlib2-devel" "libpng-devel" \
                       "libcurl-devel" "libXt-devel"
        if [ $? -eq 0 ] ; then
            GIT_CLONE "git://git.finalrewind.org/feh" "${TMP_DIR}/feh"
            oWD=$( pwd )
            cd "${TMP_DIR}/feh"
            RUN_LOG "make 2>&1" && RUN_LOG "make install 2>&1"
            [ $? -eq 0 ] && LN "/usr/local/bin/feh"   "/usr/bin/feh"
            cd "${oWD}"
        fi
    fi

    # Fetch config files
    WGET "${SUPPORT}/openbox/openbox-config.tar.gz" \
        && TAR_ZXF "${TMP_DIR}/openbox-config.tar.gz" "${USERHOME}/"
    if [ $? -eq 0 ] ; then
        # Process wallpaper
        case "${LSB_VENDOR}" in
          "CentOS"|"CentOSStream")
              YUM_INSTALL "ImageMagick"
              ;;
          "Debian"|"Ubuntu")
              APT_INSTALL "imagemagick"
              ;;
        esac
        if [ $? -eq 0 ] ; 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 southeast \
                       '${TMP_DIR}/logo.jpg' \
                       '${USERHOME}/Images/rapidseedbox-wp-openbox-template.jpg' \
                       '${USERHOME}/Images/rapidseedbox-wp-openbox.jpg' 2>&1"
            # Version info
            RUN_LOG "convert '${USERHOME}/Images/rapidseedbox-wp-openbox.jpg' \
                      -gravity southwest \
                      -pointsize 16 \
        	      -fill yellow -annotate +5+2 '${LSB_DESCRIPTION}' \
                    '${USERHOME}/Images/rapidseedbox-wp-openbox.jpg' 2>&1"
        else
            CP_FILE "${USERHOME}/Images/rapidseedbox-wp-openbox-template.jpg" \
                    "${USERHOME}/Images/rapidseedbox-wp-openbox.jpg"
        fi

        MKDIR "${USERHOME}/.config/openbox"
        cat <<-EOF > "${USERHOME}/.config/openbox/autostart" 2>> "${LOGFILE}"
		#feh --bg-fill ${USERHOME}/Images/rapidseedbox-wp-openbox.jpg
		feh --bg-scale ${USERHOME}/Images/rapidseedbox-wp-openbox.jpg
		EOF
        CHOWN_DIR "${USERNAME}":"${USERNAME}" "${USERHOME}/.config/openbox"
    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
    ${PKG_REMOVE} \
        "openbox" \
        "obconf" \
        "obmenu" \
        "feh"
    RM_FILES "${USERHOME}/Images/rapidseedbox-wp-openbox-template.jpg" \
             "${USERHOME}/Images/rapidseedbox-wp-openbox.jpg"
    RM_DIRS "${USERHOME}/.config/openbox"
fi

SCRIPTEXIT
