#!/bin/sh

if [ -f /app/var/data/dump.rdb ]; then
  rm -f /app/var/data/dump.rdb
fi

if ! /usr/local/bin/init-secrets; then
  echo "[entrypoint] FATAL: init-secrets failed — refusing to start" >&2
  exit 1
fi

if [ "$(id -u)" = "0" ]; then
  if ! php /services/api/bin/fix-ownership; then
    echo "[entrypoint] FATAL: storage ownership fix failed" >&2
    exit 1
  fi
fi

if [ -f /app/var/data/keys/passphrase ]; then
  export JWT_PASSPHRASE=$(cat /app/var/data/keys/passphrase)
fi

if [ -f /app/var/data/keys/mercure_secret ]; then
  export MERCURE_JWT_SECRET=$(cat /app/var/data/keys/mercure_secret)
fi

if [ -f /app/var/data/keys/app_secret ]; then
  export APP_SECRET=$(cat /app/var/data/keys/app_secret)
fi

delegate slink slink dotenv:dump prod --quiet

exec /init "$@"