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

#
# Run Script for AURA Engine
#
# Call with one of these parameters:
#
# - dev
# - prod
# - test
# - recreate-database

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

if [[ $* =~ ^(dev|prod|test|recreate-database)$ ]]; then
	mode=$1
fi

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


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


# Check for the correct Python version (3.8+)
PYTHON_EXEC="python3"
echo "[ Using $(python3 -V) ]"


# +++ DEFAULT COMMANDS +++ #

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

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

	if [[ $mode == "dev" ]]; then
		source python/bin/activate
		echo "Running Engine in Python Environment ($(python3 -V))"
		python run.py
	fi

	### Runs Engine ###

	if [[ $mode == "prod" ]]; then
		/usr/bin/env $PYTHON_EXEC run.py
	fi

	### Runs Tests ###

	if [[ $mode == "test" ]]; then
		source python/bin/activate
		echo "Running Engine Tests in Python Environment ($(python3 -V))"
		python -m unittest discover tests
	fi

	### CAUTION: This deletes everything in your database ###

	if [[ $mode == "recreate-database" ]]; then
		/usr/bin/env $PYTHON_EXEC run.py --recreate-database
	fi

fi


# +++ DOCKER COMMANDS +++ #

if [[ $docker == "true" ]]; then
	BASE_DIR=$(readlink -f .)
	echo "Absolute base dir: " $BASE_DIR

	### Runs Engine Docker Container (Local build for development, if n/a pull from Docker Hub) ###

	if [[ $mode == "dev" ]]; then

		if [[ -z "${AURA_ENGINE_CONFIG_PATH}" ]]; then
			AURA_ENGINE_CONFIG_PATH=$(readlink -f "${BASE_DIR}/config/docker.engine.ini")
		fi

		if [[ -z "${AURA_ENGINE_SOCKET_DIR}" ]]; then
			AURA_ENGINE_SOCKET_DIR=$(readlink -f "${BASE_DIR}/../engine-core/socket")
		fi

		if [[ -z "${AURA_ENGINE_CONTROL_CHANNEL}" ]]; then
			AURA_ENGINE_CONTROL_CHANNEL="127.0.0.1:1337"
		fi

		if [[ -z "${AURA_ENGINE_AUDIO_SOURCE_DIR}" ]]; then
			AURA_ENGINE_AUDIO_SOURCE_DIR=$(readlink -f "${BASE_DIR}/audio/source")
		fi

		if [[ -z "${AURA_ENGINE_AUDIO_PLAYLIST_DIR}" ]]; then
			AURA_ENGINE_AUDIO_PLAYLIST_DIR=$(readlink -f "${BASE_DIR}/audio/playlist")
		fi

		echo AURA_ENGINE_CONFIG_PATH="$AURA_ENGINE_CONFIG_PATH"
		echo AURA_ENGINE_SOCKET_DIR="$AURA_ENGINE_SOCKET_DIR"
		echo AURA_ENGINE_CONTROL_CHANNEL="$AURA_ENGINE_CONTROL_CHANNEL"
		echo AURA_ENGINE_AUDIO_SOURCE_DIR="$AURA_ENGINE_AUDIO_SOURCE_DIR"
		echo AURA_ENGINE_AUDIO_PLAYLIST_DIR="$AURA_ENGINE_AUDIO_PLAYLIST_DIR"

		exec sudo docker run \
			--network="host" \
			--name aura-engine \
			--rm \
			-u $UID:$GID \
			-p "$AURA_ENGINE_CONTROL_CHANNEL":1337/tcp \
			-v "$AURA_ENGINE_CONFIG_PATH":"/srv/config/engine.ini" \
			-v "$AURA_ENGINE_SOCKET_DIR":"/srv/socket" \
			-v "$AURA_ENGINE_AUDIO_SOURCE_DIR":"/var/audio/source":ro \
			-v "$AURA_ENGINE_AUDIO_PLAYLIST_DIR":"/var/audio/playlist" \
			-v "$BASE_DIR/logs":"/srv/logs" \
			-e TZ=Europe/Vienna \
			autoradio/engine
	fi

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

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

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

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