#!/bin/bash

# Individual host backup script
# Runs backup for each host one by one with detailed output

set -uo pipefail

# Load passwords
source secrets/ssh_passwords.env

# Configuration
BACKUP_TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="/export/omv800_backup/pre_migration_${BACKUP_TIMESTAMP}"

echo "=== INDIVIDUAL HOST BACKUP SCRIPT ==="
echo "Backup directory: $BACKUP_DIR"
echo "Timestamp: $BACKUP_TIMESTAMP"
echo

# Create backup directory
ssh jon@raspberrypi "mkdir -p $BACKUP_DIR"

# Function to backup a single host
backup_host() {
    local host="$1"
    local user="$2"
    
    echo "🔄 BACKING UP: $host (user: $user)"
    echo "=================================="
    
    # Get password for this host
    case "$host" in
        "fedora")
            password="$FEDORA_PASSWORD"
            ;;
        "lenovo")
            password="$LENOVO_PASSWORD"
            ;;
        "lenovo420")
            password="$LENOVO420_PASSWORD"
            ;;
        "omv800")
            password="$OMV800_PASSWORD"
            ;;
        "surface")
            password="$SURFACE_PASSWORD"
            ;;
        "audrey")
            password="$AUDREY_PASSWORD"
            ;;
        "raspberrypi")
            password="$RASPBERRYPI_PASSWORD"
            ;;
        *)
            echo "❌ No password configured for $host"
            return 1
            ;;
    esac
    
    # Test connectivity first
    echo "   Testing connectivity..."
    if sshpass -p "$password" ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$user@$host" "echo 'Connection test successful'" 2>/dev/null; then
        echo "✅ Connectivity: SUCCESS"
    else
        echo "❌ Connectivity: FAILED"
        return 1
    fi
    
    # Create host-specific directories
    ssh jon@raspberrypi "mkdir -p $BACKUP_DIR/configurations $BACKUP_DIR/secrets $BACKUP_DIR/user_data $BACKUP_DIR/system_configs"
    
    # Backup configurations
    echo "   Backing up configurations..."
    if sshpass -p "$password" ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$user@$host" "tar czf /tmp/config_backup.tar.gz -C /etc . -C /home . 2>/dev/null || true" 2>/dev/null; then
        if sshpass -p "$password" ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$user@$host" "test -f /tmp/config_backup.tar.gz" 2>/dev/null; then
            scp "$user@$host:/tmp/config_backup.tar.gz" "/tmp/config_backup_temp.tar.gz" 2>/dev/null || true
            if [[ -f "/tmp/config_backup_temp.tar.gz" ]]; then
                rsync -avz "/tmp/config_backup_temp.tar.gz" "jon@raspberrypi:$BACKUP_DIR/configurations/${host}_configs_${BACKUP_TIMESTAMP}.tar.gz"
                local size=$(stat -c%s "/tmp/config_backup_temp.tar.gz" 2>/dev/null || echo "0")
                echo "✅ Configs: SUCCESS ($size bytes)"
                rm -f "/tmp/config_backup_temp.tar.gz" 2>/dev/null || true
                sshpass -p "$password" ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$user@$host" "rm -f /tmp/config_backup.tar.gz" 2>/dev/null || true
            else
                echo "❌ Configs: Failed to copy"
            fi
        else
            echo "❌ Configs: Failed to create"
        fi
    else
        echo "❌ Configs: Failed to connect"
    fi
    
    # Backup secrets
    echo "   Backing up secrets..."
    if sshpass -p "$password" ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$user@$host" "tar czf /tmp/secrets_backup.tar.gz -C /etc/ssl . -C /etc/letsencrypt . 2>/dev/null || true" 2>/dev/null; then
        if sshpass -p "$password" ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$user@$host" "test -f /tmp/secrets_backup.tar.gz" 2>/dev/null; then
            scp "$user@$host:/tmp/secrets_backup.tar.gz" "/tmp/secrets_backup_temp.tar.gz" 2>/dev/null || true
            if [[ -f "/tmp/secrets_backup_temp.tar.gz" ]]; then
                rsync -avz "/tmp/secrets_backup_temp.tar.gz" "jon@raspberrypi:$BACKUP_DIR/secrets/${host}_secrets_${BACKUP_TIMESTAMP}.tar.gz"
                local size=$(stat -c%s "/tmp/secrets_backup_temp.tar.gz" 2>/dev/null || echo "0")
                echo "✅ Secrets: SUCCESS ($size bytes)"
                rm -f "/tmp/secrets_backup_temp.tar.gz" 2>/dev/null || true
                sshpass -p "$password" ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$user@$host" "rm -f /tmp/secrets_backup.tar.gz" 2>/dev/null || true
            else
                echo "❌ Secrets: Failed to copy"
            fi
        else
            echo "❌ Secrets: Failed to create"
        fi
    else
        echo "❌ Secrets: Failed to connect"
    fi
    
    # Backup user data
    echo "   Backing up user data..."
    if sshpass -p "$password" ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$user@$host" "tar czf /tmp/user_data_backup.tar.gz --exclude='*/node_modules' --exclude='*/.git' --exclude='*/Downloads' --exclude='*/Videos' --exclude='*/Music' -C /home . -C /srv . 2>/dev/null || true" 2>/dev/null; then
        if sshpass -p "$password" ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$user@$host" "test -f /tmp/user_data_backup.tar.gz" 2>/dev/null; then
            scp "$user@$host:/tmp/user_data_backup.tar.gz" "/tmp/user_data_backup_temp.tar.gz" 2>/dev/null || true
            if [[ -f "/tmp/user_data_backup_temp.tar.gz" ]]; then
                rsync -avz "/tmp/user_data_backup_temp.tar.gz" "jon@raspberrypi:$BACKUP_DIR/user_data/${host}_user_data_${BACKUP_TIMESTAMP}.tar.gz"
                local size=$(stat -c%s "/tmp/user_data_backup_temp.tar.gz" 2>/dev/null || echo "0")
                echo "✅ User Data: SUCCESS ($size bytes)"
                rm -f "/tmp/user_data_backup_temp.tar.gz" 2>/dev/null || true
                sshpass -p "$password" ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$user@$host" "rm -f /tmp/user_data_backup.tar.gz" 2>/dev/null || true
            else
                echo "❌ User Data: Failed to copy"
            fi
        else
            echo "❌ User Data: Failed to create"
        fi
    else
        echo "❌ User Data: Failed to connect"
    fi
    
    # Backup system configs
    echo "   Backing up system configs..."
    if sshpass -p "$password" ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$user@$host" "tar czf /tmp/system_configs_backup.tar.gz -C /etc/systemd . -C /etc/network . -C /etc/docker . 2>/dev/null || true" 2>/dev/null; then
        if sshpass -p "$password" ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$user@$host" "test -f /tmp/system_configs_backup.tar.gz" 2>/dev/null; then
            scp "$user@$host:/tmp/system_configs_backup.tar.gz" "/tmp/system_configs_backup_temp.tar.gz" 2>/dev/null || true
            if [[ -f "/tmp/system_configs_backup_temp.tar.gz" ]]; then
                rsync -avz "/tmp/system_configs_backup_temp.tar.gz" "jon@raspberrypi:$BACKUP_DIR/system_configs/${host}_system_configs_${BACKUP_TIMESTAMP}.tar.gz"
                local size=$(stat -c%s "/tmp/system_configs_backup_temp.tar.gz" 2>/dev/null || echo "0")
                echo "✅ System Configs: SUCCESS ($size bytes)"
                rm -f "/tmp/system_configs_backup_temp.tar.gz" 2>/dev/null || true
                sshpass -p "$password" ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$user@$host" "rm -f /tmp/system_configs_backup.tar.gz" 2>/dev/null || true
            else
                echo "❌ System Configs: Failed to copy"
            fi
        else
            echo "❌ System Configs: Failed to create"
        fi
    else
        echo "❌ System Configs: Failed to connect"
    fi
    
    echo "✅ COMPLETED: $host"
    echo "=================================="
    echo
}

# Backup each accessible host
echo "Starting individual host backups..."
echo

# Test each host from all_hosts.txt
while IFS=: read -r host user; do
    if [[ -z "$host" || "$host" == "localhost" ]]; then
        continue
    fi
    
    # Skip omvbackup (DNS resolution issue)
    if [[ "$host" == "omvbackup" ]]; then
        echo "⏭️  SKIPPING: $host (DNS resolution issue)"
        echo
        continue
    fi
    
    # Backup this host
    backup_host "$host" "$user"
    
done < "comprehensive_discovery_results/all_hosts.txt"

echo "=== INDIVIDUAL HOST BACKUP COMPLETE ==="
echo "Backup directory: $BACKUP_DIR"
echo "Check the backup results with: ssh jon@raspberrypi 'ls -la $BACKUP_DIR/'"
