#!/bin/bash

# Fenrir X11 Clipboard Sync
# Synchronizes between X11 clipboard and Fenrir clipboard file
# Prevents loops using checksums and timestamps

# Check for root privileges
if [[ $(whoami) != "root" ]]; then
    echo "Error: This script must be run as root to access Fenrir's clipboard file"
    echo "Run with: sudo DISPLAY=:0 ./clipboard_sync.sh"
    exit 1
fi

FENRIR_CLIPBOARD_FILE="${1:-/tmp/fenrirClipboard}"
STATE_FILE="/var/tmp/.fenrir_clipboard_state"

# Simple state tracking without complex locking
get_file_checksum() {
    if [[ -f "$FENRIR_CLIPBOARD_FILE" ]]; then
        md5sum "$FENRIR_CLIPBOARD_FILE" 2>/dev/null | cut -d' ' -f1
    else
        echo ""
    fi
}

get_clipboard_checksum() {
    xclip -o -selection clipboard 2>/dev/null | md5sum | cut -d' ' -f1
}

# Initialize state
rm -f "$STATE_FILE" 2>/dev/null

echo "Starting Fenrir clipboard sync..."
echo "Monitoring file: $FENRIR_CLIPBOARD_FILE"

# Check dependencies
if ! command -v xclip >/dev/null 2>&1; then
    echo "Error: xclip is required but not installed"
    echo "Install with: sudo apt install xclip"
    exit 1
fi

if ! command -v inotifywait >/dev/null 2>&1; then
    echo "Error: inotify-tools is required but not installed"
    echo "Install with: sudo apt install inotify-tools"
    exit 1
fi

# Create clipboard file if it doesn't exist
touch "$FENRIR_CLIPBOARD_FILE"

echo "Starting Fenrir clipboard sync..."

while true; do
    # Read last state
    if [[ -f "$STATE_FILE" ]]; then
        read -r LAST_FILE_CHECKSUM LAST_CLIPBOARD_CHECKSUM LAST_UPDATE_TIME < "$STATE_FILE"
    else
        LAST_FILE_CHECKSUM=""
        LAST_CLIPBOARD_CHECKSUM=""
        LAST_UPDATE_TIME="0"
    fi
    
    # Get current checksums
    CURRENT_FILE_CHECKSUM=$(get_file_checksum)
    CURRENT_CLIPBOARD_CHECKSUM=$(get_clipboard_checksum)
    CURRENT_TIME=$(date +%s)
    
    # Skip update if we just made one (prevent immediate loops)
    TIME_SINCE_LAST=$((CURRENT_TIME - LAST_UPDATE_TIME))
    if [[ $TIME_SINCE_LAST -lt 3 ]]; then
        sleep 1
        continue
    fi
    
    # Clipboard changed
    if [[ "$CURRENT_CLIPBOARD_CHECKSUM" != "$LAST_CLIPBOARD_CHECKSUM" ]]; then
        echo "X11 clipboard changed, updating file..."
        if xclip -o -selection clipboard > "$FENRIR_CLIPBOARD_FILE" 2>/dev/null; then
            CURRENT_FILE_CHECKSUM=$(get_file_checksum)
            echo "$CURRENT_FILE_CHECKSUM $CURRENT_CLIPBOARD_CHECKSUM $CURRENT_TIME" > "$STATE_FILE"
            echo "File updated successfully"
        else
            echo "Failed to update file from clipboard"
        fi
        sleep 1
        continue
    fi
    
    # File changed
    if [[ "$CURRENT_FILE_CHECKSUM" != "$LAST_FILE_CHECKSUM" ]]; then
        echo "Fenrir clipboard file changed, updating X11 clipboard..."
        if cat "$FENRIR_CLIPBOARD_FILE" | xclip -i -selection clipboard 2>/dev/null; then
            CURRENT_CLIPBOARD_CHECKSUM=$(get_clipboard_checksum)
            echo "$CURRENT_FILE_CHECKSUM $CURRENT_CLIPBOARD_CHECKSUM $CURRENT_TIME" > "$STATE_FILE"
            echo "X11 clipboard updated successfully"
        else
            echo "Failed to update clipboard from file"
        fi
        sleep 1
        continue
    fi
    
    sleep 1
done