#!/bin/bash -eu

<% unless p("cf_networking.disable") %>
set -o pipefail

source /var/vcap/packages/ctl-utils/ctl_util.sh

trap 'handle_orphaned_server silk-controller ${pid}' TERM

/var/vcap/packages/silk-controller/bin/silk-controller \
  -config="${CONF_DIR}"/silk-controller.json \
  2> >(tee -a "${LOG_DIR}"/silk-controller.stderr.log | logger -p user.error -t silk-controller) \
  1> >(tee -a "${LOG_DIR}"/silk-controller.stdout.log | logger -t silk-controller) &

pid=$!

healthy=$(wait_for_server_to_become_healthy "${URL}" "${TIMEOUT}")
write_pid "${healthy}" "${pid}"
<% end %>
