#!/bin/bash

export DJANGO_SETTINGS_MODULE="settings.dev_settings"
export PYTHONUNBUFFERED=1

# Vérifier que login_inp-net existe
if [ ! -e 'login_inp-net' ]
then
    read -r -p 'Entrez votre pseudo inp-net : ' pseudo
    echo "$pseudo" > login_inp-net
fi

if [ ! -e 'env/bin/activate' ]; then
  python -m venv env
fi

source env/bin/activate
pip install -r requirements.txt

# Autoriser l'hébergement du CAS sans https
sed -i 's/^        if require_https and not is_scheme_https(service):/#&/' env/lib/python3.*/site-packages/mama_cas/models.py
sed -i 's/^            raise InvalidService("Service %s is not HTTPS" % service)/#&/' env/lib/python3.*/site-packages/mama_cas/models.py

echo "Lancement du tunnel LDAP..."
source "tunnel_ldap.sh"

echo "Migration de la base de donnée..."
./manage.py migrate

echo "Lancement du serveur Django..."
./manage.py runserver localhost:8001

tunnel_close
echo "Tunnel LDAP refermé"
