#!/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.
################################################################################

log() {
    echo "$1" >> "${LOG_FILE}"
}

check_fail () {
    local res=$1

    if [ $res -ne 0 ]; then
        echo "Installation of the DCV USB driver failed"
        echo "For more details refer to ${LOG_FILE}"
        exit 1
    fi
}

_user_id=$(id -u)

if [ "${_user_id}" != "0" ] ; then
    echo "Error: You need to be 'root' to install the Amazon DCV USB remotization."
    exit 1
fi

help="Parameters:\n\
 --quiet (optional, silently install the usb drivers without prompting the user)\n"

while [ "$1" != "" ]; do
    case "$1" in
        --quiet)        QUIET=1
                        ;;
        -h | --help )   printf "$help"
                        exit
                        ;;
        *)              echo "Unsupported parameter $1"
                        printf "$help"
                        exit 1
                        ;;
    esac
    shift
done

if [ -n "${QUIET}" ]; then
    _answer="y"
else
    read -p "Do you want to install the kernel module and enable USB remotization? [y|n] " _answer
fi

if [ "${_answer}" = "y" ]; then
    if [ -z "${DCV_ROOT}" ]; then
        export DCV_ROOT="/usr/share/dcv"
    fi

    if [ -n "${DCV_INSTALLER_LOG_FILE}" ]; then
        LOG_FILE="${DCV_INSTALLER_LOG_FILE}"
    else
        LOG_FILE="/var/log/dcv/usb-driver-update.log"
        echo "" 2>/dev/null > "${LOG_FILE}"
        if [ $? -ne 0 ]; then
            LOG_FILE=$(mktemp dcv-usb-driver-update-XXXXXX.log)
        fi
    fi

    /sbin/modprobe -r eveusb 2>> "${LOG_FILE}" >> "${LOG_FILE}"

    log "Setting USB module prerequisites..... "
    printf "Setting USB module prerequisites..... "
    export LOG_FILE=${LOG_FILE}
    ${DCV_ROOT}/eveusb/dcvusbdriverinstallprerequisites
    check_fail $?
    echo "Done"

    which dkms 2>/dev/null >/dev/null
    found="$?"
    if [ "${found}" = "0" ]; then
        dkms status -m eveusb 2>&1 | grep "installed\|added" > /dev/null
        if [ "$?" = "0" ]; then
            log "Removing old USB module "
            dkms remove -m eveusb -v 1.0.0 --all 2>> "${LOG_FILE}" >> "${LOG_FILE}"
            check_fail $?
        fi

        cat << EOF
DKMS was found on this system.

If you do not use DKMS, you will need to manually build the module with
/usr/bin/dcvusbdriverupdate every time you install
a new kernel on the system.
EOF

        if [ -n "${QUIET}" ]; then
            _answer="y"
        else
            read -p "Do you want to use DKMS to install the kernel module? [y|n] " _answer
        fi

        if [ "${_answer}" = "y" ]; then
            log "Installing USB module with DKMS"
            printf "Installing USB module... "

            mkdir -p /usr/src/eveusb-1.0.0 2>> "${LOG_FILE}" >> "${LOG_FILE}"
            cp -fr ${DCV_ROOT}/eveusb/* /usr/src/eveusb-1.0.0 2>> "${LOG_FILE}" >> "${LOG_FILE}"

            dkms add -m eveusb -v 1.0.0 2>> "${LOG_FILE}" >> "${LOG_FILE}"
            check_fail $?

            dkms build -m eveusb -v 1.0.0 2>> "${LOG_FILE}" >> "${LOG_FILE}"
            check_fail $?

            dkms install -m eveusb -v 1.0.0 2>> "${LOG_FILE}" >> "${LOG_FILE}"
            check_fail $?

            echo "Done"
            log "USB module installed."
        else
            export DCV_INSTALLER_LOG_FILE="${LOG_FILE}"
            /usr/bin/dcvusbdriverupdate
        fi
    else
        cat << EOF
DKMS was not found on this system.

Without DKMS, you will need to manually build the module
with /usr/bin/dcvusbdriverupdate every time you install
a new kernel on the system.
EOF

        export DCV_INSTALLER_LOG_FILE="${LOG_FILE}"
        /usr/bin/dcvusbdriverupdate
    fi
fi
