#!/usr/bin/env bash
set -e

# The container name generated by docker compose are different when using different versions of docker-compose.
# docker-compose V1: prefix_serviceName_1
# docker-compose V2: prefix-serviceName-1
# So we use docker inspect to get the container name every time.

server_container_name=`docker inspect -f '{{.Name}}' $(docker-compose ps -q server) | cut -c2-`
agent_container_name=`docker inspect -f '{{.Name}}' $(docker-compose ps -q agent-image) | cut -c2-`


echo [agent] install agent to $server_container_name

docker exec $server_container_name sudo mkdir -p /usr/local/holoinsight/agent/bin
docker exec $server_container_name sudo mkdir -p /usr/local/holoinsight/agent/data
docker exec $server_container_name sudo mkdir -p /usr/local/holoinsight/agent/logs
docker exec $server_container_name sudo mkdir -p /usr/local/holoinsight/agent/conf

temp=`mktemp`

docker cp $agent_container_name:/usr/local/holoinsight/agent/bin/agent $temp

docker cp $temp $server_container_name:/usr/local/holoinsight/agent/bin/agent
docker cp ./agent.yaml $server_container_name:/usr/local/holoinsight/agent/
docker cp ./agent.ini $server_container_name:/etc/supervisord.d/agent.ini

docker exec $server_container_name bash -c 'sc reread && sc add agent'
