#!/bin/bash
set -e

# Detect OS
if [ -f /etc/redhat-release ]; then
    # Rocky Linux / CentOS / RHEL
    yum install -y nginx
    systemctl enable nginx
    systemctl start nginx

    # Configure include path
    mkdir -p /app/frontend/conf
    if ! grep -q "/app/frontend/conf" /etc/nginx/nginx.conf; then
        echo "include /app/frontend/conf/*.conf;" >> /etc/nginx/nginx.conf
    fi

elif [ -f /etc/debian_version ]; then
    # Ubuntu / Debian
    apt-get update
    apt-get install -y nginx
    systemctl enable nginx
    systemctl start nginx

    # Configure include path
    mkdir -p /app/frontend/conf
    if ! grep -q "/app/frontend/conf" /etc/nginx/nginx.conf; then
        echo "include /app/frontend/conf/*.conf;" >> /etc/nginx/nginx.conf
    fi
fi

echo "Nginx installed successfully"
