#!/usr/bin/env bash

# Ensure user is set correctly
if [ "$SUPERVISOR_FLASK_USER" != "root" ] && [ "$SUPERVISOR_FLASK_USER" != "trustapp" ]; then
    echo "You should set SUPERVISOR_FLASK_USER to either 'trustapp' or 'root'."
    exit 1
fi

# Update user id if needed
if [ ! -z "$WWWUSER" ]; then
    usermod -u "$WWWUSER" trustapp
fi

# Start supervisord
if [ $# -gt 0 ]; then
    if [ "$SUPERVISOR_FLASK_USER" = "root" ]; then
      exec "$@"
    else
      exec gosu "$WWWUSER" "$@"
    fi
else
    exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
fi
