#!/usr/bin/env bash

set -euo pipefail

# Value of GARCON_FLOW_NAME to verify it's set correctly
printf 'Using GARCON_FLOW_NAME: "%s"\n' "$GARCON_FLOW_NAME"

# Check if CUSTOM_SUPERVISOR_CONF environment variable is set, otherwise default to conf/supervisor.conf
# Example: CUSTOM_SUPERVISOR_CONF=conf/beatport_supervisor.conf
SUPERVISOR_CONF=${CUSTOM_SUPERVISOR_CONF:-conf/supervisor.conf}

printf '"%s" file is being used\n' "${SUPERVISOR_CONF}"

echo "Updateing permissions on /mnt/efs"
chown -R worker:worker /mnt/efs || true

echo "Creating log pipe at /tmp/logpipe"
mkfifo -m 600 /tmp/logpipe
chown worker:worker /tmp/logpipe
unbuffer cat /tmp/logpipe &

# Start supervisord with the chosen configuration file
exec gosu worker supervisord -c ${SUPERVISOR_CONF}
