# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=libtool
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-libtool" "${MINGW_PACKAGE_PREFIX}-libltdl")
pkgver=2.5.4
pkgrel=3
pkgdesc="A generic library support script (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
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=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "autoconf-wrapper"
  "automake-wrapper"
  "help2man"
  "m4"
)
source=(https://ftp.gnu.org/gnu/libtool/${_realname}-${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 "${_realname}-${pkgver}"

  # Note: keep the patches in sync with msys 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
}

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

  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST}

  make
}

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

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

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

  cd "${_realname}-${pkgver}"

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

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${pkgname#${MINGW_PACKAGE_PREFIX}-}"

  # Fix paths
  local WINPREFIX=$(cygpath -wm ${MINGW_PREFIX})
  sed -i "s|${WINPREFIX}|${MINGW_PREFIX}|g" "${pkgdir}${MINGW_PREFIX}/bin/libtool"
}

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

  cd "${_realname}-${pkgver}"

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

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/libltdl/COPYING.LIB" -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${pkgname#${MINGW_PACKAGE_PREFIX}-}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
