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

_realname=gts
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.7.6
pkgrel=3
pkgdesc="Provides useful functions to deal with 3D surfaces meshed with interconnected triangles (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gts.sourceforge.io/"
license=('LGPL')
depends=("${MINGW_PACKAGE_PREFIX}-glib2")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools" "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://downloads.sourceforge.net/project/${_realname}/${_realname}/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "gts-mingw.patch")
sha256sums=('059c3e13e3e3b796d775ec9f96abdce8f2b3b5144df8514eda0cc12e13e8b81e'
            '7efb4a8e76dbb9b18bd35b7021ed269d8d5c871b3acd22367c4febf27d67fed7')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/gts-mingw.patch"
  WANT_AUTOMAKE=latest autoreconf -fiv
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  cp -rf "${srcdir}"/${_realname}-${pkgver}/. "${srcdir}"/build-${MSYSTEM}
  cd "${srcdir}"/build-${MSYSTEM}
  
  export LDFLAGS=${LDFLAGS//--as-needed,}
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-static \
    --enable-shared

  make
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  make install DESTDIR="${pkgdir}"
  
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}/bin/gts-config"
  
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
