#!/bin/bash

# Better signal handling - forward signals to child processes
trap 'kill -TERM $SERVER_PID; kill -TERM $NGINX_PID; exit' TERM INT

set -eu

echo "==> Starting Ente Cloudron app..."

# Create necessary directories
mkdir -p /app/data/config /app/data/storage /app/data/nginx/tmp /app/data/go /app/data/logs /run/nginx

# Add comment about Cloudron filesystem limitations
echo "==> NOTE: Running in Cloudron environment with limited write access"
echo "==> Writable directories: /app/data, /tmp, /run"

# One-time initialization tracking
if [[ ! -f /app/data/.initialized ]]; then
    echo "==> Fresh installation, setting up data directory..."
    
    echo "==> DEBUG: Full repository structure at /app/code"
    find /app/code -type d -maxdepth 3 -not -path "*/node_modules/*" -not -path "*/\.*" | sort

    echo "==> DEBUG: Looking for Go files"
    find /app/code -name "*.go" | grep -v test | sort | head -10

    echo "==> DEBUG: Looking for server-related directories"
    find /app/code -type d -path "*/server*" -o -path "*/museum*" | sort

    echo "==> DEBUG: All package.json files in repository"
    find /app/code -name "package.json" -not -path "*/node_modules/*" | sort

    echo "==> DEBUG: Looking for web app directories"
    find /app/code -type d -path "*/web*" | sort

    echo "==> DEBUG: Web app directories in /app/web (if they exist)"
    if [ -d "/app/web" ]; then
        ls -la /app/web
    else
        echo "Web app directory not yet copied to /app/web"
    fi
    
    # Create config template file on first run
    echo "==> First run - creating configuration template"
    
    # Generate random secrets
    JWT_SECRET=$(openssl rand -hex 32)
    SESSION_SECRET=$(openssl rand -hex 32)
    MASTER_KEY=$(openssl rand -hex 32)
    
    # Replace variables in template for things we know
    sed \
        -e "s|%%POSTGRESQL_HOST%%|${CLOUDRON_POSTGRESQL_HOST}|g" \
        -e "s|%%POSTGRESQL_PORT%%|${CLOUDRON_POSTGRESQL_PORT}|g" \
        -e "s|%%POSTGRESQL_USERNAME%%|${CLOUDRON_POSTGRESQL_USERNAME}|g" \
        -e "s|%%POSTGRESQL_PASSWORD%%|${CLOUDRON_POSTGRESQL_PASSWORD}|g" \
        -e "s|%%POSTGRESQL_DATABASE%%|${CLOUDRON_POSTGRESQL_DATABASE}|g" \
        -e "s|%%APP_ORIGIN%%|${CLOUDRON_APP_ORIGIN}|g" \
        -e "s|%%MAIL_SMTP_SERVER%%|${CLOUDRON_MAIL_SMTP_SERVER}|g" \
        -e "s|%%MAIL_SMTP_PORT%%|${CLOUDRON_MAIL_SMTP_PORT}|g" \
        -e "s|%%MAIL_SMTP_USERNAME%%|${CLOUDRON_MAIL_SMTP_USERNAME}|g" \
        -e "s|%%MAIL_SMTP_PASSWORD%%|${CLOUDRON_MAIL_SMTP_PASSWORD}|g" \
        -e "s|%%MAIL_FROM%%|${CLOUDRON_MAIL_FROM}|g" \
        -e "s|%%MAIL_FROM_DISPLAY_NAME%%|${CLOUDRON_MAIL_FROM_DISPLAY_NAME}|g" \
        -e "s|%%JWT_SECRET%%|${JWT_SECRET}|g" \
        -e "s|%%SESSION_SECRET%%|${SESSION_SECRET}|g" \
        -e "s|%%MASTER_KEY%%|${MASTER_KEY}|g" \
        /app/pkg/config.template.yaml > /app/data/config/config.yaml
    
    # Create an S3 configuration file template
    cat > /app/data/config/s3.env.template <<EOT
# S3 Configuration for Ente
# Fill in your S3 credentials and rename this file to s3.env

# S3 endpoint (example: https://s3.amazonaws.com)
S3_ENDPOINT=

# S3 region (example: us-east-1)
S3_REGION=

# S3 bucket name
S3_BUCKET=

# S3 access key
S3_ACCESS_KEY=

# S3 secret key
S3_SECRET_KEY=

