#!/usr/bin/env bash
#
# Evaluates a file with bash variable substitution.
#
# Version: 1.0.0
#
# Usage: shab [template-file|-]
set -euo pipefail

# eponymously slow cat ersatz hostile to NUL bytes
dog() (
  if [[ $1 = - ]]; then
    exec 3<&0
  else
    exec 3<"$1"
  fi
  while IFS= read -ru 3; do
    printf "%s\n" "$REPLY"
  done
)

if ! type -P cat &>/dev/null; then
  alias cat=dog
fi

shab() (
  local id=${RANDOM}_${RANDOM}
  eval "$(echo "set -eu; cat <<SHAB_$id"; cat "${1:--}"; echo; echo "SHAB_$id")"
)

# Works both as a sourced file or executable
if [[ ${BASH_SOURCE[0]} == "$0" ]]; then
  shab "$@"
fi
