#!/bin/bash
set -e
. nginx_common.sh

# Function to print the line and the command that failed
error_trap() {
	local exit_code=$?
	echo "Error: Command failed at line $BASH_LINENO with exit code $exit_code"
	exit $exit_code
}

trap error_trap ERR

LUANHOST_DIR="$(pwd)"

cp startup/nginx/mime.types $NGINX_ROOT_CONFIG_DIR
luan startup/nginx/nginx_master.conf.luan $NGINX_ROOT_CONFIG_DIR $NGINX_SITE_CONFIGS_DIR $NGINX_LOG_DIR $NGINX_PID_DIR $NGINX_USER $NGINX_GROUP >$NGINX_ROOT_CONFIG_FILE
luan startup/nginx/9990-luanhost_controller.conf.luan $LUANHOST_DIR >"$NGINX_SITE_CONFIGS_DIR/9990-luanhost_controller.conf"

# DON'T put this in the auto-loaded configs dir (NGINX_SITE_CONFIGS_DIR),
# it is only meant to be loaded as part of a server block.
luan startup/nginx/luanhost_default_site.conf.luan $LUANHOST_DIR >"$LUANHOST_DIR/local/luanhost_default_site.conf"
