#!/bin/bash

ORIGINAL_ARGS=$*
SCRIPT_DIR=$(dirname $(realpath $0))

cd "$SCRIPT_DIR"

# Default values

# Help message
help_message() {
	echo "Usage: docker [OPTIONS]"
	echo "Options:"
	echo "  --help             This help"
}

function _mkdir {
	mkdir -p $* || {
		echo "mkdir -p $* failed. Exiting."
		exit 12
	}
}

# Parse command-line arguments
while [[ "$#" -gt 0 ]]; do
	case $1 in
		--help)
			help_message
			exit 0
			;;
	esac
	shift
done

is_package_installed() {
	dpkg-query -W -f='${Status}' "$1" 2>/dev/null | grep -c "ok installed"
}

UPDATED_PACKAGES=0

function apt_get_update () {
	if command -v apt >/dev/null; then
		if [[ $UPDATED_PACKAGES == 0 ]]; then
			sudo apt update || {
				echo "apt-get update failed. Are you online?"
				exit 2
			}

			UPDATED_PACKAGES=1
		fi
	else
		echo "apt is not installed. You need to be on debian for auto-installing packages. Please install it manually."
		exit 10
	fi
}

# Check if Docker is installed
if ! command -v docker &>/dev/null; then
	curl -fsSL "https://get.docker.com" | bash

	exit_code=$?

	if [[ $exit_code -ne 0 ]]; then
		echo "Failed to install docker"
		exit 2
	fi
fi

if ! command -v docker-compose &>/dev/null; then
	apt_get_update

	sudo apt-get install -y docker-compose|| {
		echo "sudo apt install -y docker-compose failed"
		exit 3
	}
fi

if ! command -v wget &>/dev/null; then
	apt_get_update

	sudo apt-get install -y wget || {
		echo "sudo apt install -y wget failed"
		exit 3
	}
fi

if [[ -n $DISPLAY ]]; then
	if ! command -v xhost &>/dev/null; then
		apt_get_update
		sudo apt-get install -y xhost || {
			echo "sudo apt install -y xhost failed"
			exit 4
		}
	fi
fi


if ! command -v git &>/dev/null; then
	apt_get_update

	sudo apt-get install -y git || {
		echo "sudo apt install -y git failed"
		exit 6
	}
fi

if ! command -v docker &>/dev/null; then
	echo "docker not found. Cannot continue."
	exit 5
fi

echo "=== Current git hash before auto-pulling ==="
git rev-parse HEAD
echo "=== Current git hash before auto-pulling ==="

git pull

_mkdir runs
_mkdir logs

git rev-parse HEAD > git_hash

if groups | grep docker 2>/dev/null >/dev/null; then
	docker-compose build --build-arg GetMyUsername=$(whoami) || {
		echo "Failed composing container"
		exit 1
	}

	docker-compose up -d || {
		echo "Failed to build container"
		exit 1
	}
else
	echo "Your user is not in the group docker. Thus, docker-compose builds need sudo. Enter your password, please."
	sudo docker-compose build --build-arg GetMyUsername=$(whoami) || {
		echo "Failed composing container"
		exit 1
	}

	sudo docker-compose up -d || {
		echo "Failed to build container"
		exit 1
	}
fi

rm git_hash

if [ -z "$ORIGINAL_ARGS" ]; then
	exit 0
fi

_mkdir $HOME/.config/matplotlib_docker_omniopt

case "$ORIGINAL_ARGS" in
	./omniopt*|omniopt*|./.tests/*|.tests/*)
		if [[ -z $DISPLAY ]]; then
			docker run \
				-v $(pwd)/logs:/var/opt/omniopt/logs:rw \
				-v $(pwd)/runs:/var/opt/omniopt/runs:rw \
				-v $(pwd)/:/var/opt/omniopt/docker_user_dir:rw \
				-v /$HOME/.config/matplotlib_docker_omniopt:$HOME/.config/matplotlib:rw \
				--mount type=tmpfs,destination=/tmp \
				-t --rm \
				ax_omniopt2 bash /var/opt/omniopt/$ORIGINAL_ARGS
		else
			docker run \
				-v $(pwd)/logs:/var/opt/omniopt/logs:rw \
				-v $(pwd)/runs:/var/opt/omniopt/runs:rw \
				-v $(pwd)/:/var/opt/omniopt/docker_user_dir:rw \
				-v $HOME/.config/matplotlib_docker_omniopt:/$HOME/.config/matplotlib:rw \
				--mount type=tmpfs,destination=/tmp \
				--user=$(id -u) \
				--env="DISPLAY" \
				--volume="/etc/group:/etc/group:ro" \
				--volume="/etc/passwd:/etc/passwd:ro" \
				--volume="/etc/shadow:/etc/shadow:ro" \
				--volume="/etc/sudoers.d:/etc/sudoers.d:ro" \
				--volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
				-t --rm ax_omniopt2 \
				bash /var/opt/omniopt/$ORIGINAL_ARGS
		fi
		;;
	*)
		echo "Error: The argument does not start with one of the valid prefixes, i.e. 'omniopt*', '.tests/' or './.tests/*'. Argument was: $ORIGINAL_ARGS"
		exit 1
		;;
esac
