#!/usr/bin/env bash

#####################################################
# Created by Afiniel for Yiimpool use
#
# This script configures NGINX without SSL for a 
# subdomain 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 to print error trace on error
function print_error {
	read -r 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 Subdomain Configuration"

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

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

print_status "Creating NGINX configuration..."
echo '#####################################################
# Source Generated by nginxconfig.io
# Updated by afiniel 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..."
if [[ -L "/etc/nginx/sites-enabled/${DomainName}.conf" ]]; then
    print_warning "Symbolic link already exists, skipping creation"
else
    sudo ln -sf "/etc/nginx/sites-available/${DomainName}.conf" "/etc/nginx/sites-enabled/${DomainName}.conf"
fi
sudo ln -sf $STORAGE_ROOT/yiimp/site/web "/var/www/${DomainName}/html"

print_status "Setting permissions..."
sudo chown -R www-data:www-data /var/www/${DomainName}
sudo chmod -R 755 /var/www/${DomainName}

print_status "Restarting services..."
restart_service nginx
restart_service php8.1-fpm

print_success "NGINX subdomain configuration completed successfully"

print_header "Configuration Summary"
print_info "Subdomain: ${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

# Reset error handling
set +eu +o pipefail

cd $HOME/Yiimpoolv1/yiimp_single