# Prefix for objects in the bucket (optional)
S3_PREFIX=ente/
EOT

    echo "==> IMPORTANT: S3 storage configuration required"
    echo "    1. Edit the file at /app/data/config/s3.env.template"
    echo "    2. Fill in your S3 credentials"
    echo "    3. Rename the file to s3.env"
    echo "    4. Restart the app"
    
    # Create test S3 configuration for Wasabi
    echo "==> Creating S3 configuration for testing with Wasabi"
    cat > /app/data/config/s3.env <<EOT
# S3 Configuration for Ente with Wasabi
S3_ENDPOINT=https://s3.eu-central-2.wasabisys.com
S3_REGION=eu-central-2
S3_BUCKET=ente-due-ren
S3_ACCESS_KEY=RPPVSNEIYST6Y3U04NHG
S3_SECRET_KEY=aPdHB4fkvQAuJUqPhneoIDcHEHee9cvP2j0nKSly
S3_PREFIX=ente/
EOT
    echo "==> Test S3 configuration created for Wasabi"
    
    # Mark initialization as complete
    touch /app/data/.initialized
    echo "==> Initialization complete"
fi

# Check if s3.env exists
if [[ ! -f /app/data/config/s3.env ]]; then
    echo "==> ERROR: S3 configuration not found"
    echo "    Please configure S3 storage by editing /app/data/config/s3.env.template"
    echo "    and renaming it to s3.env, then restart the app."
    exit 1
fi

# Load S3 environment variables
source /app/data/config/s3.env

# Print S3 configuration (without sensitive values)
echo "==> S3 Configuration:"
echo "Endpoint: ${S3_ENDPOINT}"
echo "Region: ${S3_REGION}"
echo "Bucket: ${S3_BUCKET}"
echo "Prefix: ${S3_PREFIX:-ente/}"

# Create museum.yaml for proper S3 configuration
echo "==> Creating museum.yaml configuration"
cat > /app/data/config/museum.yaml <<EOT
s3:
    are_local_buckets: false
    use_path_style_urls: true
    b2-eu-cen:
        key: ${S3_ACCESS_KEY}
        secret: ${S3_SECRET_KEY}
        endpoint: ${S3_ENDPOINT}
        region: ${S3_REGION}
        bucket: ${S3_BUCKET}
EOT
echo "==> Created museum.yaml with S3 configuration"

# Update the config file with S3 credentials
sed -i \
    -e "s|%%S3_ENDPOINT%%|${S3_ENDPOINT}|g" \
    -e "s|%%S3_REGION%%|${S3_REGION}|g" \
    -e "s|%%S3_BUCKET%%|${S3_BUCKET}|g" \
    -e "s|%%S3_ACCESS_KEY%%|${S3_ACCESS_KEY}|g" \
    -e "s|%%S3_SECRET_KEY%%|${S3_SECRET_KEY}|g" \
    -e "s|%%S3_PREFIX%%|${S3_PREFIX:-ente/}|g" \
    /app/data/config/config.yaml

# Set storage type to S3 in config
sed -i 's|storage.type: "local"|storage.type: "s3"|g' /app/data/config/config.yaml
sed -i 's|s3.are_local_buckets: true|s3.are_local_buckets: false|g' /app/data/config/config.yaml

# Install or verify required packages
echo "==> Checking for required packages"
if ! command -v nginx &> /dev/null; then
    echo "==> Installing NGINX"
    apt-get update && apt-get install -y nginx
fi

# Set up NGINX to serve the web apps and proxy to the Museum server
echo "==> Setting up NGINX for web apps and API"

# Create a custom NGINX configuration in the writable data directory
cat > /app/data/nginx/ente.conf <<EOT
worker_processes 1;
error_log stderr;
daemon off;
pid /app/data/nginx/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    access_log /dev/stdout combined;
    sendfile on;
    keepalive_timeout 65;
    
    # Define temp file paths
    client_body_temp_path /app/data/nginx/tmp;
    proxy_temp_path /app/data/nginx/tmp;
    fastcgi_temp_path /app/data/nginx/tmp;
    uwsgi_temp_path /app/data/nginx/tmp;
    scgi_temp_path /app/data/nginx/tmp;
    
    server {
        listen 8080;
        
        # Photos app - root path
        location / {
            root /app/web/photos;
            try_files \$uri \$uri/ /index.html;
        }
        
        # Accounts app
        location /accounts/ {
            alias /app/web/accounts/;
            try_files \$uri \$uri/ /accounts/index.html;
        }
        
        # Auth app
        location /auth/ {
            alias /app/web/auth/;
            try_files \$uri \$uri/ /auth/index.html;
        }
        
        # Cast app
        location /cast/ {
            alias /app/web/cast/;
            try_files \$uri \$uri/ /cast/index.html;
        }
        
        # API endpoints - proxy to Museum server
        location /api/ {
            proxy_pass http://localhost:8000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade \$http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host \$host;
            proxy_cache_bypass \$http_upgrade;
            access_log /dev/stdout;
            error_log /dev/stderr debug;
        }
    }
}
EOT

