#!/bin/bash

# Service Configuration Loader Script
# This script copies custom service configurations from ./services/ to their
# corresponding locations on the deployment server.

set -e  # Exit on any error

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

# Function to print colored output
print_status() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

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

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

# Function to copy file with backup
copy_with_backup() {
    local source="$1"
    local destination="$2"
    
    if [[ -f "$source" ]]; then
        # Create backup if destination exists
        if [[ -f "$destination" ]]; then
            cp "$destination" "${destination}.backup.$(date +%Y%m%d_%H%M%S)"
            print_status "Created backup of existing $destination"
        fi
        
        # Create destination directory if it doesn't exist
        mkdir -p "$(dirname "$destination")"
        
        # Copy the file
        cp "$source" "$destination"
        print_status "Copied $source to $destination"
    else
        print_error "Source file $source does not exist"
        return 1
    fi
}

# Function to reload service
reload_service() {
    local service="$1"
    
    if systemctl is-active --quiet "$service"; then
        print_status "Reloading $service..."
        systemctl reload "$service"
        print_status "$service reloaded successfully"
    else
        print_warning "$service is not running, skipping reload"
    fi
}

# Main execution
main() {
    print_status "Starting service configuration deployment..."
    
    # Get the script directory and project root
    SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
    PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
    SERVICES_DIR="$PROJECT_ROOT/services"
    
    if [[ ! -d "$SERVICES_DIR" ]]; then
        print_error "Services directory not found: $SERVICES_DIR"
        exit 1
    fi
    
    # Apache PHP configurations
    if [[ -d "$SERVICES_DIR/apache-php" ]]; then
        print_status "Processing Apache PHP configurations..."
        
        # Apache site configuration
        APACHE_SITE_SOURCE="$SERVICES_DIR/apache-php/etc/apache2/sites-enabled/cstv.conf"
        APACHE_SITE_DEST="/etc/apache2/sites-enabled/cstv.conf"
        
        if [[ -f "$APACHE_SITE_SOURCE" ]]; then
            copy_with_backup "$APACHE_SITE_SOURCE" "$APACHE_SITE_DEST"
            
            # Enable the site
            a2ensite cstv.conf 2>/dev/null || print_warning "Could not enable cstv site (may already be enabled)"
        fi
        
        # PHP configuration
        PHP_CONFIG_SOURCE="$SERVICES_DIR/apache-php/etc/php/8.3/apache2/conf.d/99-custom.ini"
        PHP_CONFIG_DEST="/etc/php/8.3/apache2/conf.d/99-custom.ini"
        
        if [[ -f "$PHP_CONFIG_SOURCE" ]]; then
            copy_with_backup "$PHP_CONFIG_SOURCE" "$PHP_CONFIG_DEST"
        fi
        
        # Reload Apache
        reload_service "apache2"
    fi
    
    # Add more service configurations here as needed
    # Example for other services:
    # if [[ -d "$SERVICES_DIR/nginx" ]]; then
    #     print_status "Processing Nginx configurations..."
    #     # Copy nginx configs
    #     reload_service "nginx"
    # fi
    
    print_status "Service configuration deployment completed successfully!"
}

# Check if running as root (required for system file operations)
if [[ $EUID -ne 0 ]]; then
    print_error "This script must be run as root (use sudo)"
    exit 1
fi

# Run main function
main "$@"
