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

pkgbase=libtool
pkgname=('libtool' 'libltdl')
pkgver=2.6.2
pkgrel=1
pkgdesc="A generic library support script"
arch=('i686' 'x86_64')
url="https://www.gnu.org/software/libtool"
msys2_repository_url='https://cgit.git.savannah.gnu.org/cgit/libtool.git'
msys2_references=(
  "cpe: cpe:/a:gnu:libtool"
)
license=('spdx:LGPL-2.0-or-later WITH Libtool-exception')
makedepends=(
  "autoconf"
  "automake"
  "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
        0014-Support-llvm-objdump-f-output.patch)
sha256sums=('2ef1067c16c97db930fd740cc9bc3d3ba9a583804ae5ac42cc3e8719e49e191e'
            'SKIP'
            '67e4f2429dfe67499b1829d0a8312d9a043057db354307f8db41d9f967944346'
            'd873306936060e6c72cf0f3c174131cc85debc2ea6dc924eabdf6e3a813c768e'
            '00f0b0429431a884b481eb92f4b138c50bc17eb09f0985d7ebfd4a0a50407eb6'
            '96538c6cb843f8673f4db8199402b4b02d9477d71c86df5f5e94e3e4d1347b54'
            '0dd70a4f955ed4cd94a0d13b784b7db7dddbd4d1d6ee5b092202f8b9496fd3eb'
            '882d73c7d42f6f2e4062c851e72b6bb185da95f9b47942be1c4c9099a41ac93f'
            '228d6d847651ec3ae1c00d872dcf6382415985f6e94240daa445333a57707746'
            '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 \
    0014-Support-llvm-objdump-f-output.patch

  autoconf -f
}

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

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

  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}"
}
