#!/bin/sh

# EXPORT
export EXEM_HOME={{ EXEM_HOME }}
export EXEM_HOST_HOME={{ EXEM_HOME }}/host
export RECEIVER_ADDR={{ RECEIVER_ADDR }}:{{ RECEIVER_PORT | default('9010') }}


# START
PIDFILE=${EXEM_HOST_HOME}/data/host_agent_pid

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

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

chmod 755 ${EXEM_HOST_HOME}/bin/exem-host-agent

ulimit -n 1024

${EXEM_HOST_HOME}/bin/exem-host-agent -D