#!/usr/bin/with-contenv bash
# shellcheck shell=bash

# #
#   precreate log files
# #

for file in /config/log/nginx/access.log /config/log/nginx/error.log; do
    if [[ ! -f "${file}" ]]; then
        touch "${file}"
    fi
done

# #
#   copy default config files if they don't exist
# #

if [[ ! -f /config/nginx/nginx.conf ]]; then
    cp /defaults/nginx/nginx.conf.sample /config/nginx/nginx.conf
fi

if [[ ! -f /config/nginx/ssl.conf ]]; then
    cp /defaults/nginx/ssl.conf.sample /config/nginx/ssl.conf
fi

if [[ ! -f /config/nginx/site-confs/default.conf ]]; then
    cp /defaults/nginx/site-confs/default.conf.sample /config/nginx/site-confs/default.conf
fi

# #
#   force nginx.conf to include site-confs/*.conf instead of site-confs/*
# #

sed -i -E "s#^(\s*)include /config/nginx/site-confs/\*;#\1include /config/nginx/site-confs/\*.conf;#" /config/nginx/nginx.conf

# #
#   copy index.html if no index file exists
# #

INDEX_EXISTS=false
for file in /config/www/index.*; do
    if [[ -e "${file}" ]]; then
        INDEX_EXISTS=true
        break
    fi
done

THEME_EXISTS=false
for file in /config/www/theme/*; do
    if [[ -e "${file}" ]]; then
        THEME_EXISTS=true
        break
    fi
done

# dont add an index, we want fancy indexing
# if [[ ${INDEX_EXISTS} == false ]] && grep -Eq '^\s*index[^#]*index\.html' /config/nginx/**/*.conf; then
#     cp /defaults/www/index.html /config/www/index.html
# fi

if [[ ${THEME_EXISTS} == false ]]; then
    cp -r /defaults/www/theme /config/www/
fi

# #
#   copy pre-generated dhparams or generate if needed
# #

if [[ ! -f /config/nginx/dhparams.pem ]]; then
    cp /defaults/nginx/dhparams.pem /config/nginx/dhparams.pem
fi

if ! grep -q 'PARAMETERS' "/config/nginx/dhparams.pem"; then
    curl -o /config/nginx/dhparams.pem -L "https://ssl-config.mozilla.org/ffdhe4096.txt"
fi

# #
#   Set resolver, ignore ipv6 addresses
# #

touch /config/nginx/resolver.conf
if ! grep -q 'resolver' /config/nginx/resolver.conf; then
    RESOLVERRAW=$(awk 'BEGIN{ORS=" "} $1=="nameserver" {print $2}' /etc/resolv.conf)
    for i in ${RESOLVERRAW}; do
        if [[ "$(awk -F ':' '{print NF-1}' <<<"${i}")" -le 2 ]]; then
            RESOLVER="${RESOLVER} ${i}"
        fi
    done

    if [[ -z "${RESOLVER}" ]]; then
        RESOLVER="127.0.0.11"
    fi

    echo "[ Resolver ]: Setting to ${RESOLVER}"
    RESOLVEROUTPUT="# This file is auto-generated only on first start, based on the container's /etc/resolv.conf file. Feel free to modify it as you wish.\n\nresolver ${RESOLVER} valid=30s;"
    echo -e "${RESOLVEROUTPUT}" >/config/nginx/resolver.conf
fi

# #
#   Set worker_processes
# #

touch /config/nginx/worker_processes.conf
if ! grep -q 'worker_processes' /config/nginx/worker_processes.conf; then
    WORKER_PROCESSES=$(nproc)
    echo "[ Worker   ]: Setting worker_processes to ${WORKER_PROCESSES}"
    echo -e "# This file is auto-generated only on first start, based on the cpu cores detected. Feel free to change it to any other number or to auto to let nginx handle it automatically.\n\nworker_processes ${WORKER_PROCESSES};" >/config/nginx/worker_processes.conf
fi
