#!/bin/bash

# Check if we got already certificate installed
if [ -n "$(kubectl get secret | grep overcast-ingress-tls)" ]; then
    echo Certificate already installed. Enabling HTTPS in Ingress
    tls-helper https on
    exit 0
fi

# =============================================================================
# Wait for HTTPS to become enabled

echo HTTPS is disabled at start
echo Enabling it may take a while
echo You may press Ctrl-C to skip waiting and continue deployment
echo

interrupt=0
trap interrupt=1 INT

while true; do
    POD=$(kubectl get pod|grep ingress-tyk|grep -vF "0/")
    [ -n "$POD" ] && break
    [ $interrupt -eq 1 ] && break
    echo Waiting for \'ingress-tyk\' to become operational
    sleep 30
done

if [ $interrupt -eq 1 ]; then
    echo Wait cancelled. HTTPS is still disabled.
    exit 0
fi

tls-helper create-cert

if [ $? == 0 ]; then
    tls-helper https on
    echo HTTPS support activated!
else
    echo HTTPS support is disabled
fi

if [ $interrupt -eq 1 ]; then
    echo Wait cancelled. HTTPS is still disabled.
fi
