#!/usr/bin/env bash

set -euo pipefail

CONSOLE_IMAGE=${CONSOLE_IMAGE:="quay.io/openshift/origin-console:latest"}
CONSOLE_PORT=${CONSOLE_PORT:=9000}
CONSOLE_IMAGE_PLATFORM=${CONSOLE_IMAGE_PLATFORM:="linux/amd64"}

# Plugin metadata is declared in package.json
PLUGIN_NAME=${npm_package_consolePlugin_name}

echo "Starting local OpenShift console..."

BRIDGE_USER_AUTH="bearer"
BRIDGE_K8S_MODE="off-cluster"
BRIDGE_K8S_AUTH="bearer-token"
BRIDGE_K8S_MODE_OFF_CLUSTER_SKIP_VERIFY_TLS=true
BRIDGE_K8S_MODE_OFF_CLUSTER_ENDPOINT=$(oc whoami --show-server)
BRIDGE_K8S_MODE_OFF_CLUSTER_TOKEN=$(oc whoami -t)
BRIDGE_USER_SETTINGS_LOCATION="localstorage"
BRIDGE_I18N_NAMESPACES="plugin__${PLUGIN_NAME}"

# Don't fail if the cluster doesn't have gitops.
set +e
GITOPS_HOSTNAME=$(oc -n openshift-gitops get route cluster -o jsonpath='{.spec.host}' 2>/dev/null)
set -e
if [ -n "$GITOPS_HOSTNAME" ]; then
    BRIDGE_K8S_MODE_OFF_CLUSTER_GITOPS="https://$GITOPS_HOSTNAME"
fi

echo "API Server: $BRIDGE_K8S_MODE_OFF_CLUSTER_ENDPOINT"
echo "Console Image: $CONSOLE_IMAGE"
echo "Console URL: http://localhost:${CONSOLE_PORT}"
echo "Console Platform: $CONSOLE_IMAGE_PLATFORM"

# Prefer podman if installed. Otherwise, fall back to docker.
if [ -x "$(command -v podman)" ]; then
    if [ "$(uname -s)" = "Linux" ]; then
        # Use host networking on Linux since host.containers.internal is unreachable in some environments.
        BRIDGE_PLUGINS="project-pause-plugin=http://localhost:9001"
        podman run --pull always --platform $CONSOLE_IMAGE_PLATFORM --rm --network=host \
            -e BRIDGE_USER_AUTH="$BRIDGE_USER_AUTH" \
            -e BRIDGE_K8S_MODE="$BRIDGE_K8S_MODE" \
            -e BRIDGE_K8S_AUTH="$BRIDGE_K8S_AUTH" \
            -e BRIDGE_K8S_MODE_OFF_CLUSTER_ENDPOINT="$BRIDGE_K8S_MODE_OFF_CLUSTER_ENDPOINT" \
            -e BRIDGE_K8S_MODE_OFF_CLUSTER_SKIP_VERIFY_TLS="$BRIDGE_K8S_MODE_OFF_CLUSTER_SKIP_VERIFY_TLS" \
            -e BRIDGE_K8S_MODE_OFF_CLUSTER_TOKEN="$BRIDGE_K8S_MODE_OFF_CLUSTER_TOKEN" \
            -e BRIDGE_PLUGINS="$BRIDGE_PLUGINS" \
            $CONSOLE_IMAGE
    else
        BRIDGE_PLUGINS="project-pause-plugin=http://host.containers.internal:9001"
        podman run --pull always --platform $CONSOLE_IMAGE_PLATFORM --rm -p "$CONSOLE_PORT":9000 \
            -e BRIDGE_USER_AUTH="$BRIDGE_USER_AUTH" \
            -e BRIDGE_K8S_MODE="$BRIDGE_K8S_MODE" \
            -e BRIDGE_K8S_AUTH="$BRIDGE_K8S_AUTH" \
            -e BRIDGE_K8S_MODE_OFF_CLUSTER_ENDPOINT="$BRIDGE_K8S_MODE_OFF_CLUSTER_ENDPOINT" \
            -e BRIDGE_K8S_MODE_OFF_CLUSTER_SKIP_VERIFY_TLS="$BRIDGE_K8S_MODE_OFF_CLUSTER_SKIP_VERIFY_TLS" \
            -e BRIDGE_K8S_MODE_OFF_CLUSTER_TOKEN="$BRIDGE_K8S_MODE_OFF_CLUSTER_TOKEN" \
            -e BRIDGE_PLUGINS="$BRIDGE_PLUGINS" \
            $CONSOLE_IMAGE
    fi
else
    BRIDGE_PLUGINS="project-pause-plugin=http://host.docker.internal:9001"
    docker run --pull always --platform $CONSOLE_IMAGE_PLATFORM --rm -p "$CONSOLE_PORT":9000 \
        -e BRIDGE_USER_AUTH="$BRIDGE_USER_AUTH" \
        -e BRIDGE_K8S_MODE="$BRIDGE_K8S_MODE" \
        -e BRIDGE_K8S_AUTH="$BRIDGE_K8S_AUTH" \
        -e BRIDGE_K8S_MODE_OFF_CLUSTER_ENDPOINT="$BRIDGE_K8S_MODE_OFF_CLUSTER_ENDPOINT" \
        -e BRIDGE_K8S_MODE_OFF_CLUSTER_SKIP_VERIFY_TLS="$BRIDGE_K8S_MODE_OFF_CLUSTER_SKIP_VERIFY_TLS" \
        -e BRIDGE_K8S_MODE_OFF_CLUSTER_TOKEN="$BRIDGE_K8S_MODE_OFF_CLUSTER_TOKEN" \
        -e BRIDGE_PLUGINS="$BRIDGE_PLUGINS" \
        $CONSOLE_IMAGE
fi