echo "==> Custom NGINX configuration created at /app/data/nginx/ente.conf"

# Looking for Museum (Go server component)
echo "==> Looking for Museum (Go server component)"

# Find the server directory - expanded search
SERVER_DIR=""

# Look in all the most likely places
if [ -d "/app/code/server" ]; then
    echo "==> Found server directory at /app/code/server"
    SERVER_DIR="/app/code/server"
elif [ -d "/app/code/museum" ]; then
    echo "==> Found server directory at /app/code/museum"
    SERVER_DIR="/app/code/museum"
fi

# If not found yet, try to find it based on Go files
if [ -z "$SERVER_DIR" ]; then
    echo "==> Searching for Go server files in the repository"
    
    # Look for cmd/museum path which is mentioned in docs
    if find /app/code -path "*/cmd/museum" -type d | grep -q .; then
        MUSEUM_DIR=$(find /app/code -path "*/cmd/museum" -type d | head -1)
        echo "==> Found Museum command directory at $MUSEUM_DIR"
        SERVER_DIR=$(dirname $(dirname "$MUSEUM_DIR"))
        echo "==> Setting server directory to $SERVER_DIR"
    # Look for main.go files in areas that might be server-related
    elif find /app/code -name "main.go" -path "*/server*" | grep -q .; then
        MAIN_GO=$(find /app/code -name "main.go" -path "*/server*" | head -1)
        SERVER_DIR=$(dirname "$MAIN_GO")
        echo "==> Found main.go in $SERVER_DIR"
    # Look for any Go files with "museum" in the path
    elif find /app/code -name "*.go" -path "*/museum*" | grep -q .; then
        MUSEUM_GO=$(find /app/code -name "*.go" -path "*/museum*" | head -1)
        SERVER_DIR=$(dirname "$MUSEUM_GO")
        echo "==> Found Go file in museum directory: $SERVER_DIR"
    # Last resort - look for any main.go file
    elif find /app/code -name "main.go" | grep -v "test" | grep -q .; then
        MAIN_GO=$(find /app/code -name "main.go" | grep -v "test" | head -1)
        SERVER_DIR=$(dirname "$MAIN_GO")
        echo "==> Found main.go as fallback: $SERVER_DIR"
    fi
fi

# If still not found, check the 'cli' directory as it might be a client for the Museum
if [ -z "$SERVER_DIR" ] && [ -d "/app/code/cli" ]; then
    echo "==> Checking CLI directory for Museum server code"
    if find /app/code/cli -name "*.go" | grep -q .; then
        SERVER_DIR="/app/code/cli"
        echo "==> Using CLI directory as fallback for server: $SERVER_DIR"
    fi
fi

# If all else fails, just use the root
if [ -z "$SERVER_DIR" ]; then
    echo "==> WARNING: Could not find server directory with Go files"
    echo "==> Using repository root as fallback"
    SERVER_DIR="/app/code"
fi

echo "==> Selected server directory: $SERVER_DIR"
echo "==> Contents of $SERVER_DIR:"
ls -la "$SERVER_DIR"

# Check for server dependencies
echo "==> Installing Go dependencies"
if [ -x "$(command -v go)" ]; then
    echo "==> Go is installed, version: $(go version)"
    # Check go.mod version requirement and modify if needed
    GO_VERSION=$(go version | cut -d " " -f 3 | sed 's/go//')
    echo "==> Current Go version: $GO_VERSION"
    if [ -f "$SERVER_DIR/go.mod" ]; then
        REQUIRED_VERSION=$(grep -o "go [0-9]\+\.[0-9]\+" "$SERVER_DIR/go.mod" | cut -d " " -f 2)
        echo "==> Required Go version: $REQUIRED_VERSION"
        
        # Don't try to modify the go.mod file since filesystem is read-only
        # Instead, use environment variables to override version requirements
        echo "==> Setting Go flags to override version requirements"
        export GOFLAGS="-mod=mod"
        export GO111MODULE=on
        export GOTOOLCHAIN=local
    fi
