#!/bin/bash

echo "=== WIKI.JS DEPLOYMENT LOG RETRIEVAL ==="
echo "Date: Feb 10, 2026"
echo "Checking all logs from 8:00 PM - midnight CST"
echo ""

# Create results directory
mkdir -p ~/wiki-deployment-investigation
cd ~/wiki-deployment-investigation

echo "=== 1. SYSTEMD SERVICE LOGS ===" > all-logs.txt
echo "" >> all-logs.txt

# Check for Wiki.js services (various possible names)
for service in wiki wikijs wiki-staff wiki-subscriber wiki.js; do
    if systemctl list-units --all | grep -q "$service"; then
        echo "--- Service: $service ---" >> all-logs.txt
        journalctl -u "$service" --since "2026-02-10 20:00" --until "2026-02-10 23:59" >> all-logs.txt 2>&1
        echo "" >> all-logs.txt
    fi
done

echo "=== 2. NGINX LOGS ===" >> all-logs.txt
echo "" >> all-logs.txt
echo "--- Nginx Error Log (Feb 10 evening) ---" >> all-logs.txt
grep "Feb 10.*2[0-3]:" /var/log/nginx/error.log 2>/dev/null >> all-logs.txt || echo "No nginx error.log found" >> all-logs.txt
echo "" >> all-logs.txt

echo "--- Nginx Access Log (staff.firefrostgaming.com) ---" >> all-logs.txt
grep "staff.firefrostgaming.com" /var/log/nginx/access.log 2>/dev/null | grep "Feb 10.*2[0-3]:" >> all-logs.txt || echo "No staff.firefrostgaming.com entries" >> all-logs.txt
echo "" >> all-logs.txt

echo "--- Nginx Access Log (subscribers.firefrostgaming.com) ---" >> all-logs.txt
grep "subscribers.firefrostgaming.com" /var/log/nginx/access.log 2>/dev/null | grep "Feb 10.*2[0-3]:" >> all-logs.txt || echo "No subscribers.firefrostgaming.com entries" >> all-logs.txt
echo "" >> all-logs.txt

echo "=== 3. AUTOMATION SYSTEM LOGS ===" >> all-logs.txt
echo "" >> all-logs.txt
ls -la ~/firefrost-work/firefrost-operations-manual/automation/logs/*2026-02-10* 2>/dev/null >> all-logs.txt || echo "No automation logs from Feb 10" >> all-logs.txt
echo "" >> all-logs.txt

if ls ~/firefrost-work/firefrost-operations-manual/automation/logs/*2026-02-10* 2>/dev/null; then
    for log in ~/firefrost-work/firefrost-operations-manual/automation/logs/*2026-02-10*; do
        echo "--- Automation Log: $(basename $log) ---" >> all-logs.txt
        cat "$log" >> all-logs.txt
        echo "" >> all-logs.txt
    done
fi

echo "=== 4. GIT COMMIT HISTORY (Feb 10 evening) ===" >> all-logs.txt
echo "" >> all-logs.txt
cd ~/firefrost-work/firefrost-staff-wiki
echo "--- Staff Wiki Git Log ---" >> ~/wiki-deployment-investigation/all-logs.txt
git log --since="2026-02-10 20:00" --until="2026-02-10 23:59" --pretty=format:"%h %ad %s" --date=local >> ~/wiki-deployment-investigation/all-logs.txt
echo "" >> ~/wiki-deployment-investigation/all-logs.txt
echo "" >> ~/wiki-deployment-investigation/all-logs.txt

cd ~/firefrost-work/firefrost-operations-manual
echo "--- Operations Manual Git Log ---" >> ~/wiki-deployment-investigation/all-logs.txt
git log --since="2026-02-10 20:00" --until="2026-02-10 23:59" --pretty=format:"%h %ad %s" --date=local >> ~/wiki-deployment-investigation/all-logs.txt
echo "" >> ~/wiki-deployment-investigation/all-logs.txt

echo "=== 5. GHOST VPS PROCESSES ===" >> ~/wiki-deployment-investigation/all-logs.txt
echo "" >> ~/wiki-deployment-investigation/all-logs.txt
echo "--- Current Wiki.js processes ---" >> ~/wiki-deployment-investigation/all-logs.txt
ps aux | grep -i wiki >> ~/wiki-deployment-investigation/all-logs.txt || echo "No wiki processes found" >> ~/wiki-deployment-investigation/all-logs.txt
echo "" >> ~/wiki-deployment-investigation/all-logs.txt

echo "=== 6. BASH HISTORY (if available) ===" >> ~/wiki-deployment-investigation/all-logs.txt
echo "" >> ~/wiki-deployment-investigation/all-logs.txt
if [ -f ~/.bash_history ]; then
    echo "--- Commands from Feb 10 evening ---" >> ~/wiki-deployment-investigation/all-logs.txt
    # Note: bash_history doesn't have timestamps by default, but we'll capture it
    tail -200 ~/.bash_history >> ~/wiki-deployment-investigation/all-logs.txt
fi

echo "" >> ~/wiki-deployment-investigation/all-logs.txt
echo "=== LOG RETRIEVAL COMPLETE ===" >> ~/wiki-deployment-investigation/all-logs.txt

# Copy to results for Claude to read
cp all-logs.txt ~/firefrost-work/firefrost-operations-manual/automation/results/wiki-deployment-logs.txt

echo ""
echo "✓ All logs captured in ~/wiki-deployment-investigation/all-logs.txt"
echo "✓ Copied to automation/results for review"
echo ""
echo "Review with: cat ~/wiki-deployment-investigation/all-logs.txt"

exit 0
