#!/bin/bash
# Default mode
mode="dev"
docker="false"

#
# Run Script for AURA Steering
#
# Call with one of these parameters:
#
# - init
# - dev
# - prod
# - test

# - docker:build
# - docker:push
# - docker:dev
# 

if [[ $* =~ ^(init|init-db|dev|prod|test|build|push|serve)$ ]]; then
	mode=$1
fi

if [[ "$1" == *"docker:"* ]]; then
	docker="true"
	mode=${1#*:}
fi


echo "[ Run mode=$mode ]"
echo "[ Docker=$docker ]"



# +++ DEFAULT COMMANDS +++ #

if [[ $docker == "false" ]]; then

	if [[ $mode == "init-db" ]]; then
		echo "apply database migrations"
		./manage.py migrate
    echo "collect static resources"
    ./manage.py collectstatic --clear --noinput
		echo "create django superuser"
		./manage.py createsuperuser --noinput
		echo "create rsa key"
		./manage.py creatersakey
		if [[ -z ${TANK_CALLBACK_BASE_URL} ]]; then
			TANK_CALLBACK_BASE_URL="${AURA_PROTO}://${AURA_HOST}/tank"
		fi
		if [[ -z ${DASHBOARD_CALLBACK_BASE_URL} ]]; then
			DASHBOARD_CALLBACK_BASE_URL="${AURA_PROTO}://${AURA_HOST}"
		fi
		echo "create dashboard client with client id $DASHBOARD_OIDC_CLIENT_ID"
    	./manage.py create_oidc_client dashboard public --client-id $DASHBOARD_OIDC_CLIENT_ID --client-secret $DASHBOARD_OIDC_CLIENT_SECRET -r "id_token token" -u ${DASHBOARD_CALLBACK_BASE_URL}/oidc_callback.html -u ${DASHBOARD_CALLBACK_BASE_URL}/oidc_callback_silentRenew.html -p ${DASHBOARD_CALLBACK_BASE_URL} -p ${DASHBOARD_CALLBACK_BASE_URL}/
    	echo "create tank client with client id $TANK_OIDC_CLIENT_ID"
		./manage.py create_oidc_client tank confidential --client-id $TANK_OIDC_CLIENT_ID --client-secret $TANK_OIDC_CLIENT_SECRET -r "code" -u ${TANK_CALLBACK_BASE_URL}/auth/oidc/callback
	fi

	### Initializes the project (Development) ###

	if [[ $mode == "init" ]]; then
		echo "Creating Python VirtualEnvironment"
		python3.8 -m venv python
		echo "Activate Environment"
		source python/bin/activate
        echo "Install dependencies"
        pip3 install -r requirements.txt
        echo "Next you need to create the configuration, run fixtures, migrations and create a superuser."
	fi

	### Runs Steering in development mode (Virtualenv) ###

	if [[ $mode == "dev" ]]; then
		echo "Activating Python Environment"
		source python/bin/activate
        python manage.py runserver
	fi

	### Runs Steering in production mode (WSGI) ###

	if [[ $mode == "prod" ]]; then
		echo "!!! Not yet implemented !!!"
		#gunicorn -c config/gunicorn.conf.py src.app:app
	fi

	### Runs the test-cases ###

	if [[ $mode == "test" ]]; then
		echo "!!! Not yet implemented !!!"
	fi

fi


# +++ DOCKER COMMANDS +++ #

if [[ $docker == "true" ]]; then
	BASE_D=$(realpath "${BASH_SOURCE%/*}/")

	### Runs Steering from Docker ###

	if [[ $mode == "dev" ]]; then
		exec sudo docker run --rm -it \
            -u $UID:$GID \
            -p 127.0.0.1:8000:8000 \
            -v "$BASE_D":/srv autoradio/steering /srv/manage.py \
            runserver 0.0.0.0:8000
	fi

	### Create Docker Image from local project ###

	if [[ $mode == "build" ]]; then
		exec sudo docker build -t autoradio/steering .
	fi

	### Pushes the latest Docker Image to Docker Hub ###

	if [[ $mode == "push" ]]; then
		exec sudo docker push autoradio/steering
	fi
fi