else
    echo "==> Installing Go"
    apt-get update && apt-get install -y golang
fi

# Skip trying to install specific Go versions since we can't modify /app/code
echo "==> Using existing Go toolchain with compatibility flags"

# Check for libsodium
if dpkg -l | grep -q libsodium; then
    echo "==> libsodium is installed"
else
    echo "==> Installing libsodium"
    apt-get update && apt-get install -y libsodium23 libsodium-dev
fi

# Check for pkg-config
if [ -x "$(command -v pkg-config)" ]; then
    echo "==> pkg-config is installed"
else
    echo "==> Installing pkg-config"
    apt-get update && apt-get install -y pkg-config
fi

# Change to server directory
cd "$SERVER_DIR"

# Set Go module cache to a writable location
export GOPATH=/app/data/go
export GO111MODULE=on
# Set version compatibility flags
export GOTOOLCHAIN=local
# Create a temporary directory for Go module cache and build in writable area
export GOCACHE=/app/data/go/cache
export GOMODCACHE=/app/data/go/modcache
mkdir -p $GOCACHE $GOMODCACHE
echo "==> Set up Go environment in writable directories"

# Set up more verbose logging for API debugging
export ENTE_LOG_LEVEL=debug

# Test if API endpoint is reachable
echo "==> Testing API connectivity"
curl -v http://localhost:8000/api/health || echo "API not yet available, this is normal during startup"

# Determine available memory and set limits accordingly
if [[ -f /sys/fs/cgroup/cgroup.controllers ]]; then # cgroup v2
    memory_limit=$(cat /sys/fs/cgroup/memory.max)
    [[ "${memory_limit}" == "max" ]] && memory_limit=$(( 2 * 1024 * 1024 * 1024 )) # "max" really means unlimited
else
    memory_limit=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) # this is the RAM. we have equal amount of swap
fi
memory_mb=$((memory_limit/1024/1024))
echo "==> Available memory: ${memory_mb}MB"

# Set up database environment variables and ensure proper SSL config
export ENTE_DB_USER="${CLOUDRON_POSTGRESQL_USERNAME}"
export ENTE_DB_PASSWORD="${CLOUDRON_POSTGRESQL_PASSWORD}"
export ENTE_DB_HOST="${CLOUDRON_POSTGRESQL_HOST}"
export ENTE_DB_PORT="${CLOUDRON_POSTGRESQL_PORT}"
export ENTE_DB_NAME="${CLOUDRON_POSTGRESQL_DATABASE}"
export ENTE_DB_SSLMODE="require"
export CONFIG_PATH="/app/data/config/config.yaml"

# Additional environment variables
export REMOTE_STORAGE_ENDPOINT="${S3_ENDPOINT}"
export REMOTE_STORAGE_REGION="${S3_REGION}"
export REMOTE_STORAGE_BUCKET="${S3_BUCKET}"
export REMOTE_STORAGE_ACCESS_KEY="${S3_ACCESS_KEY}"
export REMOTE_STORAGE_SECRET_KEY="${S3_SECRET_KEY}"
export REMOTE_STORAGE_PREFIX="${S3_PREFIX:-ente/}"

# Change ownership to cloudron user
chown -R cloudron:cloudron /app/data
chown -R cloudron:cloudron /run/nginx

# Start Museum server on port 8000 (different from the NGINX port 8080)
echo "==> Starting Museum server"

# Modify environment variables for frontend
echo "==> Setting up environment variables for frontend"
# For web frontend, these aren't changing
export NEXT_PUBLIC_ENTE_ENDPOINT="${CLOUDRON_APP_ORIGIN}"

