#!/bin/sh

# EXPORT
export EXEM_HOME={{ EXEM_HOME }}
export EXEM_CONTAINER_HOME={{ EXEM_HOME }}/container
export COMMON_RECEIVER_IP={{ RECEIVER_ADDR }}
export COMMON_RECEIVER_PORT={{ RECEIVER_CONTAINER_PORT | default('9009') }}
export CONTAINER_LOG_LEVEL=info


# START
PIDFILE=${EXEM_CONTAINER_HOME}/data/container_agent_pid

if [ -f "${PIDFILE}" ]; then
  PID=$(echo -n | cat ${PIDFILE})
  IS_RUNNING=$(ps -fp "${PID}" | grep -c exem-container-agent)

  if [ "${IS_RUNNING}" -ne 0 ]; then
    echo "exem-container-agent is already running (${PID})"
    exit
  fi
fi

chmod 755 ${EXEM_CONTAINER_HOME}/bin/exem-container-agent

${EXEM_CONTAINER_HOME}/bin/exem-container-agent run