#!/bin/bash

# load some functions
. /opt/bunkerized-nginx/entrypoint/utils.sh

function do_and_check_cmd() {
	if [ "$CHANGE_DIR" != "" ] ; then
		cd "$CHANGE_DIR"
	fi
	if [ "$AS_ROOT" != "" ] ; then
		output=$("$@" 2>&1)
	else
		output=$(su -s "/bin/bash" -c "$1" nginx 2>&1)
	fi
	ret="$?"
	if [ $ret -ne 0 ] ; then
		echo "[!] Error from command : $*"
		echo "$output"
		exit $ret
	fi
	echo "$output"
	return 0
}

# Check if we are root
if [ $(id -u) -ne 0 ] ; then
	echo "[!] Run me as root"
	exit 1
fi

# Check if variables.env is present
if [ ! -f "/opt/bunkerized-nginx/variables.env" ] ; then
	echo "[!] Missing /opt/bunkerized-nginx/variables.env"
	exit 1
fi

# Run generator
echo "[*] Generate configuration files"
do_and_check_cmd "/opt/bunkerized-nginx/gen/main.py --settings /opt/bunkerized-nginx/settings.json --templates /opt/bunkerized-nginx/confs --output /etc/nginx --variables /opt/bunkerized-nginx/variables.env"

# Run temporary nginx if needed
status="$(systemctl status nginx 2>&1)"
if [ $? -ne 0 ] && [ "$(grep "^.*AUTO_LETS_ENCRYPT=yes$" /opt/bunkerized-nginx/variables.env)" != "" ] ; then
	echo "[*] Run temp nginx"
	do_and_check_cmd "cp /opt/bunkerized-nginx/confs/global/nginx-temp.conf /tmp/nginx-temp.conf"
	replace_in_file "/tmp/nginx-temp.conf" "%USE_API%" ""
	replace_in_file "/tmp/nginx-temp.conf" "%HTTP_PORT%" "80"
	AS_ROOT="yes" do_and_check_cmd nginx -c /tmp/nginx-temp.conf -g 'user nginx;'
fi

# Run pre-jobs
echo "[*] Run jobs"
CHANGE_DIR=/tmp do_and_check_cmd "/opt/bunkerized-nginx/entrypoint/jobs.sh"

# Stop temp nginx
status="$(systemctl status nginx 2>&1)"
if [ $? -ne 0 ] && [ -f "/tmp/nginx-temp.pid" ] ; then
	AS_ROOT="yes" do_and_check_cmd nginx -c /tmp/nginx-temp.conf -s quit
fi

# Reload nginx if it's running
status="$(systemctl status nginx 2>&1)"
if [ $? -eq 0 ] ; then
	echo "[*] Reload nginx"
	AS_ROOT="yes" do_and_check_cmd systemctl reload nginx
# Otherwise start it
else
	echo "[*] Start nginx"
	AS_ROOT="yes" do_and_check_cmd systemctl start nginx
fi

# Done
echo "[*] bunkerized-nginx successfully executed"
