#!/usr/bin/env bash
# Scan for software licenses on macOS
# Following prime directive: thoughtful discovery of existing licenses

set -euo pipefail

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

echo -e "${BLUE}🔍 Scanning for software licenses...${NC}"
echo ""

# Function to check if file contains license info
check_license_file() {
    local file="$1"
    if [[ -f "$file" ]] && [[ -r "$file" ]]; then
        # Check if file contains license-like content
        if grep -qiE "(license|serial|registration|key:|product)" "$file" 2>/dev/null; then
            return 0
        fi
    fi
    return 1
}

# 1. Check common application support directories
echo -e "${YELLOW}📁 Checking Application Support...${NC}"
find "$HOME/Library/Application Support" -maxdepth 2 \( \
    -name "*.license" -o \
    -name "*.lic" -o \
    -name "*License*" -o \
    -name "registration" -o \
    -name "serial*" \
    \) -type f 2>/dev/null | while read -r file; do
    echo "  Found: ${file#$HOME/Library/Application Support/}"
done

# 2. Check preferences for license plists
echo -e "${YELLOW}📁 Checking Preferences...${NC}"
find "$HOME/Library/Preferences" -name "*.plist" -type f 2>/dev/null | while read -r plist; do
    # Check if plist contains license info
    if plutil -p "$plist" 2>/dev/null | grep -qiE "(license|serial|registration)" 2>/dev/null; then
        app_name=$(basename "$plist" .plist)
        echo "  Potential license in: $app_name"
    fi
done

# 3. Check home directory for common license files
echo -e "${YELLOW}📁 Checking home directory...${NC}"
find "$HOME" -maxdepth 2 \( \
    -name ".vuescanrc" -o \
    -name "*.license" -o \
    -name "*.lic" -o \
    -name "*license.txt" \
    \) -type f 2>/dev/null | while read -r file; do
    echo "  Found: ${file#$HOME/}"
done

# 4. Check for apps that commonly store licenses
echo -e "${YELLOW}📱 Checking known applications...${NC}"

# BBEdit
if [[ -d "$HOME/Library/Application Support/BBEdit" ]]; then
    if ls "$HOME/Library/Application Support/BBEdit/"*.license 2>/dev/null | grep -q .; then
        echo "  ✓ BBEdit (license file found)"
    elif defaults read com.barebones.bbedit 2>/dev/null | grep -q "Serial" 2>/dev/null; then
        echo "  ✓ BBEdit (registration in preferences)"
    fi
fi

# Marked 2
if defaults read com.brettterpstra.marked2 2>/dev/null | grep -qE "(license|registration)" 2>/dev/null; then
    echo "  ✓ Marked 2 (registration in preferences)"
fi

# Alfred
if [[ -f "$HOME/Library/Preferences/com.runningwithcrayons.Alfred-Preferences.plist" ]]; then
    echo "  ✓ Alfred (check Powerpack in app)"
fi

# Bartender
if [[ -f "$HOME/Library/Preferences/com.surteesstudios.Bartender.plist" ]]; then
    echo "  ✓ Bartender (check registration in app)"
fi

# Adobe (Creative Cloud apps often store licenses differently)
if [[ -d "/Library/Application Support/Adobe" ]]; then
    echo "  ✓ Adobe Creative Cloud (managed by Creative Cloud app)"
fi

# 5. List all installed applications for manual check
echo ""
echo -e "${YELLOW}📋 Installed applications to check manually:${NC}"
echo "  Run each app and check Help → License/Registration"
echo ""

# List non-Apple apps
ls /Applications/*.app 2>/dev/null | grep -v "^/Applications/.*\.app$" | while read -r app; do
    app_name=$(basename "$app" .app)
    # Skip Apple apps
    if [[ ! "$app_name" =~ ^(App Store|Automator|Books|Calculator|Calendar|Chess|Contacts|Dictionary|FaceTime|Finder|Font Book|Home|Image Capture|Launchpad|Mail|Maps|Messages|Mission Control|Music|News|Notes|Photo Booth|Photos|Podcasts|Preview|QuickTime Player|Reminders|Safari|Shortcuts|Stickies|Stocks|System Information|System Preferences|System Settings|TextEdit|Time Machine|TV|Voice Memos|Xcode)$ ]]; then
        echo "  - $app_name"
    fi
done | sort -u

echo ""
echo -e "${GREEN}💡 Tips:${NC}"
echo "  1. Add found licenses to pass: new-license 'AppName'"
echo "  2. Check email for license keys (search for 'license' or 'registration')"
echo "  3. Look in your password manager for stored licenses"
echo "  4. Check purchase confirmations in email"