#!/bin/sh

# Open a shell on one of the DBT-2 Docker images.

# Use the specified engine from the environment, or try podman then docker.
if [ "$ENGINE" = "" ]; then
	ENGINE="podman"
	if ! which $ENGINE > /dev/null 2>&1; then
		ENGINE="docker"
		if ! which $ENGINE > /dev/null 2>&1; then
			echo "podman nor docker in path"
			exit 1
		fi
	fi
elif ! which "$ENGINE" > /dev/null 2>&1; then
	echo "specified engine $ENGINE not in path"
	exit 1
fi

usage()
{
	echo "usage: $(basename "$0") dbt2-base"
	echo "usage: $(basename "$0") dbt2-client"
	echo "usage: $(basename "$0") dbt2-database <dbms> <warehouses>"
	echo "usage: $(basename "$0") dbt2-driver"
}

DBMS=""
DBT2_TAG=""
CONTAINER_TAG=""
SF=""

if [ $# -lt 1 ] || [ $# -gt 3 ]; then
	usage
	exit 1
fi

DBT2_TAG="$1"

if [ $# -eq 1 ]; then
	if [ ! "$DBT2_TAG" = "dbt2-base" ] && \
			[ ! "$DBT2_TAG" = "dbt2-driver" ]; then
		usage
		exit 1
	fi
	CONTAINER_TAG="$DBT2_TAG"
fi

DBMS="$2"
if [ $# -eq 2 ]; then
	usage
	exit 1
fi

SF="$3"
if [ $# -eq 3 ]; then
	if [ ! "$DBT2_TAG" = "dbt2-database" ]; then
		usage
		exit 1
	fi
	CONTAINER_TAG="${DBT2_TAG}-${DBMS}-${SF}w"
fi

$ENGINE run --rm -i -t "${CONTAINER_TAG}" /bin/bash
