#!/bin/bash -ie

usage() {
  echo -n "##################################################
# Start POCS via Docker.
#
##################################################

 $ $(basename $0) [COMMAND]

 Options:
  COMMAND   These options are passed at the end of the docker-compose command.
            To start all service simply pass 'up'.

 Examples:

    # Start all services in the foreground.
    $POCS/bin/pocs up

    #Start specific docker containers in the background with
    $POCS/bin/pocs up --no-deps -d <container name>

    e.g.

    # Start config-server service in the background.
    $POCS/bin/pocs up --no-deps -d config-server

    # Read the logs from the config-server
    $POCS/bin/pocs logs config-server

    # Manually stop docker containers in the with
    docker stop <container name>
"
}

START=${1:-help}
if [ "${START}" = 'help' ] || [ "${START}" = '-h' ] || [ "${START}" = '--help' ]; then
    usage
    exit 1
fi

PARAMS="$@"

cd "$PANDIR"
CMD="docker-compose \
    --project-directory ${PANDIR} \
    -f panoptes-utils/docker/docker-compose.yaml \
    -f POCS/docker/docker-compose-aag.yaml \
    -f POCS/docker/docker-compose.yaml \
    -p panoptes"

# If user only asked to start, check if already running and if so use "-d" option.
if [[ "$PARAMS" == "up" ]]; then
    if [[ ! -z $(eval "${CMD} top") ]]; then
        echo "Some containers already running, using -d to only start non-running containers."
     echo "For more info on managing docker containers manually, run bin/pocs --help".
        PARAMS="up -d"
    fi
fi

# Run the docker-compose command with user params.
eval "${CMD} ${PARAMS}"
