#!/bin/bash
# Simple direct reproduction on the host system

echo "================================================"
echo "Direct reproduction of usbguard-selinux bug"
echo "================================================"
echo ""

# Create a temporary repo file
sudo tee /etc/yum.repos.d/koji-usbguard-test.repo > /dev/null <<EOF
[koji-usbguard-144467097]
name=Koji build for usbguard test
baseurl=https://kojipkgs.fedoraproject.org/repos/f45-build/latest/x86_64/
gpgcheck=0
enabled=1
priority=1
EOF

echo "Added Koji repository"
echo ""

# Check if usbguard is already installed
if rpm -q usbguard &>/dev/null; then
    echo "usbguard is already installed, removing first..."
    sudo dnf5 remove -y usbguard usbguard-selinux 2>/dev/null || true
fi

echo "=========================================="
echo "Installing usbguard-selinux-1.1.4-1.fc45"
echo "This should trigger the bug..."
echo "=========================================="
echo ""

set +e
sudo dnf5 install -y usbguard-selinux-1.1.4-1.fc45 2>&1 | tee /tmp/usbguard-install-test.log
RESULT=$?
set -e

# Cleanup
sudo rm -f /etc/yum.repos.d/koji-usbguard-test.repo

echo ""
if [ $RESULT -eq 0 ]; then
    echo "=========================================="
    echo "Installation SUCCEEDED"
    echo "=========================================="
    echo ""
    echo "Note: Bug was NOT reproduced. This might mean:"
    echo "- The package ordering was different"
    echo "- SELinux policy was updated"
    echo "- The issue only occurs in certain environments"
else
    echo "=========================================="
    echo "!! BUG REPRODUCED !!"
    echo "=========================================="
    echo ""
    echo "Error output:"
    grep -B 3 -A 5 "restorecon.*failed\|scriptlet failed\|Transaction failed" /tmp/usbguard-install-test.log || true
fi

