#!/bin/bash

# Run glbc. First run `setup-local.sh` to set things up.
#
# Files touched: /tmp/kubectl-proxy.log /tmp/glbc.log

GCECONF=${GCECONF:-/tmp/gce.conf}
GLBC=${GLBC:-./glbc}
PORT=${PORT:-7127}
V=${V:-3}

usage() {
    echo "Usage: $(basename "$0")"
    echo "    [--gceconf /path/to/gce.conf]"
    echo "    [--glbc /path/to/glbc]"
    echo "    [--port 7127]"
    echo "    [--v 3]"

    exit 0
}

while [[ $# -gt 0 ]]; do
    case "$1" in
        --gceconf)
            GCECONF="$2"
            shift 2
            ;;
        --glbc)
            GLBC="$2"
            shift 2
            ;;
        --port)
            PORT="$2"
            shift 2
            ;;
        --v)
            V="$2"
            shift 2
            ;;
        -h|--help)
            usage
            ;;
        *)
            break
            ;;
    esac
done

GOOGLE_APPLICATION_CREDENTIALS="${HOME}/.config/gcloud/application_default_credentials.json"

if [ ! -r ${GOOGLE_APPLICATION_CREDENTIALS} ]; then
    echo "You must login your application default credentials"
    echo "$ gcloud auth application-default login"
    exit 1
fi

echo "GCECONF=${GCECONF} GLBC=${GLBC} PORT=${PORT} V=${V}"

if [ ! -x "${GLBC}" ]; then
    echo "ERROR: No ${GLBC} executable found" >&2
    exit 1
fi

echo "$(date) start" >> /tmp/kubectl-proxy.log
kubectl proxy --port="${PORT}" \
    >> /tmp/kubectl-proxy.log &

PROXY_PID=$!
cleanup() {
    echo "Killing proxy (pid=${PROXY_PID})"
    kill ${PROXY_PID}
}
trap cleanup EXIT

kubectl apply -f docs/deploy/resources/default-http-backend.yaml

sleep 2 # Wait for proxy to start up
${GLBC} \
    --apiserver-host=http://localhost:${PORT} \
    --running-in-cluster=false \
    --enable-label-propagation=true \
    --logtostderr --v=${V} \
    --config-file-path=${GCECONF} \
    "${@}" \
    2>&1 | tee -a /tmp/glbc.log
