#!/usr/bin/env bash
# Copyright IBM Corp. 2016, 2025
# SPDX-License-Identifier: BUSL-1.1

set -e

fail() {
  echo "$1" 1>&2
  exit 1
}

[[ -z "$VAULT_ADDR" ]] && fail "VAULT_ADDR env variable has not been set"
[[ -z "$VAULT_TOKEN" ]] && fail "VAULT_TOKEN env variable has not been set"
[[ -z "$VAULT_INSTALL_DIR" ]] && fail "VAULT_INSTALL_DIR env variable has not been set"

binpath=${VAULT_INSTALL_DIR}/vault
test -x "$binpath" || fail "unable to locate vault binary at $binpath"

customer_id=$(sudo "$binpath" license inspect /etc/vault.d/vault.lic | grep "Customer ID" | awk -F: '{print $2}' | xargs)
[[ -z "$customer_id" ]] && fail "Customer ID is empty in license inspect output"

# Send customer ID to stdout so it can be extracted as output of this step
echo "$customer_id"
