#!/usr/bin/env bash
#################################################################
#
#   SPDX-License-Identifier: BSD-3-Clause
#   Copyright(c) 2007-2026 Intel Corporation
# 
#  version: QAT.B.3.15.0-00012
#   These contents may have been developed with support from one or more
#   Intel-operated generative artificial intelligence solutions.
#
#################################################################

#################################################################
# @file: adf_ctl_dbg_wrapper
#
# @description:
#     Script wraps original adf_ctl tool to perform debuggability
#     related routines before and after adf_ctl execution.
#     Debuggability routines can consits of:
#     - shutting down synchronization daemon before ADF stop
#       if deamon exists
#     - starting synchronization daemon if debuggability
#       feature is turned on for at leat one device
#
#################################################################

QAT_ADF_CTL_BIN=/usr/sbin/adf_ctl_bin
QAT_DBG_DMN_PATH=/usr/sbin/qat_dbg_sync_daemon

QAT_MAX_TERM_RETRY=5
QAT_SLEEP_RETRY=0.1

TerminateSyncDaemon()
{
    local -i attempt=0
    local dmn_name

    dmn_name=$(basename ${QAT_DBG_DMN_PATH})
    if [ -z ${dmn_name} ]; then
        exit 1
    fi

    # Term loop
    while [ ${attempt} -lt ${QAT_MAX_TERM_RETRY} ]; do
        pgrep ${dmn_name} 2> /dev/null | xargs kill -TERM 2> /dev/null
        if [ $? -ne 0 ]; then
            break
        fi
        sleep $QAT_SLEEP_RETRY
        ((attempt = attempt + 1))
    done

    # Check if there is any dbg daemon instance running
    pgrep ${dmn_name} > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        exit 0
    fi

    # Kill loop
    attempt=0
    while [ ${attempt} -lt ${QAT_MAX_TERM_RETRY} ]; do
        pgrep ${dmn_name} 2> /dev/null | xargs kill -KILL 2> /dev/null
        if [ $? -ne 0 ]; then
            break
        fi
        sleep $QAT_SLEEP_RETRY
        ((attempt = attempt + 1))
    done

    # Check if there is any dbg daemon instance running
    pgrep ${dmn_name} > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        exit 0
    fi

    exit 1
}

GetNoOfDbgDevices()
{
    local -i dbg_on_ctr=0

    dbg_on_ctr=$(sysctl -a | egrep "dev.qat.[0-9].qat_debug.enabled" | grep -c "enabled: 1")

    echo ${dbg_on_ctr}
}

StartSyncDaemon()
{
    local dbg_devices

    # Get number of devices configured with debuggability
    dbg_devices=$(GetNoOfDbgDevices)
    if [[ ${dbg_devices} -gt 0 ]]; then
        if [[ ! -f ${QAT_DBG_DMN_PATH} ]]; then
            exit 1
        fi
        ${QAT_DBG_DMN_PATH} > /dev/null 2>&1
        if [[ $? -ne 0 ]]; then
            exit 1
        fi
    fi
}

# Check against satus command
status_only=0
for arg in "$@"; do
    if [[ ${arg} =~ "status" ]]; then
        status_only=1
    fi
done

if [ ${status_only} -gt 0 ]; then
    # Use original adf_ctl to start device
    ${QAT_ADF_CTL_BIN} $@
    exit $?
fi

# Terminate all instances of QAT dbg daemon
out=$(TerminateSyncDaemon)
if [ $? -ne 0 ]; then
    echo "QAT dbg synchronization daemon terminating failed"
    exit 1
fi

# Use original adf_ctl to start device
${QAT_ADF_CTL_BIN} $@
# Save original return code
adf_ctl_rc=$?

# Start daemon
out=$(StartSyncDaemon)
if [ $? -ne 0 ]; then
    echo "Starting QAT dbg synchronization daemon failed"
    exit 1
fi

exit ${adf_ctl_rc}
