#!/bin/bash

# Test script for apt-ostree OSTree environment detection
# This script demonstrates how apt-ostree detects if it's running in an OSTree environment

set -e

echo "=== apt-ostree OSTree Environment Detection Test ==="
echo

# Check if we're in an OSTree environment
echo "1. Checking OSTree filesystem detection..."
if [ -d "/ostree" ]; then
    echo "   ✓ /ostree directory exists"
else
    echo "   ✗ /ostree directory does not exist"
fi

echo
echo "2. Checking OSTree booted detection..."
if [ -f "/run/ostree-booted" ]; then
    echo "   ✓ /run/ostree-booted file exists"
else
    echo "   ✗ /run/ostree-booted file does not exist"
fi

echo
echo "3. Checking OSTree kernel parameter..."
if grep -q "ostree" /proc/cmdline 2>/dev/null; then
    echo "   ✓ 'ostree' found in kernel command line"
else
    echo "   ✗ 'ostree' not found in kernel command line"
fi

echo
echo "4. Testing apt-ostree environment validation..."
if command -v apt-ostree >/dev/null 2>&1; then
    echo "   Running: apt-ostree daemon-ping"
    if apt-ostree daemon-ping 2>/dev/null; then
        echo "   ✓ apt-ostree daemon is available"
    else
        echo "   ✗ apt-ostree daemon is not available"
    fi
else
    echo "   ✗ apt-ostree command not found"
fi

echo
echo "5. Testing apt-ostree status command..."
if command -v apt-ostree >/dev/null 2>&1; then
    echo "   Running: apt-ostree status"
    if apt-ostree status 2>/dev/null; then
        echo "   ✓ apt-ostree status command works"
    else
        echo "   ✗ apt-ostree status command failed"
    fi
else
    echo "   ✗ apt-ostree command not found"
fi

echo
echo "=== Environment Summary ==="

# Determine environment type
if [ -d "/ostree" ] && [ -f "/run/ostree-booted" ]; then
    if grep -q "ostree" /proc/cmdline 2>/dev/null; then
        echo "Environment: Fully functional OSTree environment"
    else
        echo "Environment: Minimal OSTree environment (can operate)"
    fi
elif [ -d "/ostree" ]; then
    echo "Environment: Partial OSTree environment (filesystem only)"
else
    echo "Environment: Non-OSTree environment"
fi

echo
echo "=== Detection Methods Used ==="
echo "1. Filesystem Detection: /ostree directory"
echo "2. Boot Detection: /run/ostree-booted file"
echo "3. Kernel Parameter Detection: 'ostree' in /proc/cmdline"
echo "4. Library Detection: OSTree sysroot loading"
echo "5. Service Detection: apt-ostree daemon availability"
echo
echo "These detection methods match rpm-ostree's approach for"
echo "determining if the system is running in an OSTree environment." 