update.sh
#!/bin/bash
# Safe update script for gork-bot
# Preserves state files while pulling latest code from GitHub

echo "🤖 Gork Bot Safe Update Script"
echo "================================"

# Navigate to bot directory
cd "$(dirname "$0")"

# Color codes for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# Files to preserve during update
STATE_FILES=(
    "processed_uris.txt"
    "thread_replies.json"
    ".env"
    "blacklist.csv"
)

echo -e "${YELLOW}📋 Backing up state files...${NC}"

# Create backup directory with timestamp
BACKUP_DIR="backups/$(date +%Y%m%d_%H%M%S)"
mkdir -p "$BACKUP_DIR"

# Backup each state file
for file in "${STATE_FILES[@]}"; do
    if [ -f "$file" ]; then
        cp "$file" "$BACKUP_DIR/"
        echo "  ✓ Backed up: $file"
    else
        echo "  ⚠ Skipped (not found): $file"
    fi
done

echo -e "${YELLOW}🛑 Stopping gork-bot service...${NC}"
sudo systemctl stop gork-bot 2>/dev/null || echo "  ⚠ Service not running or no systemctl access"

echo -e "${YELLOW}📥 Fetching latest code from GitHub...${NC}"

# Stash any local changes
git stash push -m "Auto-stash before update $(date +%Y%m%d_%H%M%S)"

# Pull latest code
if git pull origin main; then
    echo -e "${GREEN}✓ Successfully pulled latest code${NC}"
else
    echo -e "${RED}✗ Failed to pull from GitHub${NC}"
    echo -e "${YELLOW}Restoring state files...${NC}"
    for file in "${STATE_FILES[@]}"; do
        if [ -f "$BACKUP_DIR/$file" ]; then
            cp "$BACKUP_DIR/$file" .
        fi
    done
    exit 1
fi

echo -e "${YELLOW}♻️  Restoring state files...${NC}"

# Restore state files from backup
for file in "${STATE_FILES[@]}"; do
    if [ -f "$BACKUP_DIR/$file" ]; then
        cp "$BACKUP_DIR/$file" .
        echo "  ✓ Restored: $file"
    fi
done

echo -e "${YELLOW}🚀 Starting gork-bot service...${NC}"
sudo systemctl start gork-bot 2>/dev/null || echo "  ⚠ Could not start service (use: nohup python3 bot.py &)"

echo ""
echo -e "${GREEN}✅ Update complete!${NC}"
echo ""
echo "📊 Check status with: sudo systemctl status gork-bot"
echo "📜 View logs with: sudo journalctl -u gork-bot -f"
echo "💾 Backup saved to: $BACKUP_DIR"
echo ""
