#!/bin/sh
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.

if [ $# -ne 1 ]; then
  echo "entrypoint requires the handler name to be the first argument" 1>&2
  exit 142
fi
export _HANDLER="$1"

RUNTIME_ENTRYPOINT="/usr/local/bin/python3 -m awslambdaric ${_HANDLER}"
if [ -z "${AWS_LAMBDA_RUNTIME_API}" ]; then
  exec /usr/local/bin/aws-lambda-rie ${RUNTIME_ENTRYPOINT}
else
  export SENTRY_DSN=$(aws secretsmanager get-secret-value --secret-id ${SENTRY_DSN} | jq -r '.SecretString' | jq -r '.slz_backfill_lambda')

  # delphi/dev/slz/storage/pg_proxy/user
  DELPHI_SLZ=$(aws secretsmanager get-secret-value --secret-id ${DELPHI_SLZ_DB_CREDENTIALS} | jq -r '.SecretString')
  export DELPHI_SLZ_DB_HOST=$(echo "${DELPHI_SLZ}" | jq -r '.active_endpoint')
  export DELPHI_SLZ_DB_USER=$(echo "${DELPHI_SLZ}" | jq -r '.username')
  export DELPHI_SLZ_DB_PASS=$(echo "${DELPHI_SLZ}" | jq -r '.password')
  export DELPHI_SLZ_DB_PORT=$(echo "${DELPHI_SLZ}" | jq -r '.port')
  export DELPHI_SLZ_DB_NAME=$(echo "${DELPHI_SLZ}" | jq -r '.database')
  export SLZDB_URI="postgresql://${DELPHI_SLZ_DB_USER}:${DELPHI_SLZ_DB_PASS}@${DELPHI_SLZ_DB_HOST}:${DELPHI_SLZ_DB_PORT}/${DELPHI_SLZ_DB_NAME}"

  exec ${RUNTIME_ENTRYPOINT}
fi