# First check for pre-built Museum binary
if find "$SERVER_DIR" -name "museum" -type f -executable | grep -q .; then
    MUSEUM_BIN=$(find "$SERVER_DIR" -name "museum" -type f -executable | head -1)
    echo "==> Found Museum binary at $MUSEUM_BIN"
    /usr/local/bin/gosu cloudron:cloudron "$MUSEUM_BIN" --port 8000 \
        --storage.s3.endpoint="${S3_ENDPOINT}" \
        --storage.s3.region="${S3_REGION}" \
        --storage.s3.bucket="${S3_BUCKET}" \
        --storage.s3.accessKey="${S3_ACCESS_KEY}" \
        --storage.s3.secretKey="${S3_SECRET_KEY}" \
        --storage.s3.prefix="${S3_PREFIX:-ente/}" \
        --storage.s3.forcePathStyle=true \
        --storage.s3.areLocalBuckets=false \
        --storage.type="s3" \
        --config.path="/app/data/config/museum.yaml" \
        --database.sslmode="require" > /app/data/logs/museum-server.log 2>&1 &
    SERVER_PID=$!
    echo "==> Museum server started with PID $SERVER_PID"
# Next check for cmd/museum directory pattern
elif [ -d "$SERVER_DIR/cmd/museum" ]; then
    echo "==> Found Museum source in cmd/museum, running with go run"
    cd "$SERVER_DIR"
    
    # Instead of modifying go.mod, set environment variables for compatibility
    echo "==> Setting Go environment variables for compatibility"
    export GOFLAGS="-mod=mod"
    export GOTOOLCHAIN=local
    
    # For Wasabi specific settings
    if [[ "${S3_ENDPOINT}" == *"wasabi"* ]]; then
        echo "==> Detected Wasabi S3 endpoint, adjusting settings"
        /usr/local/bin/gosu cloudron:cloudron go run cmd/museum/main.go --port 8000 \
            --storage.s3.endpoint="${S3_ENDPOINT}" \
            --storage.s3.region="${S3_REGION}" \
            --storage.s3.bucket="${S3_BUCKET}" \
            --storage.s3.accessKey="${S3_ACCESS_KEY}" \
            --storage.s3.secretKey="${S3_SECRET_KEY}" \
            --storage.s3.prefix="${S3_PREFIX:-ente/}" \
            --storage.s3.forcePathStyle=true \
            --storage.s3.areLocalBuckets=false \
            --storage.type="s3" \
            --config.path="/app/data/config/museum.yaml" \
            --database.sslmode="require" \
            --log.level=debug > /app/data/logs/museum-server.log 2>&1 &
    else
        /usr/local/bin/gosu cloudron:cloudron go run cmd/museum/main.go --port 8000 \
            --storage.s3.endpoint="${S3_ENDPOINT}" \
            --storage.s3.region="${S3_REGION}" \
            --storage.s3.bucket="${S3_BUCKET}" \
            --storage.s3.accessKey="${S3_ACCESS_KEY}" \
            --storage.s3.secretKey="${S3_SECRET_KEY}" \
            --storage.s3.prefix="${S3_PREFIX:-ente/}" \
            --storage.s3.forcePathStyle=true \
            --storage.s3.areLocalBuckets=false \
            --storage.type="s3" \
            --config.path="/app/data/config/museum.yaml" \
            --database.sslmode="require" \
            --log.level=debug > /app/data/logs/museum-server.log 2>&1 &
    fi
    SERVER_PID=$!
    echo "==> Museum server started with PID $SERVER_PID"
    
    # Wait for the server to start
    echo "==> Waiting for Museum server to start..."
    sleep 5
    
    # Test if API is responding
    MAX_RETRIES=5
    RETRY_COUNT=0
    while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
        echo "==> Testing API connection (attempt $((RETRY_COUNT+1))/$MAX_RETRIES)"
        if curl -s http://localhost:8000/api/health > /dev/null; then
            echo "==> API is now responding"
            break
        else
            echo "==> API not responding yet, waiting..."
            RETRY_COUNT=$((RETRY_COUNT+1))
            sleep 5
            # Print recent log output
            echo "==> Recent server log output:"
            tail -n 20 /app/data/logs/museum-server.log || echo "==> No logs available yet"
        fi
    done
    
    if [ $RETRY_COUNT -eq $MAX_RETRIES ]; then
        echo "==> WARNING: API server did not respond after multiple attempts"
        echo "==> Checking server logs:"
        ps -p $SERVER_PID >/dev/null || echo "==> ERROR: Server process is not running!"
        tail -n 50 /app/data/logs/museum-server.log || echo "==> No logs available"
    fi

