#!/usr/bin/sh

################################################################################
# Copyright (C) 2015-2026 NICE s.r.l.
# All Rights Reserved
#
# This software is the confidential and proprietary information
# of NICE s.r.l. ("Confidential Information").
# You shall not disclose such Confidential Information
# and shall use it only in accordance with the terms of
# the license agreement you entered into with NICE.
################################################################################

set -e

get_platform() {
    local _id=$(cat /etc/os-release | grep '^ID=' | cut -d'=' -f2 | tr -d '"' | tr '[:upper:]' '[:lower:]')
    local _version=$(cat /etc/os-release | grep '^VERSION_ID=' | cut -d'=' -f2 | tr -d '"')

    echo $_id:$_version
}

install_usb_module_prerequisites() {
    local _platform=$1

    case $_platform in
        sles )
            zypper in -y kernel-devel gcc 2>> "${LOG_FILE}" >> "${LOG_FILE}"
            ;;
        amzn|centos|rhel|rocky )
            yum install -y kernel-devel gcc 2>> "${LOG_FILE}" >> "${LOG_FILE}"
            ;;
        fedora )
            dnf -y install kernel-devel gcc 2>> "${LOG_FILE}" >> "${LOG_FILE}"
            ;;
        ubuntu|debian )
            apt -y install linux-headers-$(uname -r) gcc 2>> "${LOG_FILE}" >> "${LOG_FILE}"
            ;;
        * )
            echo "Failure installing usb module prerequisites. '$_platform' is not a supported operative system." >> "${LOG_FILE}"
            return 1
            ;;
    esac
}

configure_system () {
    local _platform=$1
    local _platform_major_version=$2

    case $_platform in
        sles )
            local _conf=/etc/modprobe.d/10-unsupported-modules.conf
            local _allow_unsupported_modules=0
            if [ -f $_conf ]; then
                _allow_unsupported_modules=$(grep "^allow_unsupported_modules[[:space:]]\+[0-9]" "$_conf" | tr -s [:space:] | cut -d' ' -f2)
            fi

            if [ "${_allow_unsupported_modules}" != "1" ]; then
                cat <<EOF
The DCV USB kernel cannot be installed because third party kernel modules are disabled on this system.
In order to enable third party kernel modules, you need to edit the $_conf file.

Refer to:
https://documentation.suse.com/sles/${_platform_major_version}/html/SLES-all/cha-adm-support.html#sec-admsupport-kernel-unsupported
EOF
                return 1
            fi
            ;;
        centos|rhel|rocky )
            local major_kernel_version=$(uname -r | cut -d'.' -f1)
            local minor_kernel_version=$(uname -r | cut -d'.' -f2)
            if [ $major_kernel_version -gt 4 ] || { [ $major_kernel_version -eq  4 ] && [ $minor_kernel_version -ge 18 ]; }; then
                sed -i "s/#define ACCESS_OK_FLAG 0/#define ACCESS_OK_FLAG 1/g" "${DCV_ROOT}/eveusb/eveusb-vhci-iocifc.c"
            fi
            ;;
        amzn|debian|fedora|ubuntu )
            ;;
        * )
            echo "Failure configuring the system. '$_platform' is not a supported operative system." >> "${LOG_FILE}"
            return 1
            ;;
    esac
}

main () {
    local _platform_info=$(get_platform)
    local _platform=${_platform_info%:*}
    local _platform_version=${_platform_info#*:}
    local _platform_major_version=${_platform_version%.*}

    install_usb_module_prerequisites $_platform
    configure_system $_platform $_platform_major_version
}

main "$@"
