#!/bin/bash
# Test script for bootc systems
# This script enables usr-overlay and installs swai_inst for testing

set -e

# Check for root privileges
if [ "$EUID" -ne 0 ]; then
    echo "Error: This script must be run as root"
    exit 1
fi

echo "Enabling bootc usr-overlay..."
bootc usr-overlay || true

echo "Determining Python purelib path..."
# Dynamically fetch the purelib path using python3
SITELIB=$(python3 -c "import sysconfig; print(sysconfig.get_path('purelib'))")

if [ -z "$SITELIB" ]; then
    echo "Error: Could not determine Python purelib path."
    exit 1
fi

echo "Installing swai_inst to $SITELIB..."
mkdir -p "${SITELIB}/swai_inst"
mkdir -p /usr/bin
install -m 0755 *.py "${SITELIB}/swai_inst"
install -m 0777 __main__.py /usr/bin/swai-inst

echo "Installing desktop and mime files..."
mkdir -p /usr/share/applications
mkdir -p /usr/share/mime/packages
install -m 0644 swai-installer.desktop /usr/share/applications/
install -m 0644 swai-mime.xml /usr/share/mime/packages/

echo "Updating mime and desktop databases..."
update-mime-database /usr/share/mime || true
update-desktop-database /usr/share/applications || true

echo "Done! swai_inst is now installed for testing."
