#!/bin/sh
set -eu

scratch=/tmp/roundcube
rm -rf "$scratch"
mkdir -p "$scratch/temp"

mkdir "$scratch/plugins"
ln -s /opt/roundcube/plugins/* "$PWD"/plugins/* "$scratch/plugins/"

export ROUNDCUBE_SCRATCH="$scratch"
export RCUBE_CONFIG_PATH="/opt/roundcube/config:$PWD"
ROUNDCUBE_DES_KEY="$(head -c 18 /dev/urandom | base64)"
export ROUNDCUBE_DES_KEY

# Development clusters' test mail server (MAIL=1 make k8s-up); sandboxes elsewhere have no DNS.
greenmail=greenmail.ivy-mail.svc.cluster.local
if timeout 2 getent hosts "$greenmail" >/dev/null 2>&1; then
  export ROUNDCUBE_GREENMAIL="$greenmail"
fi

export PHP_CLI_SERVER_WORKERS=4
exec php -d "auto_prepend_file=$PWD/prepend.php" -S 127.0.0.1:8080 -t /opt/roundcube/public_html
