#!/bin/bash
# Content-Only Backup Script for WorkRoot Website
# Quick incremental backup of content files (blog posts, markdown)

set -e

# Configuration
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
BACKUP_DIR="$PROJECT_ROOT/backups/incremental"
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
BACKUP_FILE="$BACKUP_DIR/content-$TIMESTAMP.tar.gz"
LOG_FILE="$PROJECT_ROOT/backups/backup.log"

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

log() {
    echo "[$(date +"%Y-%m-%d %H:%M:%S")] $1" | tee -a "$LOG_FILE"
}

mkdir -p "$BACKUP_DIR"

log "${GREEN}Starting content backup...${NC}"

# Content directories to backup
CONTENT_ITEMS=(
    "src/content/"
    "public/images/"
    "public/assets/"
)

# Create backup
cd "$PROJECT_ROOT"
tar -czf "$BACKUP_FILE" "${CONTENT_ITEMS[@]}" 2>/dev/null || {
    log "${YELLOW}Warning: Some content directories may not exist${NC}"
}

# Verify backup
if [ ! -f "$BACKUP_FILE" ]; then
    log "ERROR: Content backup failed"
    exit 1
fi

BACKUP_SIZE=$(du -h "$BACKUP_FILE" | cut -f1)
log "${GREEN}Content backup completed${NC}"
log "Location: $BACKUP_FILE"
log "Size: $BACKUP_SIZE"

# Create latest symlink
ln -sf "$BACKUP_FILE" "$BACKUP_DIR/latest-content.tar.gz"

# Cleanup old incremental backups (keep last 72 hours)
find "$BACKUP_DIR" -name "content-*.tar.gz" -type f -mtime +3 -delete 2>/dev/null || true
log "Old backups cleaned up"

echo -e "${GREEN}✓ Content backup completed${NC}"
echo -e "  Location: $BACKUP_FILE"
echo -e "  Size: $BACKUP_SIZE"
