#!/usr/bin/env sh

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

################################################################################
#
# Cake is a shell script for invoking CakePHP shell commands
#
# CakePHP(tm) :  Rapid Development Framework (https://cakephp.org)
# Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
#
# Licensed under The MIT License
# For full copyright and license information, please see the LICENSE.txt
# Redistributions of files must retain the above copyright notice.
#
# @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
# @link          https://cakephp.org CakePHP(tm) Project
# @since         1.2.0
# @license       https://opensource.org/licenses/mit-license.php MIT License
#
################################################################################

# Canonicalize by following every symlink of the given name recursively
canonicalize() {
    NAME="$1"
    if [ -f "$NAME" ]
    then
        DIR=$(dirname -- "$NAME")
        NAME=$(cd -P "$DIR" > /dev/null && pwd -P)/$(basename -- "$NAME")
    fi
    while [ -h "$NAME" ]; do
        DIR=$(dirname -- "$NAME")
        SYM=$(readlink "$NAME")
        NAME=$(cd "$DIR" > /dev/null && cd "$(dirname -- "$SYM")" > /dev/null && pwd)/$(basename -- "$SYM")
    done
    echo "$NAME"
}

# Find a CLI version of PHP
findCliPhp() {
    for TESTEXEC in php php-cli /usr/local/bin/php
    do
        SAPI=$(echo "<?= PHP_SAPI ?>" | $TESTEXEC 2>/dev/null)
        if [ "$SAPI" = "cli" ]
        then
            echo $TESTEXEC
            return
        fi
    done
    echo "Failed to find a CLI version of PHP; falling back to system standard php executable" >&2
    echo "php";
}

# If current path is a symlink, resolve to real path
realname="$0"
if [ -L "$realname" ] 
then
	realname=$(readlink -f "$0")
fi

CONSOLE=$(dirname -- "$(canonicalize "$realname")")
APP=$(dirname "$CONSOLE")

# If your CLI PHP is somewhere that this doesn't find, you can define a PHP environment
# variable with the correct path in it.
if [ -z "$PHP" ]
then
    PHP=$(findCliPhp)
fi

if [ "$(basename "$realname")" != 'cake' ]
then
    exec "$PHP" "$CONSOLE"/cake.php "$(basename "$realname")" "$@"
else
    exec "$PHP" "$CONSOLE"/cake.php "$@"
fi

exit