# Next try to find any main.go for the Museum
else
    # Fallback approach - find main.go files
    MAIN_FILES=$(find "$SERVER_DIR" -name "main.go" | grep -v "test" || echo "")
    if [ -n "$MAIN_FILES" ]; then
        MAIN_FILE=$(echo "$MAIN_FILES" | head -1)
        MAIN_DIR=$(dirname "$MAIN_FILE")
        echo "==> Using main.go file at $MAIN_FILE"
        cd "$MAIN_DIR"
        
        # Instead of trying to modify go.mod, set environment variables for compatibility
        echo "==> Setting Go environment variables for compatibility"
        export GOFLAGS="-mod=mod"
        export GOTOOLCHAIN=local
        
        echo "==> Running main.go with Go"
        /usr/local/bin/gosu cloudron:cloudron go run main.go --port 8000 \
            --storage.s3.endpoint="${S3_ENDPOINT}" \
            --storage.s3.region="${S3_REGION}" \
            --storage.s3.bucket="${S3_BUCKET}" \
            --storage.s3.accessKey="${S3_ACCESS_KEY}" \
            --storage.s3.secretKey="${S3_SECRET_KEY}" \
            --storage.s3.prefix="${S3_PREFIX:-ente/}" \
            --storage.s3.forcePathStyle=true \
            --storage.s3.areLocalBuckets=false \
            --storage.type="s3" \
            --config.path="/app/data/config/museum.yaml" \
            --log.level=debug > /app/data/logs/museum-server.log 2>&1 &
        SERVER_PID=$!
        echo "==> Museum server started with PID $SERVER_PID"
        
        # Check if the server process is actually running after a brief pause
        sleep 2
        if ! ps -p $SERVER_PID > /dev/null; then
            echo "==> WARNING: Server process exited immediately"
            echo "==> Recent server log output:"
            tail -n 50 /app/data/logs/museum-server.log || echo "==> No logs available yet"
            echo "==> Falling back to mock server"
            SERVER_PID=""
        fi
    else
        echo "==> ERROR: Could not find Museum binary or main.go source file"
        echo "==> Available Go files:"
        find "$SERVER_DIR" -name "*.go" | sort
        
        SERVER_PID=""
    fi
fi

# If server didn't start successfully, use mock server
if [ -z "$SERVER_PID" ] || ! ps -p $SERVER_PID > /dev/null; then
    echo "==> Starting mock server as fallback"
    # Last resort - create a temporary Go HTTP server that returns a meaningful error
    mkdir -p /tmp/mock-server
    cat > /tmp/mock-server/main.go <<EOT
package main

import (
    "fmt"
    "log"
    "net/http"
    "os"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "text/html")
        fmt.Fprintf(w, "<html><body><h1>Ente Museum Server Error</h1>")
        fmt.Fprintf(w, "<p>The Ente Museum server could not be started due to compatibility issues.</p>")
        fmt.Fprintf(w, "<p>Please check the logs for more information.</p>")
        
        // Show log excerpts if available
        if _, err := os.Stat("/app/data/logs/museum-server.log"); err == nil {
            logs, err := os.ReadFile("/app/data/logs/museum-server.log")
            if err == nil && len(logs) > 0 {
                fmt.Fprintf(w, "<h2>Recent Log Output:</h2><pre>%s</pre>", logs)
            }
        }
        
        fmt.Fprintf(w, "</body></html>")
    })
    
    // Add a health endpoint
    http.HandleFunc("/api/health", func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "application/json")
        w.Write([]byte(`{"status": "error", "message": "Running in fallback mode"}`))
    })
    
    log.Println("Starting mock server on port 8000")
    go func() {
        log.Fatal(http.ListenAndServe(":8000", nil))
    }()
}
EOT
    cd /tmp/mock-server
    
    # Set Go environment variables for compatibility with the mock server
    export GOFLAGS="-mod=mod"
    export GOTOOLCHAIN=local
    
    go run main.go &
    SERVER_PID=$!
    echo "==> Mock server started with PID $SERVER_PID to show error message"
fi

# Serve the static web apps in the foreground using our custom nginx config
echo "==> Running NGINX in the foreground with custom configuration"
nginx -c /app/data/nginx/ente.conf &
NGINX_PID=$!
echo "==> NGINX started with PID $NGINX_PID"

echo "==> Ente is now running!"
echo "==> Museum server: PID $SERVER_PID"
echo "==> NGINX: PID $NGINX_PID"

# Wait for the processes to finish (or be terminated)
wait 