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

#
# Run Script for AURA Engine
#
# Call with one of these parameters:
#
# - core
# - debug

# - docker:core
# - docker:build
# - docker:push
# 

if [[ $* =~ ^(core|debug)$ ]]; 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

	### Runs Engine Core (Liquidsoap) ###

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

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

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

fi


# +++ DOCKER COMMANDS +++ #

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

	### Runs Engine Core & Liquidsoap ###

	if [[ $mode == "engine" ]]; then
		exec sudo docker run \
			--network="host" \
			--name aura-engine \
			--rm -d \
			-u $UID:$GID \
			-v "$BASE_D":/srv \
			-v "$BASE_D/audio/source":/var/audio/source:ro \
			-v "$BASE_D/audio/station":/var/audio/station:ro \
			-v "$BASE_D/audio/playlist":/var/audio/playlist:ro \
			-v "$BASE_D/config/docker":/etc/aura \
			-v "/dev/snd":/dev/snd \
			--privileged \
			--tmpfs /var/log/aura/ \
			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