#!/bin/bash
echo ""
echo ""
echo "Verifying Requirements:"
DOCKER_VER=$(docker version -f "{{.Server.Version}}" 2> /dev/null)
if [ -z "$DOCKER_VER" ]; then
    echo "Docker not found; install it: https://docs.docker.com/engine/install/"
    exit 1
fi
MIN_DOCKER_VER=19.0.3
if { echo "$MIN_DOCKER_VER"; echo "$DOCKER_VER"; } | sort --version-sort --check=quiet; then
    echo "Docker Found; OK"
else
    echo "Docker version less than $MIN_DOCKER_VER; upgrade it: https://docs.docker.com/engine/install/"
    exit 1
fi
docker compose version 2>&1 > /dev/null
COMPOSE_PLUGIN_RC=$?
docker-compose --version 2>&1 > /dev/null
COMPOSE_CLI_RC=$?
if [ "$COMPOSE_PLUGIN_RC" -eq 0 ] || [ "$COMPOSE_CLI_RC" -eq 0 ]; then
    echo "Docker Compose found; OK"
    if [ "$COMPOSE_PLUGIN_RC" -eq 0 ]; then
        COMPOSE_COMMAND="docker compose"
    else
	COMPOSE_COMMAND="docker-compose"
    fi
else
    echo "Docker Compose not found; install it: https://docs.docker.com/compose/install/compose-plugin/"
    exit 1
fi
echo ""
echo ""
echo "Building and Running P2Pool docker-compose Configuration"
docker build -t p2pool_config:latest cfg
docker run -it --rm -v $PWD:/docker-compose --user $(id -u):$(id -g) p2pool_config:latest
CONFIGURE_RC=$?
echo ""
echo ""
if [ "$CONFIGURE_RC" -eq 0 ]; then
    echo "P2Pool is configured.  Start the project with: $COMPOSE_COMMAND up --build -d"
else
    exit 1
fi