#!/bin/bash

export CONTAINERROLE=":${CONTAINERROLE:-all}:"
export DEBIAN_FRONTEND=noninteractive
export DA_ROOT="${DA_ROOT:-/usr/share/docassemble}"
export DA_DEFAULT_LOCAL="local3.12"

export DA_ACTIVATE="${DA_PYTHON:-${DA_ROOT}/${DA_DEFAULT_LOCAL}}/bin/activate"
export DA_CONFIG_FILE="${DA_CONFIG:-${DA_ROOT}/config/config.yml}"
source /dev/stdin < <(su -c "source \"$DA_ACTIVATE\" && python -m docassemble.base.read_config \"$DA_CONFIG_FILE\"" www-data)

set -- $LOCALE
export LANG=$1

if [[ '$(dpkg --print-architecture)' == 'amd64' ]]; then
    CURRENTARCH=x86_64
else
    CURRENTARCH=aarch64
fi

if [ "${DAHOSTNAME:-none}" == "none" ]; then
    if [ "${EC2:-false}" == "true" ]; then
	export LOCAL_HOSTNAME=`curl -s http://169.254.169.254/latest/meta-data/local-hostname`
	export PUBLIC_HOSTNAME=`curl -s http://169.254.169.254/latest/meta-data/public-hostname`
    else
	export LOCAL_HOSTNAME=`hostname --fqdn`
	export PUBLIC_HOSTNAME="${LOCAL_HOSTNAME}"
    fi
    export DAHOSTNAME="${PUBLIC_HOSTNAME}"
fi

if [ "${BEHINDHTTPSLOADBALANCER:-false}" == "true" ]; then
    a2enmod remoteip
    a2enconf docassemble-behindlb
else
    a2dismod remoteip
    a2disconf docassemble-behindlb
fi

echo -e "# This file is automatically generated" > /etc/apache2/conf-available/docassemble.conf
echo -e "LoadModule wsgi_module ${DA_PYTHON:-${DA_ROOT}/${DA_DEFAULT_LOCAL}}/lib/python3.12/site-packages/mod_wsgi/server/mod_wsgi-py312.cpython-312-${CURRENTARCH}-linux-gnu.so" >> /etc/apache2/conf-available/docassemble.conf
echo -e "WSGIPythonHome ${DA_PYTHON:-${DA_ROOT}/${DA_DEFAULT_LOCAL}}" >> /etc/apache2/conf-available/docassemble.conf
echo -e "Timeout ${DATIMEOUT:-60}\nDefine DAHOSTNAME ${DAHOSTNAME}\nDefine DAPOSTURLROOT ${POSTURLROOT}\nDefine DAWSGIROOT ${WSGIROOT}\nDefine DASERVERADMIN ${SERVERADMIN}\nDefine DAWEBSOCKETSIP ${DAWEBSOCKETSIP:-127.0.0.1}\nDefine DAWEBSOCKETSPORT ${DAWEBSOCKETSPORT}\nDefine DACROSSSITEDOMAINVALUE *\nDefine DALISTENPORT ${PORT:-80}" >> /etc/apache2/conf-available/docassemble.conf
echo "Listen ${PORT:-80}" > /etc/apache2/ports.conf
if [ "${BEHINDHTTPSLOADBALANCER:-false}" == "true" ]; then
    echo "Listen 8081" >> /etc/apache2/ports.conf
    a2ensite docassemble-redirect
fi
if [ "${USEHTTPS:-false}" == "true" ]; then
    echo "Listen 443" >> /etc/apache2/ports.conf
    a2enmod ssl
    a2ensite docassemble-ssl
else
    a2dismod ssl
    a2dissite -q docassemble-ssl &> /dev/null
fi
if [[ $CONTAINERROLE =~ .*:(log):.* ]]; then
    echo "Listen 8080" >> /etc/apache2/ports.conf
fi

function stopfunc {
    echo "Sending stop command"
    /usr/sbin/apache2ctl stop
    echo "Waiting for apache to stop"
    while pgrep apache2 > /dev/null; do sleep 1; done
    echo "Apache stopped"
    exit 0
}

trap stopfunc SIGINT SIGTERM

/usr/sbin/apache2ctl -DFOREGROUND &
wait %1
