#!/bin/bash
set -euo pipefail

BASEDIR=$(dirname $0)
echo "Script location: ${BASEDIR}"

TMPDIR=${BASEDIR}/tmp
CERT_DIR=${TMPDIR}/k8s-webhook-server/serving-certs

mkdir -p ${CERT_DIR}
for file in ca.crt tls.crt tls.key; do
  if [ ! -f ${CERT_DIR}/${file} ]; then
    continue
  fi

  escapedFile="${file//\./\\.}"
  kubectl --context=arn:aws:eks:us-west-2:385595570414:cluster/dev-base-us-west-2 \
    -n infra get secrets infra-webhook-cert-v2 -ojsonpath="{.data.${escapedFile}}" | \
    base64 -d > ${CERT_DIR}/${file}
done

echo "Running webhook locally"
cd $(dirname ${BASEDIR}); TMPDIR=${TMPDIR} go run main.go
