# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=ucl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.03
pkgrel=3
pkgdesc="Portable lossless data compression library written in ANSI C (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.oberhumer.com/opensource/ucl/"
msys2_repository_url="https://github.com/korczis/ucl"
license=('spdx:GPL-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://www.oberhumer.com/opensource/${_realname}/download/${_realname}-${pkgver}.tar.gz"
        "ucl-1.03-autoconf-compat.patch"
        "ucl-1.03-missing-macros.patch"
        "ucl-1.03-no-undefined.patch"
        "ucl-1.03-aarch64.patch")
sha256sums=('b865299ffd45d73412293369c9754b07637680e5c826915f097577cd27350348'
            '0cbd00a8383c098f98d5b022fcb4fa8d1448501e00221a223be04be0383bafc0'
            'ed5425536d98e2097488001e6e73d2ed93da65050a6fded62435bec077bc594b'
            '56a110aedab800c0a06cf4fd4dcef8f78691a0d091e2f143a2a8f02cfbb4c172'
            'f94c63c8cfc30d7c8555462564cd1cc82205e1925dd87d83bdf4e8ed4d6b3d05')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 --binary -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  
  rm -f acinclude.m4
  rm -f m4/local.m4
  
  apply_patch_with_msg \
    ucl-1.03-autoconf-compat.patch \
    ucl-1.03-missing-macros.patch \
    ucl-1.03-no-undefined.patch \
    ucl-1.03-aarch64.patch

  touch ChangeLog
  WANT_AUTOMAKE=latest autoreconf -fiv
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  CFLAGS+=" -std=gnu90"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --enable-static \
    --enable-shared

  make
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  make install DESTDIR="${pkgdir}"
}
