#!/bin/bash
set -e

docker-compose -p %image_tag% build --build-arg env="%reverse.dep.*.build_mode%"
docker-compose -p %image_tag% up -d
container_id=$(docker-compose -p %image_tag% ps -q webbpm-app)
addr=%teamcity.agent.hostname%
port_web=$(docker-compose -p %image_tag% port webbpm-app 8080 | cut -d: -f2)
port_mgmt=$(docker-compose -p %image_tag% port webbpm-app 9990| cut -d: -f2)
port_debug=$(docker-compose -p %image_tag% port webbpm-app 8787| cut -d: -f2)


echo "Started container $container_id"

echo "Management URL: http://$addr:$port_mgmt/"
echo "Webapp URL: http://$addr:$port_web/%APP_PATH%"
echo "Debug URL: $addr:$port_debug"

echo "##teamcity[setParameter name='container_id' value='$container_id']"
echo "##teamcity[setParameter name='webapp_url' value='http://$addr:$port_web/%APP_PATH%']"

echo "Waiting until the app is deployed..."
# Wait until the app is deployed
timeout 600 bash -c "until curl -s -o /dev/null --fail http://${addr}:${port_web}/%APP_PATH%/ ; do sleep 1; done"