#!/bin/bash

# Crontab Recovery Script
# This script fixes the crontab merging issue by restoring system-specific entries

set -e

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

SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
HOSTNAME=$(hostname)

log_message() {
    echo -e "$(date '+%H:%M:%S') $1"
}

log_error() {
    log_message "${RED}ERROR: $1${NC}"
}

log_success() {
    log_message "${GREEN}SUCCESS: $1${NC}"
}

log_warning() {
    log_message "${YELLOW}WARNING: $1${NC}"
}

log_info() {
    log_message "${BLUE}INFO: $1${NC}"
}

show_current_problem() {
    log_info "Current crontab merging issue analysis:"
    echo
    
    log_info "Current root crontab on $HOSTNAME:"
    sudo crontab -l 2>/dev/null || log_warning "No crontab found"
    echo
    
    case "$HOSTNAME" in
        "europa")
            log_info "Europa should have:"
            echo "  - move-backups.sh (pulls files FROM racknerd)"
            echo "  - backup-plex.sh (backs up Plex)"
            echo "  - validate-plex-backups.sh (validates backups)"
            echo "  - Immich database backup move"
            echo "  - Weekly Plex reports"
            ;;
        "racknerd")
            log_info "Racknerd should have:"
            echo "  - backup-docker.sh (backs up Docker containers)"
            echo "  - NO move-backups.sh (that's Europa's job)"
            echo "  - NO Plex-related jobs (Plex runs on Europa)"
            ;;
        *)
            log_warning "Unknown hostname: $HOSTNAME"
            ;;
    esac
    echo
}

fix_crontab() {
    local system_name="$1"
    local crontab_file="$SCRIPT_DIR/crontab-${system_name}.txt"
    
    if [ ! -f "$crontab_file" ]; then
        log_error "System-specific crontab file not found: $crontab_file"
        return 1
    fi
    
    log_info "Creating backup before fixing crontab"
    if [ -f "$SCRIPT_DIR/crontab-backup-system.sh" ]; then
        "$SCRIPT_DIR/crontab-backup-system.sh" backup "pre-fix"
    fi
    
    log_info "Installing correct crontab for $system_name"
    
    # Extract just the cron entries (skip comments and empty lines)
    grep -E '^[0-9]' "$crontab_file" > /tmp/cron_entries_fix.txt
    
    if sudo crontab /tmp/cron_entries_fix.txt; then
        log_success "Correct crontab installed for $system_name"
        rm -f /tmp/cron_entries_fix.txt
        
        # Create a post-fix backup
        if [ -f "$SCRIPT_DIR/crontab-backup-system.sh" ]; then
            "$SCRIPT_DIR/crontab-backup-system.sh" backup "post-fix"
        fi
        
        log_info "New crontab contents:"
        sudo crontab -l
        
        return 0
    else
        log_error "Failed to install correct crontab"
        rm -f /tmp/cron_entries_fix.txt
        return 1
    fi
}

main() {
    log_info "=== Crontab Recovery Script ==="
    log_info "System: $HOSTNAME"
    echo
    
    show_current_problem
    
    case "$HOSTNAME" in
        "europa")
            log_info "Fixing crontab for Europa (media server)"
            fix_crontab "europa"
            ;;
        "racknerd")
            log_info "Fixing crontab for Racknerd (backup server)"
            fix_crontab "racknerd"
            ;;
        *)
            log_error "Unknown system: $HOSTNAME"
            log_info "This script supports: europa, racknerd"
            exit 1
            ;;
    esac
    
    echo
    log_success "Crontab recovery completed for $HOSTNAME"
    log_info "The management script now uses system-specific files:"
    log_info "  - crontab-europa.txt"
    log_info "  - crontab-racknerd.txt"
    echo
    log_info "To manage crontabs going forward, use:"
    log_info "  ./manage-enhanced-crontab.sh install"
}

main "$@"
