# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=libtool
pkgname=('libtool' 'libltdl')
pkgver=2.5.4
pkgrel=4
pkgdesc="A generic library support script"
arch=('i686' 'x86_64')
url="https://www.gnu.org/software/libtool"
msys2_references=(
  "cpe: cpe:/a:gnu:libtool"
)
license=('spdx:LGPL-2.0-or-later WITH Libtool-exception')
makedepends=(
  "autoconf-wrapper"
  "automake-wrapper"
  "gcc"
  "help2man"
  "m4"
)
source=(https://ftp.gnu.org/gnu/libtool/${pkgbase}-${pkgver}.tar.xz{,.sig}
        0001-cygwin-mingw-Create-UAC-manifest-files.patch
        0002-Fix-seems-to-be-moved.patch
        0003-Fix-STRICT_ANSI-vs-POSIX.patch
        0004-Allow-statically-linking-Flang-support-libraries-whe.patch
        0005-libtool-include-process.h.patch
        0006-Pass-various-flags-to-GCC.patch
        0007-msysize.patch
        0011-Pick-up-clang_rt-static-archives-compiler-internal-l.patch
        0013-Allow-statically-linking-compiler-support-libraries-.patch
        0014-Support-llvm-objdump-f-output.patch)
sha256sums=('f81f5860666b0bc7d84baddefa60d1cb9fa6fceb2398cc3baca6afaa60266675'
            'SKIP'
            '67e4f2429dfe67499b1829d0a8312d9a043057db354307f8db41d9f967944346'
            'd873306936060e6c72cf0f3c174131cc85debc2ea6dc924eabdf6e3a813c768e'
            '00f0b0429431a884b481eb92f4b138c50bc17eb09f0985d7ebfd4a0a50407eb6'
            '96538c6cb843f8673f4db8199402b4b02d9477d71c86df5f5e94e3e4d1347b54'
            '0dd70a4f955ed4cd94a0d13b784b7db7dddbd4d1d6ee5b092202f8b9496fd3eb'
            '2cb1b61247a4632698ac7e26c496c3ad8c1b4fde080d301994dbddc0794a4bf4'
            'fca5986c0010b202ee65c69939815269bd46b3ce948d9222916985652da7e2af'
            'de68dc9387f47ee129f55b2f525a7d222374ddd8915f4f42af05f185b779b826'
            '8069e887aeeab7491f15e00547fa66d9b9e86407f5a23f37a6d8c7d165de752e'
            'e62e836f018339974f5ef7cf81bc0a4252901158eb0824eb62d0270206093ad7')
validpgpkeys=('7C5FBB96BE82B954AC20DF5F6EAC957F8EEB55C0'  # Alex Ameen <alex.ameen.tx@gmail.com>
              'FA26CA784BE188927F22B99F6570EA01146F7354') # Ileana Dumitrescu <ileanadumi95@protonmail.com>

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

prepare() {
  cd "${pkgbase}-${pkgver}"

  # Note: keep the patches in sync with mingw libtool
  apply_patch_with_msg \
    0001-cygwin-mingw-Create-UAC-manifest-files.patch \
    0002-Fix-seems-to-be-moved.patch \
    0003-Fix-STRICT_ANSI-vs-POSIX.patch \
    0004-Allow-statically-linking-Flang-support-libraries-whe.patch \
    0005-libtool-include-process.h.patch \
    0006-Pass-various-flags-to-GCC.patch \
    0007-msysize.patch \
    0011-Pick-up-clang_rt-static-archives-compiler-internal-l.patch \
    0013-Allow-statically-linking-compiler-support-libraries-.patch \
    0014-Support-llvm-objdump-f-output.patch

  autoconf -f
}

build() {
  cd "${pkgbase}-${pkgver}"

  ./configure \
    --build=${CHOST} \
    --prefix=/usr \
    lt_cv_sys_lib_dlsearch_path_spec="/usr/lib /lib"

  make
}

check() {
  cd "${pkgbase}-${pkgver}"

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

package_libtool() {
  depends=("bash" "libltdl=${pkgver}")

  cd "${pkgbase}-${pkgver}"

  make DESTDIR=${pkgdir} install-binSCRIPTS install-man install-info \
                         install-data-local

  install -Dm644 "${srcdir}/${pkgbase}-${pkgver}/COPYING" -t "${pkgdir}/usr/share/licenses/${pkgname}"
}

package_libltdl() {
  pkgdesc="A system independent dlopen wrapper for GNU libtool"

  cd "${pkgbase}-${pkgver}"

  make DESTDIR=${pkgdir} install-libLTLIBRARIES install-includeHEADERS \
                         install-ltdlincludeHEADERS

  install -Dm644 "${srcdir}/${pkgbase}-${pkgver}/libltdl/COPYING.LIB" -t "${pkgdir}/usr/share/licenses/${pkgname}"
}
