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

#
# Run Script for AURA Engine
#
# Call with one of these parameters:
#
# - dev
# - prod
# - debug
# - log

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

if [[ $* =~ ^(dev|prod|debug|log)$ ]]; then
	mode=$1
fi

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


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


# +++ DEFAULT COMMANDS +++ #

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

	### Runs Engine Core in Development (Liquidsoap) ###

	if [[ $mode == "dev" ]]; then
		(cd src && $LQS_CMD ./engine.liq)
	fi

	### Runs Engine Core in Production (Liquidsoap) ###

	if [[ $mode == "prod" ]]; then
		(cd src && $LQS_CMD ./engine.liq)
	fi

	### Runs Engine Core (Verbose & debug output) ###

	if [[ $mode == "debug" ]]; then
		(cd src && $LQS_CMD  --verbose --debug ./engine.liq)
	fi

	### Tails the log file only (Used for Docker debugging) ###

	if [[ $mode == "log" ]]; then
		tail -f logs/engine-core.log
	fi
fi


# +++ DOCKER COMMANDS +++ #

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

	### Runs Engine Core ###

	if [[ $mode == "dev" ]]; then
		exec sudo docker run \
			--network="host" \
			--name aura-engine-core \
			--rm \
			-u $UID:$GID \
			-v "$BASE_DIR/config/engine-core.docker.ini":"/srv/config/engine-core.ini":ro \
			-v "$BASE_DIR/socket":"/srv/socket" \
			-v "$AUDIO_DIR/source":"/var/audio/source":ro \
			-v "$AUDIO_DIR/playlist":"/var/audio/playlist":ro \
			-v "$AUDIO_DIR/station":"/var/audio/station":ro \
			-v "$BASE_DIR/logs":"/srv/logs" \
			-v "/etc/asound.conf":"/etc/asound.conf" \
			--mount type=tmpfs,destination=/tmp \
			--device /dev/snd \
			--group-add audio \
			autoradio/engine-core
	fi

	### Debugging mode: only tails the log file and enter the container manually ###

	if [[ $mode == "debug" ]]; then
		exec sudo docker run \
			--network="host" \
			--name aura-engine-core \
			--rm \
			-u $UID:$GID \
			-v "$BASE_DIR/config/engine-core.docker.ini":"/srv/config/engine-core.ini":ro \
			-v "$BASE_DIR/socket":"/srv/socket" \
			-v "$AUDIO_DIR/source":"/var/audio/source":ro \
			-v "$AUDIO_DIR/playlist":"/var/audio/playlist":ro \
			-v "$AUDIO_DIR/station":"/var/audio/station":ro \
			-v "$BASE_DIR/contrib":"/srv/contrib" \
			-v "$BASE_DIR/tests":"/srv/tests" \
			-v "$BASE_DIR/logs":"/srv/logs" \
			-v "$BASE_DIR/contrib":"/srv/contrib" \
			-v "$BASE_DIR/tests":"/srv/tests" \
			-v "/etc/asound.conf":"/etc/asound.conf" \
			--privileged \
			--memory=8g --memory-reservation=4g \
			--cpus=4.0 --cpu-shares=3000 \
			--mount type=tmpfs,destination=/tmp \
			--device /dev/snd \
			--group-add audio \
			autoradio/engine-core log
	fi

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

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

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

	if [[ $mode == "push" ]]; then
		docker push autoradio/engine-core
	fi
fi