#!/bin/bash
# TSID QR Debug Script

echo "======================================"
echo "TSID QR Login Debug Information"
echo "======================================"
echo "Time: $(date)"
echo ""

# 1. Check if TSID is installed
echo "1. TSID Installation Status:"
if [ -f "/lib/x86_64-linux-gnu/security/pam_tsid.so" ] || [ -f "/usr/lib64/security/pam_tsid.so" ]; then
    echo "   PAM module installed"
    if [ -f "/lib/x86_64-linux-gnu/security/pam_tsid.so" ]; then
        echo "   Found Ubuntu PAM module: /lib/x86_64-linux-gnu/security/pam_tsid.so"
    fi
    if [ -f "/usr/lib64/security/pam_tsid.so" ]; then
        echo "   Found Rocky PAM module: /usr/lib64/security/pam_tsid.so"
    fi
else
    echo "   PAM module NOT found"
fi

if [ -f "/usr/bin/tsid-qr-display" ]; then
    echo "   QR display script exists"
else
    echo "   QR display script NOT found"
fi

# 2. Check LightDM configuration
echo ""
echo "2. LightDM Configuration:"
if [ -f "/etc/lightdm/lightdm.conf.d/50-tsid-greeter.conf" ]; then
    echo "   ✓ TSID greeter config exists"
    cat /etc/lightdm/lightdm.conf.d/50-tsid-greeter.conf
else
    echo "   ✗ TSID greeter config NOT found"
fi

# 3. Check display manager
echo ""
echo "3. Display Manager:"
if [ -f "/etc/X11/default-display-manager" ]; then
    DM=$(cat /etc/X11/default-display-manager)
    echo "   Current: $DM"
    if [[ "$DM" == *"lightdm"* ]]; then
        echo "   ✓ Using LightDM"
    else
        echo "   ✗ NOT using LightDM"
    fi
else
    echo "   ✗ No display manager configured"
fi

# 4. Check environment variables
echo ""
echo "4. Environment Variables:"
echo "   DISPLAY: ${DISPLAY:-'NOT SET'}"
echo "   XAUTHORITY: ${XAUTHORITY:-'NOT SET'}"

# 5. Check running processes
echo ""
echo "5. Running Processes:"
if pgrep -f "tsid-qr-display" > /dev/null; then
    echo "   ✓ tsid-qr-display running"
    ps aux | grep tsid-qr-display | grep -v grep
else
    echo "   ✗ tsid-qr-display NOT running"
fi

if pgrep -f "lightdm" > /dev/null; then
    echo "   ✓ LightDM running"
else
    echo "   ✗ LightDM NOT running"
fi

# 6. Check TSID configuration
echo ""
echo "6. TSID Configuration:"
if [ -f "/etc/tsid/config.conf" ]; then
    echo "   ✓ Config file exists"
    echo "   Server code: $(cat /etc/tsid/server_code 2>/dev/null || echo 'NOT SET')"
else
    echo "   ✗ Config file NOT found"
fi

# 7. Check recent logs
echo ""
echo "7. Recent PAM Logs:"
sudo tail -20 /var/log/auth.log | grep pam_tsid || echo "   No PAM logs found"

echo ""
echo "8. Recent LightDM Logs:"
sudo tail -20 /var/log/lightdm/seat0-greeter.log 2>/dev/null || echo "   No LightDM greeter logs found"

echo ""
echo "9. Test QR Display (Manual):"
echo "   Testing with sample QR..."
if command -v tsid-qr-display &>/dev/null; then
    timeout 5 tsid-qr-display --show-qr "https://example.com" --state-code "test" 2>&1 || echo "   QR display test failed"
else
    echo "   tsid-qr-display command not found"
fi

echo ""
echo "======================================"
echo "Debug Complete"
echo "======================================"
echo ""
echo "If you see issues above, try:"
echo "1. sudo systemctl restart lightdm"
echo "2. sudo dpkg-reconfigure lightdm"
echo "3. Check X11 is working: startx"
