#!/bin/bash

set -eu

# Create necessary directories
mkdir -p /app/data/config /app/data/storage

# Print debug information about repository structure
echo "==> DEBUG: Repository structure at /app/code"
find /app/code -type d -not -path "*/node_modules/*" -not -path "*/\.*" | sort
echo "==> DEBUG: All package.json files in repository"
find /app/code -name "package.json" -not -path "*/node_modules/*" | sort
echo "==> DEBUG: Web app directories"
ls -la /app/web

# Create config template file on first run
if [[ ! -f /app/data/config/config.yaml ]]; then
    echo "==> First run - creating configuration template"
    
    # 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" \
        /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"
    
    # Exit with a message if this is the first run
    if [[ ! -f /app/data/config/s3.env ]]; then
        echo "==> Exiting. Please configure S3 storage and restart the app."
        exit 1
    fi
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

# 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 up NGINX to serve the web apps and proxy to the Museum server
echo "==> Setting up NGINX for web apps and API"

# Create NGINX configuration
cat > /etc/nginx/sites-available/ente <<EOT
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;
    }
}
EOT

# Enable the site
ln -sf /etc/nginx/sites-available/ente /etc/nginx/sites-enabled/ente

# Start NGINX
echo "==> Starting NGINX"
service nginx restart

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

# Find the server directory
if [ -d "/app/code/server" ]; then
    echo "==> Found server directory at /app/code/server"
    SERVER_DIR="/app/code/server"
else
    echo "==> ERROR: Could not find server directory at /app/code/server"
    echo "==> Looking for Museum executable or source in alternate locations"
    
    # Search for Museum executable or Go source files
    GO_FILES=$(find /app/code -name "*.go" -not -path "*/\.*" | sort)
    if [ -n "$GO_FILES" ]; then
        echo "==> Found Go files in repository:"
        echo "$GO_FILES"
        
        # Check for museum in Go files path
        if find /app/code -path "*/museum/*" -name "*.go" | grep -q .; then
            MUSEUM_DIR=$(find /app/code -path "*/museum/*" -name "*.go" | head -1 | xargs dirname)
            echo "==> Found Museum directory at $MUSEUM_DIR"
        else
            echo "==> No clear Museum directory found"
            echo "==> Using first Go file directory as fallback"
            FIRST_GO_FILE=$(echo "$GO_FILES" | head -1)
            MUSEUM_DIR=$(dirname "$FIRST_GO_FILE")
        fi
        
        # Check for cmd/museum pattern which is mentioned in docs
        if [ -d "/app/code/server/cmd/museum" ]; then
            MUSEUM_DIR="/app/code/server/cmd/museum"
            echo "==> Found Museum main directory at $MUSEUM_DIR"
        fi
        
        SERVER_DIR=$(dirname $(dirname "$MUSEUM_DIR"))
    else
        echo "==> FATAL ERROR: Could not find Go files in repository"
        echo "==> Repository structure:"
        find /app/code -type d -maxdepth 3 | sort
        exit 1
    fi
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)"
else
    echo "==> Installing Go"
    apt-get update && apt-get install -y golang
fi

# 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 up database environment variables
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 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

# Start Museum server on port 8000 (different from the NGINX port 8080)
if [ -x "$SERVER_DIR/museum" ]; then
    echo "==> Found Museum binary, running it in the background"
    /usr/local/bin/gosu cloudron:cloudron "$SERVER_DIR/museum" --port 8000 &
elif [ -d "$SERVER_DIR/cmd/museum" ]; then
    echo "==> Found Museum source in cmd/museum, running with go run in the background"
    cd "$SERVER_DIR"
    /usr/local/bin/gosu cloudron:cloudron go run cmd/museum/main.go --port 8000 &
else
    # Fallback approach - find main.go files
    MAIN_FILES=$(find "$SERVER_DIR" -name "main.go" | grep -v "test")
    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, running in the background"
        cd "$MAIN_DIR"
        /usr/local/bin/gosu cloudron:cloudron go run main.go --port 8000 &
    else
        echo "==> FATAL ERROR: Could not find Museum binary or main.go source file"
        echo "==> Available Go files:"
        find "$SERVER_DIR" -name "*.go" | sort
        exit 1
    fi
fi

# Serve the static web apps in the foreground
echo "==> Running NGINX in the foreground"
exec nginx -g "daemon off;" 