#!/bin/bash

set -euo pipefail

export MCO_NAMESPACE="openshift-machine-config-operator"
export MCD_DAEMONSET="daemonset/machine-config-daemon"
export MCD_CONTAINER_NAME="machine-config-daemon"
export ROOTFS_MCD_PATH="/rootfs/usr/local/bin/machine-config-daemon"

exists() {
  builtin type -P "$1" &> /dev/null
}

can_run() {
  # Check if we have an oc binary in our path
  if ! exists 'oc'; then
    echo "oc not in path"
    exit 1
  fi

  # Check if KUBECONFIG is set
  if [ -z "$KUBECONFIG" ]; then
    echo "KUBECONFIG is not set"
    exit 1
  fi

  # Check if KUBECONFIG actually points to something
  if [ ! -f "$KUBECONFIG" ]; then
    echo "$KUBECONFIG does not exist"
    exit 1
  fi

  if [ ! "$(oc get nodes)" ]; then
    echo "Stale kubeconfig, cannot authenticate"
    exit 1
  fi
}
