#! /bin/sh

#---------------------------------------------------------------------
#                             load-config                             
# 
# This is the opportunity for the template to modify the package
# configuration stored in fw-pkgin/config, e.g., add the template to the
# build dependencies.
# 
# The convention is that every program that wants to load fw-pkgin/config
# will go through template/$template/load-config
#---------------------------------------------------------------------

#---------------------------------------------------------------------
#                           chicken and egg                           
#---------------------------------------------------------------------

test -f "${FW_ROOT}/share/fw/sh/fw-find.sh" && fws="${FW_ROOT}/share/fw/sh/fw-find.sh"
test -f "fw/sh/fw-find.sh" && fws="fw/sh/fw-find.sh"
test -f "fw.local/sh/fw-find.sh" && fws="fw.local/sh/fw-find.sh"
test ! -z "$fws" || {
  echo "erlang/load-config: fatal: can't locate fw-find.sh" 1>&2
  exit 1
}

. "$fws"

#---------------------------------------------------------------------
#                              routines                               
#---------------------------------------------------------------------

fw_source "erlang/load-config" "sh/fw-exec.sh"
fw_source "erlang/load-config" "sh/parse-args.sh"
fw_source "erlang/load-config" "sh/validate-args.sh"

#---------------------------------------------------------------------
#                                main                                 
#---------------------------------------------------------------------

parse_args "erlang/load-config" "$@"

validate_args "erlang/load-config" "" ""

# default behavior: add the package providing the template to the build
# dependencies

fw_exec "build/automake/load-config" |                                       \
perl -e 'BEGIN { $suggests = 0; }'                                           \
     -e 'END { print "FW_PACKAGE_SUGGESTS=erlrc\n" unless $suggests; }'  \
     -pe '
  m%FW_PACKAGE_SUGGESTS="(.*)"% and do { $suggests = 1 };
  s%FW_PACKAGE_SUGGESTS="(.*)"%FW_PACKAGE_SUGGESTS="erlrc, $1"%;
  s%FW_PACKAGE_BUILD_DEPENDS="(.*)"%FW_PACKAGE_BUILD_DEPENDS="fw-template-erlang, $1"%; 
  s%, "%"%; 
  s%#(.*)%%;'
