#!/bin/bash
# This file is part of Karabo.
#
# http://www.karabo.eu
#
# Copyright (C) European XFEL GmbH Schenefeld. All rights reserved.
#
# Karabo is free software: you can redistribute it and/or modify it under
# the terms of the MPL-2 Mozilla Public License.
#
# You should have received a copy of the MPL-2 Public License along with
# Karabo. If not, see <https://www.mozilla.org/en-US/MPL/2.0/>.
#
# Karabo is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.

#check the existDb docker image
IMAGE=europeanxfel/existdb:2.2
CONTAINER=karabo_existdb

#check if the image is already available
docker images $IMAGE --format "{{.Repository}}:{{.Tag}}" | grep $IMAGE > /dev/null
if [[ $? -ne 0 ]]; then
    # pull in case we miss this image
    docker pull $IMAGE
    if [[ $? -ne 0 ]]; then
        echo "ERROR: is docker installed?"
        exit 1
    fi
fi

EXIST_DATA_DIR=$KARABO/var/data/exist_data
mkdir -p $EXIST_DATA_DIR

# unfortunately docker ps will always return 0, so we need to grep the output
docker ps --filter name=$CONTAINER | grep $CONTAINER > /dev/null
if [[ $? -eq 0 ]]; then
    # already running, exit quietly
    exit 0
fi

docker ps --all --filter name=$CONTAINER | grep $CONTAINER > /dev/null
# if no container from the image is running, start it.
if [[ $? -ne 0 ]]; then
    docker run -d -p 8181:8080 -v $EXIST_DATA_DIR:/opt/exist_data/export/ --name $CONTAINER $IMAGE > /dev/null
else
    docker port $CONTAINER | grep "8080/tcp -> 0.0.0.0:8181" > /dev/null
    if [[ $? -ne 0 ]]; then
        docker rm $CONTAINER > /dev/null
        docker run -d -p 8181:8080 -v $EXIST_DATA_DIR:/opt/exist_data/export/ --name $CONTAINER $IMAGE > /dev/null
    else
        docker start $CONTAINER > /dev/null
    fi
fi


if [[ $? -eq 0 ]]; then
    echo "------------------------------------------"
    echo "Configuration DB was started."
    echo "You can access the backend at: http://localhost:8181/exist/"
    exit 0
else
    echo "------------------------------------------"
    echo "Error Starting the Configuration DB"
    exit 1
fi
