#!/bin/bash
# In consul-template's exec mode (https://github.com/hashicorp/consul-template/blob/master/docs/modes.md#exec-mode)
# subprocesses do not run in a shell, so we use this wrapper script to correctly set the working directory
# and then exec to the desired process. We use exec so that signals are passed to the process correctly.

. /etc/lb-startup-common.sh

cd /code/listenbrainz

log run-lb-command starting "$@"

# we grep the config file rendered by consul to check for missing keys
keyerrors=$(grep KEYDOESNOTEXIST listenbrainz/config.py)
if [ -n "$keyerrors" ]; then
  log "Found keys that are missing from consul"
  echo "$keyerrors"
  log "Config file is as follows"
  cat listenbrainz/config.py
  exit 10
fi
serviceerrors=$(grep SERVICEDOESNOTEXIST listenbrainz/config.py)
if [ -n "$serviceerrors" ]; then
  log "Found services that are missing from consul"
  echo "$serviceerrors"
  log "Config file is as follows"
  cat listenbrainz/config.py
  exit 11
fi

exec "$@"
