#!/bin/sh
#
# Regina - A Normal Surface Theory Calculator
# Autoconf Preparation Utility
#
# Copyright (c) 2002-2007, Ben Burton
# For further details contact Ben Burton (bab@debian.org).
#
# Usage: ac_gen
#
# Regenerates ../configure.ac and ../acinclude.m4 from files within this
# (admin) subdirectory.
#
# This script should NOT be run directly; to use it run "make -f Makefile.svn"
# from the top-level source directory (the directory above this).
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.
#

set -e

# Are we in the top-level source directory?

if ! test -d admin; then
  echo "This script should not be run directly." 1>&2
  echo "To use it, run \"make -f Makefile.svn\" from the top-level" 1>&2
  echo "source directory." 1>&2
  exit 1
fi

# ------------------------
# Regenerate configure.ac:
# ------------------------

real=configure.ac
output=configure.ac.tmp

echo "Regenerating $real:"

# Actually generate the new file.

cat > "$output" <<EOF
dnl
dnl    DO NOT EDIT THIS FILE DIRECTLY if this source tree has been checked
dnl    out from subversion or CVS.  It has been automatically generated by
dnl    Makefile.svn and any changes will be overwritten.  Try editing
dnl    configure.ac.top or configure.ac.bot from subdirectory ./admin instead.
EOF

cat admin/configure.ac.top >> "$output"

echo "# List of Makefiles (automatically generated by Makefile.svn):" \
  >> "$output"
for i in `find . -name Makefile.am | sort | sed -e 's#^./##;s#\.am$##'`; do
  case "$i" in
    ./Makefile )
      echo "AC_CONFIG_FILES([Makefile])" >> "$output"
    ;;
    javaui/* )
    ;;
    test/* )
    ;;
    utils/local/* )
    ;;
    utils/snappea/* )
    ;;
    * )
      echo "AC_CONFIG_FILES([$i])" >> "$output"
    ;;
  esac
done

cat admin/configure.ac.bot >> "$output"

# Did configure.ac change?

if test -e "$real"; then
  if diff "$output" "$real" > /dev/null; then
    echo "File $real is unchanged."
    rm -f "$output"
  else
    echo "File $real has changed."
    rm -f "$real"
    mv "$output" "$real"
  fi
else
  echo "File $real is new."
  mv "$output" "$real"
fi

# ------------------------
# Regenerate acinclude.m4:
# ------------------------

real=acinclude.m4
output=acinclude.m4.tmp

echo "Regenerating $real:"

# Actually generate the new file.

cat > "$output" <<EOF
dnl
dnl    DO NOT EDIT THIS FILE DIRECTLY if this source tree has been checked
dnl    out from subversion or CVS.  It has been automatically generated by
dnl    Makefile.svn and any changes will be overwritten.  Try editing
dnl    acinclude.m4.top from subdirectory ./admin instead.
EOF

cat admin/acinclude.m4.top >> "$output"

for i in admin/*.m4.ext; do
  echo "Adding $i..."
  echo >> "$output"
  echo "dnl" >> "$output"
  echo "dnl    Imported by $0: $i" >> "$output"
  echo "dnl" >> "$output"
  echo >> "$output"
  cat "$i" >> "$output"
done

# Did acinclude.m4 change?

if test -e "$real"; then
  if diff "$output" "$real" > /dev/null; then
    echo "File $real is unchanged."
    rm -f "$output"
  else
    echo "File $real has changed."
    rm -f "$real"
    mv "$output" "$real"
  fi
else
  echo "File $real is new."
  mv "$output" "$real"
fi

