#!/bin/bash
# Setup script for external logback-spring.xml on VPS
# This script extracts logback-spring.xml from the JAR and places it in the config directory
# MUST be run before starting Docker containers to create the required files

set -e

# Determine config directory based on current location
if [ -d "/opt/app/backend" ]; then
    CONFIG_DIR="/opt/app/backend/config"
    LOG_DIR="/opt/app/logs"
elif [ -d "/opt/app/backend/honey-be" ]; then
    CONFIG_DIR="/opt/app/backend/honey-be/config"
    LOG_DIR="/opt/app/logs"
else
    # Try to find from current directory
    SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
    BACKEND_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
    CONFIG_DIR="$BACKEND_DIR/config"
    LOG_DIR="/opt/app/logs"
fi

echo "Setting up external logging configuration..."
echo "Config directory: $CONFIG_DIR"
echo "Log directory: $LOG_DIR"

# Create config directory if it doesn't exist
mkdir -p "$CONFIG_DIR"
chmod 755 "$CONFIG_DIR"

# Create log directory if it doesn't exist
mkdir -p "$LOG_DIR"
chmod 755 "$LOG_DIR"

# Extract logback-spring.xml from JAR if it doesn't exist
if [ ! -f "$CONFIG_DIR/logback-spring.xml" ]; then
    echo "Extracting logback-spring.xml from JAR..."
    
    # Try multiple locations for JAR file
    JAR_PATH=""
    for search_path in "/opt/app/backend" "/opt/app/backend/honey-be" "$(dirname "$CONFIG_DIR")" "$(dirname "$(dirname "$CONFIG_DIR")")"; do
        if [ -d "$search_path" ]; then
            found_jar=$(find "$search_path" -name "honey-be-*.jar" -type f 2>/dev/null | head -n 1)
            if [ -n "$found_jar" ]; then
                JAR_PATH="$found_jar"
                break
            fi
        fi
    done
    
    # Try to find in target directory
    if [ -z "$JAR_PATH" ]; then
        for search_path in "/opt/app/backend" "/opt/app/backend/honey-be" "$(dirname "$CONFIG_DIR")"; do
            if [ -d "$search_path/target" ]; then
                found_jar=$(find "$search_path/target" -name "*.jar" -type f | head -n 1)
                if [ -n "$found_jar" ]; then
                    JAR_PATH="$found_jar"
                    break
                fi
            fi
        done
    fi
    
    if [ -z "$JAR_PATH" ]; then
        echo "Warning: JAR file not found. Trying to copy from source..."
        # If JAR not found, copy from source (if available)
        for search_path in "/opt/app/backend" "/opt/app/backend/honey-be" "$(dirname "$CONFIG_DIR")"; do
            if [ -f "$search_path/src/main/resources/logback-spring.xml" ]; then
                cp "$search_path/src/main/resources/logback-spring.xml" "$CONFIG_DIR/logback-spring.xml"
                echo "Copied from source: $search_path/src/main/resources/logback-spring.xml"
                break
            fi
        done
        
        if [ ! -f "$CONFIG_DIR/logback-spring.xml" ]; then
            echo "Error: Cannot find logback-spring.xml in JAR or source."
            echo "Please ensure the file exists or copy it manually to: $CONFIG_DIR/logback-spring.xml"
            exit 1
        fi
    else
        echo "Found JAR: $JAR_PATH"
        # Extract from JAR
        unzip -p "$JAR_PATH" BOOT-INF/classes/logback-spring.xml > "$CONFIG_DIR/logback-spring.xml" 2>/dev/null || \
        unzip -p "$JAR_PATH" logback-spring.xml > "$CONFIG_DIR/logback-spring.xml" 2>/dev/null || {
            echo "Warning: Could not extract from JAR. Trying to copy from source..."
            # Try copying from source
            for search_path in "/opt/app/backend" "/opt/app/backend/honey-be" "$(dirname "$CONFIG_DIR")"; do
                if [ -f "$search_path/src/main/resources/logback-spring.xml" ]; then
                    cp "$search_path/src/main/resources/logback-spring.xml" "$CONFIG_DIR/logback-spring.xml"
                    break
                fi
            done
            
            if [ ! -f "$CONFIG_DIR/logback-spring.xml" ]; then
                echo "Error: Cannot extract or find logback-spring.xml."
                echo "Please copy it manually to: $CONFIG_DIR/logback-spring.xml"
                exit 1
            fi
        }
        echo "Extracted from JAR: $JAR_PATH"
    fi
    
    echo "logback-spring.xml created at $CONFIG_DIR/logback-spring.xml"
else
    echo "logback-spring.xml already exists at $CONFIG_DIR/logback-spring.xml"
fi

# Set proper permissions
chmod 644 "$CONFIG_DIR/logback-spring.xml"
chown $USER:$USER "$CONFIG_DIR/logback-spring.xml" 2>/dev/null || true

echo "Logging configuration setup complete!"
echo ""
echo "Configuration file: $CONFIG_DIR/logback-spring.xml"
echo "Log directory: $LOG_DIR"
echo ""
echo "You can now edit $CONFIG_DIR/logback-spring.xml to change log levels at runtime."
echo "Changes will take effect within 30 seconds (no restart needed)."

