#!/usr/bin/env bash
# Load our configuration files
# Copyright (c) 2023 Ismo Vuorinen. All Rights Reserved.

set -euo pipefail

# Set verbosity with VERBOSE=1 x-load-configs
VERBOSE="${VERBOSE:=0}"

[ "$VERBOSE" = "1" ] && {
  set -x
}

CONFIG_PATH="$DOTFILES/config"

# Load the shell dotfiles, and then some:
HOST="$(hostname -s)"
[ "$VERBOSE" = "1" ] && {
  echo "x-load-configs: VERBOSE=1"
  echo "x-load-configs: HOST: $HOST"
}

configFile()
{
  echo "$CONFIG_PATH/$1"
}

configMsg()
{
  printf 'x-load-configs: %s %s\n' "$1" "$2"
}

loadConfigFiles()
{
  CONFIG_FILE=$1
  SECRET_FILE=$CONFIG_FILE-secret
  HOST_FILE=$CONFIG_FILE-$HOST
  SECRET_HOST=$HOST_FILE-secret

  [ "$VERBOSE" = "1" ] && configMsg "?" "$CONFIG_FILE"

  # global (exports|alias|functions) FILENAME for all hosts
  # shellcheck source=../config/exports
  [ -r "$CONFIG_FILE" ] && {
    source "$CONFIG_FILE" && [ "$VERBOSE" = "1" ] && configMsg "Found" "$CONFIG_FILE"
  }

  # global secret FILENAME, git ignored
  # shellcheck source=../config/exports-secret
  [ "$VERBOSE" = "1" ] && configMsg "?" "$SECRET_FILE"
  [ -r "$SECRET_FILE" ] && {
    source "$SECRET_FILE" && [ "$VERBOSE" = "1" ] && configMsg "Found" "$SECRET_FILE"
  }
  # host specific (exports|alias|functions) FILENAME
  # shellcheck source=../config/exports
  [ "$VERBOSE" = "1" ] && configMsg "?" "$HOST_FILE"
  [ -r "$HOST_FILE" ] && {
    source "$HOST_FILE" && [ "$VERBOSE" = "1" ] && configMsg "Found" "$HOST_FILE"
  }
  # host specific (exports|alias|functions) FILENAME, git ignored
  # shellcheck source=../config/exports
  [ "$VERBOSE" = "1" ] && configMsg "?" "$SECRET_HOST"
  [ -r "$SECRET_HOST" ] && {
    source "$SECRET_HOST" \
      && [ "$VERBOSE" = "1" ] && configMsg "Found" "$SECRET_HOST"
  }
}

FILE_EXPORTS=$(configFile "exports")
FILE_FUNCTIONS=$(configFile "functions")
FILE_ALIAS=$(configFile "alias")

loadConfigFiles "$FILE_EXPORTS"
loadConfigFiles "$FILE_FUNCTIONS"
loadConfigFiles "$FILE_ALIAS"

exit 0
