dockerize -wait tcp://{{ MYSQL_HOST }}:{{ MYSQL_PORT }} -timeout 20s

echo "Loading settings $DJANGO_SETTINGS_MODULE"

./manage.py lms migrate

# Delete obsolete credentials for Android application
./manage.py lms shell -c 'from oauth2_provider.models import get_application_model
get_application_model().objects.filter(name="android").exclude(user__username="login_service_user").delete()'
# Create oauth credentials for Android application
./manage.py lms create_dot_application \
    --client-id android \
    --client-secret {{ ANDROID_OAUTH2_SECRET }} \
    --grant-type password \
    --public \
    --update \
    android \
    login_service_user

# Fix incorrect uploaded file path
if [ -d /openedx/data/uploads/ ]; then
  if [ -n "$(ls -A /openedx/data/uploads/)" ]; then
    echo "Migrating LMS uploaded files to shared directory"
    mv /openedx/data/uploads/* /openedx/media/
    rm -rf /openedx/data/uploads/
  fi
fi

# Create waffle switches to enable some features, if they have not been explicitly defined before
# Completion tracking: add green ticks to every completed unit
(./manage.py lms waffle_switch --list | grep completion.enable_completion_tracking) || ./manage.py lms waffle_switch --create completion.enable_completion_tracking on
