#!/bin/sh

# Get ENVs from secrets
if test -f "$SECRET_KEY_FILE"; then
  export SECRET_KEY=$(cat $SECRET_KEY_FILE)
fi
if test -f "$EMAIL_API_KEY_FILE"; then
  export EMAIL_API_KEY=$(cat $EMAIL_API_KEY_FILE)
fi
if test -f "$DB_PASSWD_FILE"; then
  export DB_PASSWD=$(cat $DB_PASSWD_FILE)
fi
if test -f "$G_PRIVATE_KEY_ID_FILE"; then
  export G_PRIVATE_KEY_ID=$(cat $G_PRIVATE_KEY_ID_FILE)
fi
if test -f "$G_PRIVATE_KEY_FILE"; then
  export G_PRIVATE_KEY="$(cat $G_PRIVATE_KEY_FILE)"
fi
if test -f "$G_CLIENT_EMAIL_FILE"; then
  export G_CLIENT_EMAIL=$(cat $G_CLIENT_EMAIL_FILE)
fi
if test -f "$G_CLIENT_ID_FILE"; then
  export G_CLIENT_ID=$(cat $G_CLIENT_ID_FILE)
fi
if test -f "$G_CLIENT_URL_FILE"; then
  export G_CLIENT_URL=$(cat $G_CLIENT_URL_FILE)
fi
if test -f "$GROUP_KEY_FILE"; then
  export GROUP_KEY=$(cat $GROUP_KEY_FILE)
fi

# Collect static files
echo "Collect static files"
python manage.py collectstatic --noinput

# Apply database migrations
echo "Apply database migrations"
python manage.py migrate

# Start server
echo "Django running on http://localhost:8000 in production mode"
gunicorn --log-level debug -w 4 -b 0.0.0.0:8000 sikweb.wsgi
