#!/bin/bash

###############################################################################
# Migration Script: Move to /opt/migration/ Structure
# Purpose: Reorganize migration tools to standard Linux FHS structure
###############################################################################

set -e

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

log() {
    echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
}

warn() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Check if running as root
if [ "$EUID" -ne 0 ]; then 
    error "Please run as root (use sudo)"
    exit 1
fi

CURRENT_DIR="/var/www/html/wordpress6/wordpress/EP/migration"
NEW_BASE="/opt/migration"
NEW_SCRIPTS="${NEW_BASE}/scripts"
NEW_DOCS="${NEW_BASE}/docs"
NEW_LOGS="${NEW_BASE}/logs"
NEW_TEMPLATES="${NEW_BASE}/templates"

log "Starting migration to /opt/migration/ structure..."

# Check if current directory exists
if [ ! -d "$CURRENT_DIR" ]; then
    error "Current directory not found: $CURRENT_DIR"
    exit 1
fi

# Create new directory structure
log "Creating new directory structure..."
mkdir -p "$NEW_SCRIPTS"
mkdir -p "$NEW_DOCS"
mkdir -p "$NEW_LOGS"
mkdir -p "$NEW_TEMPLATES"

# Move scripts
log "Moving scripts..."
if ls "$CURRENT_DIR"/*.sh 1> /dev/null 2>&1; then
    mv "$CURRENT_DIR"/*.sh "$NEW_SCRIPTS/"
    log "✓ Scripts moved to $NEW_SCRIPTS"
else
    warn "No .sh files found to move"
fi

# Move documentation
log "Moving documentation..."
if ls "$CURRENT_DIR"/*.md 1> /dev/null 2>&1; then
    mv "$CURRENT_DIR"/*.md "$NEW_DOCS/"
    log "✓ Documentation moved to $NEW_DOCS"
else
    warn "No .md files found to move"
fi

# Move templates/config files
log "Moving templates..."
if ls "$CURRENT_DIR"/*.conf 1> /dev/null 2>&1; then
    mv "$CURRENT_DIR"/*.conf "$NEW_TEMPLATES/" 2>/dev/null || true
fi
if ls "$CURRENT_DIR"/*.template 1> /dev/null 2>&1; then
    mv "$CURRENT_DIR"/*.template "$NEW_TEMPLATES/" 2>/dev/null || true
fi
log "✓ Templates moved to $NEW_TEMPLATES"

# Move any log files
log "Moving log files..."
if ls "$CURRENT_DIR"/*.log 1> /dev/null 2>&1; then
    mv "$CURRENT_DIR"/*.log "$NEW_LOGS/" 2>/dev/null || true
    log "✓ Logs moved to $NEW_LOGS"
fi

# Set permissions
log "Setting permissions..."
chown -R root:root "$NEW_BASE"
chmod -R 755 "$NEW_BASE"
chmod +x "$NEW_SCRIPTS"/*.sh 2>/dev/null || true
log "✓ Permissions set"

# Update script references
log "Updating script references..."
find "$NEW_SCRIPTS" -type f -name "*.sh" -exec sed -i "s|$CURRENT_DIR|$NEW_SCRIPTS|g" {} \;
find "$NEW_SCRIPTS" -type f -name "*.sh" -exec sed -i "s|/var/www/html/wordpress6/wordpress/EP/migration|$NEW_SCRIPTS|g" {} \;

# Update documentation references
log "Updating documentation references..."
find "$NEW_DOCS" -type f -name "*.md" -exec sed -i "s|$CURRENT_DIR|$NEW_SCRIPTS|g" {} \;
find "$NEW_DOCS" -type f -name "*.md" -exec sed -i "s|/var/www/html/wordpress6/wordpress/EP/migration|$NEW_SCRIPTS|g" {} \;

log "✓ References updated"

# Create symlink for backward compatibility (optional)
log "Creating backward compatibility symlink..."
if [ -d "$CURRENT_DIR" ] && [ -z "$(ls -A "$CURRENT_DIR" 2>/dev/null)" ]; then
    rmdir "$CURRENT_DIR" 2>/dev/null || true
    ln -s "$NEW_BASE" "$CURRENT_DIR"
    log "✓ Symlink created: $CURRENT_DIR -> $NEW_BASE"
elif [ ! -e "$CURRENT_DIR" ]; then
    ln -s "$NEW_BASE" "$CURRENT_DIR"
    log "✓ Symlink created: $CURRENT_DIR -> $NEW_BASE"
else
    warn "Could not create symlink - directory not empty or doesn't exist"
fi

# Test scripts
log "Testing scripts..."
if [ -f "$NEW_SCRIPTS/inventory-applications.sh" ]; then
    if bash -n "$NEW_SCRIPTS/inventory-applications.sh" 2>/dev/null; then
        log "✓ Scripts syntax check passed"
    else
        warn "Some scripts may have syntax issues"
    fi
fi

log "=========================================="
log "Migration complete!"
log "=========================================="
log ""
log "New structure:"
log "  Scripts:    $NEW_SCRIPTS"
log "  Docs:       $NEW_DOCS"
log "  Logs:       $NEW_LOGS"
log "  Templates:  $NEW_TEMPLATES"
log ""
log "Quick access:"
log "  cd $NEW_SCRIPTS"
log ""
log "To use scripts:"
log "  $NEW_SCRIPTS/prepare-new-server.sh"
log "  $NEW_SCRIPTS/inventory-applications.sh"
log ""
warn "Old directory: $CURRENT_DIR"
warn "If symlink was created, old path will still work"
warn "Otherwise, update any external references to use: $NEW_SCRIPTS"



