#!/bin/bash

set -o errexit
set -o pipefail

echo $IT_APP_ENVIRONMENT

if [[ $IT_APP_ENVIRONMENT == dev* ]] || [[ $IT_FORCE_RESET_SCHEMA == "true" ]]; then
    echo "IT_APP_ENVIRONMENT=$IT_APP_ENVIRONMENT; IT_FORCE_RESET_SCHEMA=$IT_FORCE_RESET_SCHEMA"
    echo "Resetting the database schema."
    python /app/manage.py reset_schema
else
    echo "IT_APP_ENVIRONMENT=$IT_APP_ENVIRONMENT"
    echo "Migrating the database schema. (no reset)"
    python /app/manage.py migrate
fi

# Use sentry for supercronic only in prod* environments
# FIXME: does not seem to work
#if [[ $IT_APP_ENVIRONMENT == prod* ]]; then
#    sed -i "s|command=/usr/local/bin/supercronic /app/supercronic_crontab|command=/usr/local/bin/supercronic /app/supercronic_crontab -sentry-dsn '$IT_SENTRY_DSN'|" /app/supervisord.conf
#fi

# Create Prüfungslehrgang
python /app/manage.py create_vermittler_pruefung

# Create Motorfahrzeug Prüfungslehrgang
python /app/manage.py create_motorfahrzeug_pruefung

# Set the command to run supervisord
/home/django/.local/bin/supervisord -c /app/supervisord.conf
