#!/bin/bash

SCRIPT_VER="1.1"

APP="Test - DB 001"
APP_CATEGORY="__Fixes"
APP_SCOPE="system"
APP_ENABLED="no"
SUPPORTED_ON="CentOS7,CentOS8,Debian9,Debian10,Ubuntu16.04,Ubuntu18.04,Ubuntu20.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

# -- Functions

function _init_db () {
    local db_ver, pfx, python_ver, s, ss
    local cat_id, cat_name, app_name, app_sname, app_supp, app_status
    local oAPP, oAPP_DIRECTORY, oDELUGE_VER, oRT_S_VER
    if [ -f "${APP_DB}" ] ; then
        db_ver=$( ${SQL_CMD} "${APP_DB}" "SELECT id FROM version" )
        if [ "${db_ver}" != "${APP_DB_VER}" ] ; then
            RM_FILES "${APP_DB}" || return 3
        else
            return 0
        fi
    fi
    INFO "Initializing DB: [${APP_DB}]"
    ${__run_cmd} "${SQL_CMD} \"${APP_DB}\"" <<-EOF
		CREATE TABLE version ( id TEXT );
		INSERT INTO version ( id ) VALUES ( "${APP_DB_VER}" );
		CREATE TABLE category    (
		    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
		    name TEXT
		  );
		CREATE TABLE application (
		    id     INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
		    cat_id INTEGER,
		    name   TEXT,
		    sname  TEXT,
		    status INTEGER
		  );
		EOF
    if [ $? -ne 0 ] ; then
        ERROR "Error initializing DB !"
        return 3
    fi
    WGET "${SUPPORT}/categorized-apps.list.auto" || return 3
    RSB_SITE=$( echo "${SUPPORT}" | sed -e 's|\(.*\)/support$|\1|' )
    oAPP="${APP}"
    oAPP_DIRECTORY="${APP_DIRECTORY}"
    oDELUGE_VER="${DELUGE_VER}"
    oRT_S_VER="${RT_S__VER}"
    while read s ; do
        cat_name=$(  echo "${s}" | awk -F'/' '{ print $1 }' )
        app_name=$(  echo "${s}" | awk -F'/' '{ print $2 }' )
        app_sname=$( echo "${s}" | awk -F'/' '{ print $3 }' )
        app_supp=$(  echo "${s}" | awk -F'/' '{ print $4 }' )
        # Check "supported" status:
        RUN_LOG "echo '${app_supp}' | grep -E '^${LSB_VER}$|^${LSB_VER},|,${LSB_VER}$|,${LSB_VER},' 2>&1"
        [ $? -ne 0 ] && continue
        cat_id=$( ${SQL_CMD} "${APP_DB}" "SELECT id FROM category WHERE name='${cat_name}'" )
        if [ -z "${cat_id}" ] ; then
            ${__run_cmd} "${SQL_CMD} \"${APP_DB}\"" <<-EOF
			INSERT INTO category ( name ) VALUES ( "${cat_name}" );
			EOF
            cat_id=$( ${SQL_CMD} "${APP_DB}" "SELECT id FROM category WHERE name='${cat_name}'" )
        fi
        WGET "${RSB_SITE}/install_${app_sname}.sh" || continue
        str=$( grep -e '^APP=' "${TMP_DIR}/install_${app_sname}.sh" )
        [ -n "${str}" ] && eval ${str}
        str=$( grep -e '^APP_DIRECTORY=' "${TMP_DIR}/install_${app_sname}.sh" )
        [ -n "${str}" ] && eval ${str}
        str=$( grep -e '^DELUGE_VER=' "${TMP_DIR}/install_${app_sname}.sh" )
        [ -n "${str}" ] && eval ${str}
        str=$( grep -e '^RT_S_VER=' "${TMP_DIR}/install_${app_sname}.sh" )
        [ -n "${str}" ] && eval ${str}
        app_status=0
        ${CHECK_APP} "${app_name}" "install"
        if [ $? -eq 111 ] ; then
            if [ ! -z "${DELUGE_VER}" ] ; then
                for pfx in "/usr" "/usr/local" ; do
                    for python_ver in "2.6" "2.7" ; do
                        if [ -d "${pfx}/lib/python${python_ver}/site-packages/deluge-${DELUGE_VER}-py${python_ver}.egg" -o \
                             -d "${pfx}/lib/python${python_ver}/site-packages/deluge-${DELUGE_VER}-py${python_ver}.egg" ] ; then
                            app_status=1
                            break 2
                        fi
                    done
                done
            elif [ ! -z  "${RT_S_VER}" ] ; then
                [ -d "/usr/local/rt${RT_S_VER}" ] && app_status=1
            else
                app_status=1
            fi
        fi
        ${__run_cmd} "${SQL_CMD} \"${APP_DB}\"" <<-EOF
		INSERT INTO application ( cat_id, name, sname, status )
		       VALUES ( ${cat_id}, "${app_name}", "${app_sname}", ${app_status} );
		EOF
        APP=""
        APP_DIRECTORY=""
        DELUGE_VER=""
        RT_S_VER=""
    done < "${TMP_DIR}/categorized-apps.list.auto"
    APP="${oAPP}"
    APP_DIRECTORY="${oAPP_DIRECTORY}"
    DELUGE_VER="${oDELUGE_VER}"
    RT_S_VER="${oRT_S__VER}"
    return 0
}


# -- Main test section

_init_db


SCRIPTEXIT
