#!/usr/bin/env bash

shopt -s extglob # Extended globs must always be on for BDSM.

saved_bdsm_path="${bdsm_path:-/usr/local/bdsm}"

# Load System BDSM Configuration, if it exists.
for file in /etc/bdsmrc "$HOME/.bdsmrc"
do
  if [[ -s "${file}" ]]
  then
    . "${file}"
  fi
done

bdsm_path="${saved_bdsm_path}"

if (( UID == 0 ))
then # FHS
  true \
    "${var_path:="/var"}" \
    "${etc_path:="/etc"}" \
    "${profile_path:="/etc/profile"}" \
    "${profile_d_path:="/etc/profile.d"}"
else
  true \
    "${var_path:="${bdsm_path}/var"}" \
    "${etc_path:="$HOME/.etc"}" \
    "${profile_path:="$HOME/.profile"}" \
    "${profile_d_path:="$HOME/.profile.d"}"
fi

true \
  "${modules_path:="${bdsm_path}/extensions/builtin/core/modules"}" \
  "${templates_path:="${bdsm_path}/templates"}" \
  "${src_path:="${bdsm_path}/src"}" \
  "${md5_path:="${bdsm_path}/md5"}" \
  "${archives_path:="${bdsm_path}/archives"}" \
  "${scm_path:="${bdsm_path}/scm"}" \
  "${tmp_path:="${bdsm_path}/tmp"}" \
  "${bin_path:="${bdsm_path}/bin"}" \
  "${repos_path:="${bdsm_path}/repos"}" \
  "${archives_path:="${bdsm_path}/archives"}" \
  "${extensions_src_path:="${src_path}/extensions"}" \
  "${remote:="origin"}" \
  "${branch:="master"}" \
  "${language:="shell"}" \
  "${user:=$USER}" \
  "${keep_releases:=4}" \
  "${hooks_flag:=1}" \
  "${PAGER:="less"}" \
  "${config_path:="${bdsm_path}/extensions/builtin/core/config"}" \
  "${packages_path:="${bdsm_path}/pkg"}" \
  "${active_path:="${packages_path}/active"}" \
  "${extension_args:=""}"

# Make sure preload_modules is an array
export preload_modules=( ${preload_modules[@]} )

if [[ -d "${active_path}/bin" ]]
then
  export PATH="${active_path}/bin:${active_path}/sbin:${PATH}"
fi

export bdsm_path modules_path initial_pwd extension_action_calls=0

# Load module() function and then load core modules.
source "${modules_path}/shell/core/dsl"
source "${modules_path}/shell/string/dsl"
source "${modules_path}/shell/logging/dsl"
source "${modules_path}/shell/logging/initialize"
source "${modules_path}/shell/modules/dsl"
source "${modules_path}/shell/modules/initialize"

if [[ "$MACHTYPE" = *solaris* ]]
then # custom compilations + g*
  export PATH="/usr/local/bin:/opt/local/bin:/usr/sfw/bin:/usr/sbin:/usr/bin"
fi

# Ensure variables desired to be exported to extensions actually are.
bdsm_exports

if [[ -n $bdsm_script ]]
then
  # load script
  . $bdsm_script
elif [[ -n "${call_action}" ]]
then
  if ! declare -F "${call_action}" >/dev/null
  then
    error "${call_action} is not a valid function name"
  else
    action "${call_action}" "${extension_args[@]}"
  fi
elif set | grep '^extension_args=(' >/dev/null
then
  case "${extension_args[0]}" in
    */*|*,*)
      modules bdsm/array bdsm/system
      array_shift extension_args extensions
      sequential=(${extensions//,/ })
      for sequence in "${sequential[@]}"
      do
        parallel=("${sequence//\// }")
        PATH="${bdsm_path}/bin:${PATH}"
        bdsm-ll --noswap -j $(os_cpu_count) bdsm {} ${extension_args[*]} ::: ${parallel[@]}
      done
      ;;
    *)
      modules bdsm/extensions
      extension_action
      detect_action_type
      extension_run
  esac
fi
