# Maintainer: Yang Kun <ikspress@outlook.com>

_basever=1.18
pkgname=automake${_basever}
pkgver=${_basever}
pkgrel=1
pkgdesc="A GNU tool for automatically creating Makefiles"
arch=('any')
license=('spdx:GPL-2.0-or-later')
url="https://www.gnu.org/software/automake"
msys2_references=(
  "cpe: cpe:/a:gnu:automake"
)
depends=('bash' 'perl')
makedepends=('autoconf' 'make' 'texinfo')
checkdepends=('bison' 'cscope' 'dejagnu' 'emacs' 'expect' 'flex' 'gcc' 'gettext'
              'help2man' 'libtool' 'lzip' 'sharutils' 'texinfo-tex' 'zip')
source=(https://ftp.gnu.org/gnu/automake/automake-${pkgver}.tar.xz{,.sig}
        automake-1.18-documentation.patch
        automake-1.17-msys2.patch)
sha256sums=('5bdccca96b007a7e344c24204b9b9ac12ecd17f5971931a9063bdee4887f4aaf'
            'SKIP'
            '3b066deda7ecb755a4cab8263f7fe7d5f44cf7790b45b5dcb48e5232f800856c'
            'a6b3a7099fb23ddc3d1402b16df32cf8f21bb63ca6b9314ff7caff5869b6ed50')
validpgpkeys=('17D3311B14BC0F248267BF020716748A30D155AD') # Karl Berry
replace=('automake')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _fname in "$@"; do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}/${_fname}"
  done
}

del_file_exists() {
  for _fname in "$@"; do
    if [ -f $_fname ] || [ -d $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}/automake-${pkgver}"

  apply_patch_with_msg \
    automake-1.18-documentation.patch \
    automake-1.17-msys2.patch
}

build() {
  cd "${srcdir}/automake-${pkgver}"
  ./configure --prefix=/usr
  make
  # build versioned info files
  cd doc
  makeinfo automake.texi automake-history.texi
}

check() {
  cd "${srcdir}/automake-${pkgver}"

  make -k check || warning "Tests failed"
}

package() {
  cd ${srcdir}/automake-${pkgver}
  make DESTDIR=${pkgdir} install

  mv "${pkgdir}/usr/share/aclocal/README" "${pkgdir}/usr/share/aclocal/README.am${_basever#*.}"

  # remove the unversioned info files
  rm -f "${pkgdir}"/usr/share/info/automake{,-history}.info*

  # remove the unversioned executables
  rm -f "${pkgdir}/usr/bin/automake" "${pkgdir}/usr/bin/aclocal"

  # remove the unversioned man pages
  rm -f "${pkgdir}/usr/share/man/man1/automake.1" \
    "${pkgdir}/usr/share/man/man1/aclocal.1"

  # remove amhello
  rm -rf "${pkgdir}/usr/share/doc"

  # install the versioned info files
  install -Dm644 "${srcdir}"/automake-${pkgver}/doc/*${_basever}.info* -t "${pkgdir}/usr/share/info/"
}
