#! /bin/sh

#---------------------------------------------------------------------
#                           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 "fw-template/bootstrap: fatal: can't locate fw-find.sh" 1>&2
  exit 1
}

. "$fws"

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

fw_source "fw-template/bootstrap" "sh/bootstrap-util.sh"
fw_source "fw-template/bootstrap" "sh/fatal.sh"
fw_source "fw-template/bootstrap" "sh/fw-exec.sh"
fw_source "fw-template/bootstrap" "sh/parse-args.sh"
fw_source "fw-template/bootstrap" "sh/validate-args.sh"

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

parse_args "fw-template/bootstrap" "$@"

validate_args "fw-template/bootstrap" "revision template_type" ""

topdir=`pwd`

(
  cd "fw.local/template" && {
    test -h NAME || ln -sf "$template_type" NAME
  }
) || fatal "fw-template/bootstrap"

for dir in tests fw.local fw.local/template fw.local/template/NAME .
  do
    for x in Makefile_dot_am configure.ac
      do
        target=`echo "$x" | perl -pe 's/_dot_am/.am/;'`

        bootstrap_link_optional "template/fw-template/$dir/$x"          \
                                "$dir"                                  \
                                "$target"                               \
        || fatal "fw-template/bootstrap"
      done
  done

fw_exec "build/automake/bootstrap" "$@"

