# Bootstrap configuration.                                           -*- sh -*-

# Copyright (C) 2021-2025 Simon Josefsson

# 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 3 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, see <https://www.gnu.org/licenses/>.

GNULIB_REVISION=c89cd2fbd3b9f3d7c5a146247256599714c91ec7

source_base=gl
tests_base=gl/tests
m4_base=gl/m4
gnulib_name=libgnu
gnulib_tool_option_extras="--no-vc-files"
checkout_only_file=.gitignore

gnulib_modules="
  autobuild
  csharpcomp-script
  csharpexec-script
  error
  getline
  getopt-gnu
  git-version-gen
  gitlog-to-changelog
  lib-msvc-compat
  lib-symbol-versions
  lib-symbol-visibility
  manywarnings
  pmccabe2html
  progname
  readme-release
  update-copyright
  useless-if-before-free
  valgrind-tests
  version-etc
  version-stamp
"

lib_modules="
  gettext-h
  langinfo
  stdint
  striconv
  strverscmp
  unistr/u8-check
"

# Build prerequisites
buildreq="\
autoconf   2.64
automake   1.13
git        1.4.4
perl       5.5
make       -
gperf      -
gengetopt  -
"

if ! command -v gtkdocize >/dev/null; then
    # Pacify autoreconf's invocation of gtkdocize.
    GTKDOCIZE="true gtkdocize"
    export GTKDOCIZE
fi

# Prime po/*.po from fall-back copy stored in git.
for poin in po/*.po.in; do
    po=$(echo $poin | sed 's/.in//')
    test -f $po || cp -v $poin $po
done
ls po/*.po | sed 's|.*/||; s|\.po$||' > po/LINGUAS

bootstrap_post_import_hook ()
{
  # No test modules are used but 'valgrind-tests' make gnulib-tool
  # believe we need this.
  rm -rf gl/tests

  ${GNULIB_SRCDIR}/gnulib-tool --without-tests --libtool --macro-prefix=lgl --lgpl=2 --dir=. --lib=libgnu --source-base=lib/gl --m4-base=lib/gl/m4 --no-vc-files --import ${lib_modules}

  # Autopoint and libtoolize copy M4 macros into AC_CONFIG_MACRO_DIR
  # which is lib/m4 in this project.  The ./bootstrap script copies
  # gnulib's M4 macros into gl/m4 and lib/gl/m4 respectively.
  #
  # The M4 files from gnulib must be preferred over the files from
  # autopoint and libtoolize.  Otherwise you get build errors, since
  # gnulib (other M4 macros and the *.in.h header file stubs) assume
  # they are using the gnulib M4 macros.  It may be possible to solve
  # this with clever ordering of aclocal -I's but then you would still
  # have multiple versions of the same M4 macro in the tarball.
  #
  # In most projects, the M4 files from one single gnulib-tool
  # instance would overwrite all autopoint/libtoolize M4 files, and
  # then you would use that m4-base as the AC_CONFIG_MACRO_DIR value
  # and everything would work.
  #
  # In this project it happens that some M4 files are put in gl/m4/
  # and some in lib/gl/m4/ and that neither directory contains a
  # strict superset of the M4 files that were copied into lib/m4/.
  #
  # The following is a generic solution to make sure any
  # autopoint/libtoolize M4 macros are not used, but instead the
  # version from gnulib ends up being used.
  #
  # This seem like a hack, so other solutions are welcome.
  echo Removing older autopoint/libtool M4 macros...
  for f in `cd lib/m4 && ls *.m4`; do
      test -f gl/m4/$f && rm -fv lib/m4/$f
      test -f lib/gl/m4/$f && rm -fv lib/m4/$f
  done

  if ! gtkdocize; then
      echo "warning: gtkdocize missing -- gtk-doc manual will be missing"
      # rm because gtk-doc.make might be a link to a protected file
      rm -f gtk-doc.make
      echo "EXTRA_DIST =" > gtk-doc.make
      echo "CLEANFILES =" >> gtk-doc.make
  fi

  # Automake requires that ChangeLog exist.
  touch ChangeLog || return 1
}

bootstrap_epilogue ()
{
    # Run automake again.  It seems the first time ./bootstrap runs
    # automake, it will not add some files to am__DIST_COMMON in
    # top-level Makefile.in, but on subsequent runs (for example
    # through autoreconf, which is triggered on 'make release') it
    # will add the files.  The missing files include
    # build-aux/depcomp, build-aux/mdate-sh, build-aux/texinfo.texi,
    # and build-aux/ylwrap.  We want the am__DIST_COMMON value to be
    # identical so that "make dist" and "make release" tarballs are
    # identical and reproducible.
    ${AUTOMAKE=automake} --no-force
}
