#!/bin/bash

################################################################################
#
# This script builds a docker image for the marvel 2 restapi.
#        Then it interactively executes the web server in that docker image.
#
# This script will remove a previous container, create a new container,
#       and then will clean up at the end by removing it.
#
################################################################################

set -eau pipefail

###
# build the image
# go to parent(i.e kong) directoy so that it will be consist with all_img_build.sh calling img_build.sh
cd ../
./../scripts/img_build.sh --local_build

###
service="restapi"
version_next=$(git rev-parse --short HEAD)
image_tag="marvel2-$service:$version_next"

# Temp container name
container_name=marvel2-restapi-local-test-temp-container

# Spring profile for local testing
spring_profile="local-mem"

echo
echo "----------------------------------------"
echo "Removing previous container ${container_name} if present ..."
echo "----------------------------------------"
docker container rm ${container_name} || true

echo
echo "----------------------------------------"
echo "Running the container $container_name ..."
echo "----------------------------------------"
# NOTE : Mapping the container port 80 to localhost 8080
docker run \
    --publish 8080:80 \
    --name ${container_name} \
    -ti ${image_tag} \
    /bin/sh /opt/zoox/start_marvel_restapi.sh --profile ${spring_profile} \
    || true
    # ignore exit code



echo
echo "----------------------------------------"
echo "Cleaning up, deleting ${container_name} ..."
echo "----------------------------------------"
docker container rm ${container_name} || true
echo
#
