# Contributor: Teemu Nätkinniemi ("teepean") <tnatkinn@gmail.com>

_realname=htslib
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.23
pkgrel=1
pkgdesc="library for high-throughput sequencing data formats (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.htslib.org/'
msys2_repository_url='https://github.com/samtools/htslib'
msys2_references=(
  "cpe: cpe:/a:htslib:htslib"
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-libsystre"
         "${MINGW_PACKAGE_PREFIX}-libdeflate"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools")
source=("https://github.com/samtools/htslib/releases/download/$pkgver/$_realname-$pkgver.tar.bz2"
        0001-makefile-remove-msvc-lib.patch)
sha256sums=('63927199ef9cea03096345b95d96cb600ae10385248b2ef670b0496c2ab7e4cd'
            '2e3c8eeff2b4b16a6d420243d69fbccde44d250ad6ef9ac647c9b113097b9dd6')

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

  # This removes commands which create MSVC like import libraries, See
  # https://github.com/samtools/htslib/commit/3f7e13e0688e50fca61e6f043e6655a165d596a2

  patch -p1 -i "${srcdir}/0001-makefile-remove-msvc-lib.patch"

  autoreconf -fvi
}

build() {
  # make check does not work with out-of-tree build
  cp -r "${_realname}-${pkgver}" "build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-libcurl \
    --disable-plugins

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  make check || warning "Tests failed"
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  make DESTDIR=$pkgdir install

  # Rename import library with lib prefix
  mv -v "${pkgdir}${MINGW_PREFIX}/lib/hts.dll.a" "${pkgdir}${MINGW_PREFIX}/lib/libhts.dll.a"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE $pkgdir${MINGW_PREFIX}/share/licenses/$_realname/LICENSE
}

