#!/bin/bash

# Sets up services required for running Dockerized PsyNet commands
# Ensures that the script stops on errors
set -euo pipefail

echo "Checking Docker access..."
if [[ "$(docker network ls | grep "could not be found in this WSL 2 distro")" != "" ]]
then
  echo "Docker installation could not be found in this WSL 2 distro. Did you remember to launch Docker Desktop?"
  exit 1
fi

echo "Confirming that the Dallinger network exists..."
if [[ "$(docker network ls | grep dallinger)" = "" ]]
then
  echo "...no. Creating now..."
  docker network create dallinger
else
  echo "...yes."
fi

echo "Confirming that dallinger_redis is running..."
docker start dallinger_redis || true
if [[ "$(docker ps | grep dallinger_redis)" = "" ]]
then
  echo "...no. Creating now..."
  docker run -d --name dallinger_redis --network=dallinger \
    -v dallinger_redis:/data \
    redis redis-server \
    --appendonly yes
else
  echo "...yes."
fi

echo "Confirming that dallinger_postgres is running..."
docker start dallinger_postgres || true
if [[ "$(docker ps | grep dallinger_postgres)" = "" ]]
then
  echo "...no. Creating now..."
  docker run -d --name dallinger_postgres --network=dallinger \
  -e POSTGRES_USER=dallinger \
  -e POSTGRES_PASSWORD=dallinger \
  -e POSTGRES_DB=dallinger \
  -v dallinger_postgres:/var/lib/postgresql/data \
  postgres:12
else
  echo "...yes."
fi
