#! /bin/sh

if [ -z "$(which automake | grep .nix-profile)" ] ; then

        # Automake not provided by Nix; try to find suitable native one.

	if [ -f /etc/gentoo-release ] ; then 
		# Environment variables for our Gentoo friends.
		export WANT_AUTOMAKE="1.8"
		export WANT_AUTOCONF="2.59"
        fi
fi

#	elif [ -f /etc/debian_version ] ; then
#		# Environment variables for our Debian friends.
#	        export ACLOCAL=/usr/bin/aclocal-1.9
#	        export AUTOMAKE=/usr/bin/automake-1.9
#	fi

# Determine autoxt to use.
if test -z "$STRATEGOXT_BASELINE"; then
  echo "***********************************************************"
  echo "                          WARNING"
  echo ""
  echo "The environment variable STRATEGOXT_BASELINE is not set."
  echo "It is recommended to set this variable to make sure that"
  echo "the right autoxt is used for bootstrapping Stratego/XT. I"
  echo "assume that the baseline you want to use is on the path."
  echo "***********************************************************"
  AUTOXT="autoxt"
else
  test -x "$STRATEGOXT_BASELINE/bin/autoxt"
  if test $? -eq 0; then
    AUTOXT="$STRATEGOXT_BASELINE/bin/autoxt"
  else
    echo "error: $STRATEGOXT_BASELINE/bin/autoxt does not exist."
    echo "Please check your STRATEGOXT_BASELINE environment variable"
    exit 1
  fi
fi

echo "bootstrap: using autoxt at $AUTOXT"

# $AUTOXT || exit 1
# mv ./autoxt.m4 ./config/  || exit 1

echo "WARNING: hack: using our own autoxt.m4"
cp ./autoxt/autoxt.m4 ./config/ || exit 1
echo "WARNING: hack: using our own Makefile.xt"
cp ./autoxt/Makefile.xt . || exit 1

autoreconf -ifv  || exit 1

# Bootstrap the subpackages
PKGS="autoxt xtc strc-core gpp c-tools concrete-syntax \
  aterm-front stratego-front asfix-tools sdf-front sdf-tools \
  stratego-regular xml-front stratego-libraries"

cp ./config/autoxt.m4 stratego-libraries/config
for dir in $PKGS
do 
  echo
  echo "bootstrap: bootstrapping in $dir"
  echo
  cd $dir
  if test "$dir" != "autoxt"
  then
    rm -f Makefile.xt
    cp ../Makefile.xt . 
  fi
  if test -e "../svn-revision"
  then
    rm -f 
    cp ../svn-revision .
  fi
  autoreconf -ifv || exit 1
  cd ..
done
