#!/bin/bash
# Inspect local Nuke databases for telemetry data

echo "=== Nuke Local Data Inspection ==="
echo ""

# Find all databases
echo "1. Finding all database files..."
find ~/Documents/nuke ~/.nuke ~/.cache -name "*.db" -o -name "*.sqlite" 2>/dev/null | while read dbfile; do
    echo "  Found: $dbfile"
    ls -lh "$dbfile"
done

echo ""
echo "2. Checking sync database..."
SYNC_DB="$HOME/.nuke/.sync_8178bafde38a.db"

if [ -f "$SYNC_DB" ]; then
    echo "  File: $SYNC_DB"
    echo "  Size: $(du -h "$SYNC_DB" | cut -f1)"
    echo "  Type: $(file "$SYNC_DB")"
    echo ""

    # Check if it's a SQLite database
    if file "$SYNC_DB" | grep -q SQLite; then
        echo "  It's a SQLite database. Inspecting tables..."
        sqlite3 "$SYNC_DB" ".tables" 2>&1
        echo ""
        echo "  Database schema:"
        sqlite3 "$SYNC_DB" ".schema" 2>&1 | head -50
    else
        echo "  Not a SQLite database. Checking for readable strings..."
        strings "$SYNC_DB" | grep -iE 'email|domain|location|telemetry|analytics|foundry|honeycomb|sentry' | head -20
    fi
else
    echo "  Sync database not found at expected location"
fi

echo ""
echo "3. Checking for Sentry database..."
find ~/Documents/nuke ~/.nuke -path "*sentry-db*" -ls 2>/dev/null

echo ""
echo "4. Checking Nuke config files for telemetry settings..."
find ~/Documents/nuke ~/.nuke -name "*.prefs" -o -name "*.xml" -o -name "*config*" 2>/dev/null | while read config; do
    echo "  Checking: $config"
    grep -iE 'telemetry|analytics|sentry|honeycomb|crash|report' "$config" 2>/dev/null && echo "    ^ FOUND TELEMETRY REFERENCE"
done

echo ""
echo "5. Recent Nuke cache files..."
find ~/.cache -name "*nuke*" -o -name "*foundry*" 2>/dev/null | head -10

echo ""
echo "=== Inspection Complete ==="
