#!/usr/bin/env bash
#
# HELPER PURPOSE
#
# Checks availability of service containers

# Set error handling
set -eu -o pipefail

DOCKERIZE_VERSION=v0.3.0

if hash wget 2>/dev/null; then
	wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz;
elif hash curl 2>/dev/null; then
	curl -XGET -L https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz --output dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz;
else
	echo 'wget or curl are required to run helper-check-service-ready'
	exit 1
fi
mkdir -p ~/tmp
tar -C ~/tmp -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz

for var in "$@"
do
    case $var in
    redis)
      ~/tmp/dockerize -wait tcp://localhost:6379 -timeout 1m
      ;;
    neo4j)
      ~/tmp/dockerize -wait tcp://localhost:7687 -timeout 1m
      ;;
    mongo)
      ~/tmp/dockerize -wait tcp://localhost:27017 -timeout 1m
      ;;
    esac
done
