#!/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="platform-info-binding"

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

# Install or reinstall dependencies
for p in  afb-libpython afb-test-py python3-pip afb-cmake-modules librp-utils-json-c-devel libevdev afb-helpers4 gcc g++ make cmake json-c afb-binding libmicrohttpd libdb-devel platform-runtime-tools; do
    sudo rpm -q $p && sudo dnf update -y $p || sudo dnf install -y $p 
done

sudo pip3 install libevdev

# Install missing tools if needed
for p in g++ lcov; do
    command -v $p 2>/dev/null 2>&1 || sudo dnf install -y $p
done

# 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

sudo systemctl start platform-core-detection
sudo systemctl start platform-os-detection
sudo systemctl start platform-devices-detection

echo "--- Start test-basic python platform-info binding ---"
export GCOV_PREFIX=${SCRIPT_DIR}/coverage_data/
export GCOV_PREFIX_STRIP=5
LD_LIBRARY_PATH=${SCRIPT_DIR}/coverage_data/${PACKAGE_NAME}/lib python ${SCRIPT_DIR}/test-basic.py --tap | tee /var/log/redtest/${PACKAGE_NAME}/tests.tap 2>&1


sudo chmod 0666 /dev/uinput
sudo chown root:input /dev/uinput
sudo systemctl restart systemd-udevd

echo "--- Start test-event python platform-info binding ---"
LD_LIBRARY_PATH=${SCRIPT_DIR}/coverage_data/${PACKAGE_NAME}/lib python ${SCRIPT_DIR}/test-event.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/ python -m lcov_cobertura app_filtered.info -o ${LOG_DIR}/coverage.xml


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

