#!/bin/bash

# When this script will be run, the packages your-application.rpm and your-application-redtest.rpm
# would have been installed on the target

PACKAGE_NAME="wifiap-binding"

# Load the mac80211_hwsim kernel module
dnf install -y kernel-modules-internal hostapd dnsmasq
modprobe mac80211_hwsim |:

# Create the directory where the logs need to be
LOG_DIR="/var/log/redtest/${PACKAGE_NAME}"
mkdir -p ${LOG_DIR}

# Run your tests
# To be filled by you! Remember, the log file needs to be in ".tap" format

echo "--- Start test python wifiap binding ---"
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

export GCOV_PREFIX=${SCRIPT_DIR}/coverage_data/
export GCOV_PREFIX_STRIP=5

LD_LIBRARY_PATH=${SCRIPT_DIR}/coverage_data/${PACKAGE_NAME}/lib/ python3 ${SCRIPT_DIR}/tests.py --tap | tee /var/log/redtest/${PACKAGE_NAME}/tests.tap 2>&1


##########################
# Coverage report section
##########################

echo "--- Generating coverage reports ---"

# Clean previous coverage data
rm -f lcov_cobertura*
rm -rf lcov_cobertura-*

# Download lcov_cobertura tool
wget https://github.com/eriwen/lcov-to-cobertura-xml/releases/download/v2.1.1/lcov_cobertura-2.1.1.tar.gz
tar xzvf lcov_cobertura-2.1.1.tar.gz

# Generate lcov info file
rm -f app.info app_filtered.info
lcov --directory ${SCRIPT_DIR}/coverage_data/ --capture --output-file app.info

# Remove system headers and other noise
lcov --remove app.info '/usr/*' -o app_filtered.info

# Show coverage summary (will appear in GitLab CI logs)
lcov --list app_filtered.info

# Generate Cobertura-compatible XML for GitLab coverage
PYTHONPATH=./lcov_cobertura-2.1.1/ python3 -m lcov_cobertura app_filtered.info -o ${LOG_DIR}/coverage.xml


echo "--- Coverage report generated at ${LOG_DIR}/ ---"

