#!/bin/bash

httpd_user=${httpd_user:-apache}
httpd_uid=${httpd_uid:-48}
httpd_group=${httpd_group:-apache}

useradd -u $httpd_uid -s /sbin/nologin $httpd_user

export httpd_user
export httpd_group

if [ -f /run/secrets/sentry_dsn ]; then
    export SENTRY_DSN=$(cat /run/secrets/sentry_dsn)
else
    echo "Secret file 'sentry_dsn' not found!" >&2
fi
if [ -f /run/secrets/oidc_client_id ]; then
    export OIDC_CLIENT_ID=$(cat /run/secrets/oidc_client_id)
else
    echo "Secret file 'oidc_client_id' not found!" >&2
fi
if [ -f /run/secrets/rabbitmq_username ]; then
    export RABBITMQ_USERNAME=$(cat /run/secrets/rabbitmq_username)
else
    echo "Secret file 'rabbitmq_username' not found!" >&2
fi
if [ -f /run/secrets/rabbitmq_password ]; then
    export RABBITMQ_PASSWORD=$(cat /run/secrets/rabbitmq_password)
else
    echo "Secret file 'rabbitmq_password' not found!" >&2
fi
if [ -f /run/secrets/geoserver_username ]; then
    export GEOSERVER_USERNAME=$(cat /run/secrets/geoserver_username)
else
    echo "Secret file 'geoserver_username' not found!" >&2
fi
if [ -f /run/secrets/geoserver_password ]; then
    export GEOSERVER_PASSWORD=$(cat /run/secrets/geoserver_password)
else
    echo "Secret file 'geoserver_password' not found!" >&2
fi
if [ -f /run/secrets/minio_access_key ]; then
    export MINIO_ACCESS_KEY=$(cat /run/secrets/minio_access_key)
else
    echo "Secret file 'minio_access_key' not found!" >&2
fi
if [ -f /run/secrets/minio_secret_key ]; then
    export MINIO_SECRET_KEY=$(cat /run/secrets/minio_secret_key)
else
    echo "Secret file 'minio_secret_key' not found!" >&2
fi
if [ -f /run/secrets/telegram_bot_token ]; then
    export TELEGRAM_BOT_TOKEN=$(cat /run/secrets/telegram_bot_token)
else
    echo "Secret file 'telegram_bot_token' not found!" >&2
fi
if [ -f /run/secrets/security_jwuc ]; then
    export SECURITY_JWUC=$(cat /run/secrets/security_jwuc)
else
    echo "Secret file 'security_jwuc' not found!" >&2
fi
if [ -f /run/secrets/security_jwac ]; then
    export SECURITY_JWAC=$(cat /run/secrets/security_jwac)
else
    echo "Secret file 'security_jwac' not found!" >&2
fi
if [ -f /run/secrets/mysql_jixel_username ]; then
    export MYSQL_JIXEL_USERNAME=$(cat /run/secrets/mysql_jixel_username)
else
    echo "Secret file 'mysql_jixel_username' not found!" >&2
fi
if [ -f /run/secrets/mysql_jixel_password ]; then
    export MYSQL_JIXEL_PASSWORD=$(cat /run/secrets/mysql_jixel_password)
else
    echo "Secret file 'mysql_jixel_password' not found!" >&2
fi
if [ -f /run/secrets/mysql_geo_jixel_username ]; then
    export MYSQL_GEO_JIXEL_USERNAME=$(cat /run/secrets/mysql_geo_jixel_username)
else
    echo "Secret file 'mysql_geo_jixel_username' not found!" >&2
fi
if [ -f /run/secrets/mysql_geo_jixel_password ]; then
    export MYSQL_GEO_JIXEL_PASSWORD=$(cat /run/secrets/mysql_geo_jixel_password)
else
    echo "Secret file 'mysql_geo_jixel_password' not found!" >&2
fi

chmod -R o+w /var/www/jixel/tmp/
chmod -R o+w /var/www/jixel/logs/

/usr/sbin/php-fpm
chown -R $httpd_user:$httpd_group /run/php-fpm
/usr/sbin/httpd -DFOREGROUND
