#!/usr/bin/env bash

#####################################################
# Created by Afiniel for Yiimpool use
#
# This script configures NGINX without SSL
# domain of a YiiMP pool installation.
#
# Author: Afiniel
# Date: 2024-07-15
#####################################################

# Load configuration files
source /etc/functions.sh
source /etc/yiimpool.conf
source $STORAGE_ROOT/yiimp/.yiimp.conf
source $HOME/Yiimpoolv1/yiimp_single/.wireguard.install.cnf

set -eu -o pipefail

function print_error {
	read line file <<<$(caller)
	echo "An error occurred in line $line of file $file:" >&2
	sed "${line}q;d" "$file" >&2
}
trap print_error ERR

# Display banner
term_art

print_header "NGINX Configuration Setup"

# Load WireGuard configuration if enabled
if [[ ("$wireguard" == "true") ]]; then
	source $STORAGE_ROOT/yiimp/.wireguard.conf
fi

print_status "Configuring NGINX for ${DomainName} without SSL..."

print_status "Creating NGINX configuration..."
echo '#####################################################
# Source Generated by nginxconfig.io
# Updated by yiimpool for crypto use...
#####################################################

# NGINX Simple DDoS Defense
limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:10m;
limit_conn conn_limit_per_ip 80;
limit_req zone=req_limit_per_ip burst=80 nodelay;
limit_req_zone $binary_remote_addr zone=req_limit_per_ip:40m rate=5r/s;
server {
	listen 443 ssl http2;
	listen [::]:443 ssl http2;
	server_name '"${DomainName}"';
	set $base "/var/www/'"${DomainName}"'/html";
	root $base/web;
	# SSL
	ssl_certificate '"${STORAGE_ROOT}"'/ssl/ssl_certificate.pem;
	ssl_certificate_key '"${STORAGE_ROOT}"'/ssl/ssl_private_key.pem;
	# security
	include yiimpool/security.conf;
	# logging
	access_log '"${STORAGE_ROOT}"'/yiimp/site/log/'"${DomainName}"'.app.access.log;
	error_log '"${STORAGE_ROOT}"'/yiimp/site/log/'"${DomainName}"'.app.error.log warn;
	# index.php
	index index.php;
	# index.php fallback
	location / {
		try_files $uri $uri/ /index.php?$args;
	}
	location @rewrite {
		rewrite ^/(.*)$ /index.php?r=$1;
	}
	# handle .php
	location ~ \.php$ {
		include yiimpool/php_fastcgi.conf;
	}
	# additional config
	include yiimpool/general.conf;
	include phpmyadmin.conf;
}
# HTTP redirect
server {
	listen 80;
	listen [::]:80;
	server_name .'"${DomainName}"';
	include yiimpool/letsencrypt.conf;
	location / {
		return 301 https://'"${DomainName}"'$request_uri;
	}
}
' | sudo -E tee /etc/nginx/sites-available/${DomainName}.conf >/dev/null 2>&1

print_status "Creating symbolic links..."
sudo ln -sf /etc/nginx/sites-available/${DomainName}.conf /etc/nginx/sites-enabled/${DomainName}.conf
sudo ln -sf $STORAGE_ROOT/yiimp/site/web /var/www/${DomainName}/html

restart_service nginx
restart_service php8.1-fpm

print_success "NGINX configuration completed successfully"

print_header "Configuration Summary"
print_info "Domain: ${DomainName}"
print_info "Web Root: /var/www/${DomainName}/html"
print_info "Configuration: /etc/nginx/sites-available/${DomainName}.conf"
print_info "Access Log: ${STORAGE_ROOT}/yiimp/site/log/${DomainName}.app.access.log"
print_info "Error Log: ${STORAGE_ROOT}/yiimp/site/log/${DomainName}.app.error.log"

print_divider

set +eu +o pipefail

cd $HOME/Yiimpoolv1/yiimp_single