# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=libtomcrypt
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.18.2
pkgrel=1
pkgdesc="A fairly comprehensive, modular and portable cryptographic toolkit (mingw-w64)."
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.libtom.net/LibTomCrypt/"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-libtommath")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-libtool")
options=('staticlibs' 'strip')
source=("https://github.com/libtom/libtomcrypt/releases/download/v${pkgver}/crypt-${pkgver}.tar.xz"{,.asc}
        "0001-makefile-enable-no-undefined.patch")
sha256sums=('96ad4c3b8336050993c5bc2cf6c057484f2b0f9f763448151567fbab5e767b84'
            'SKIP'
            'c53ab3ef2fa301f09fcfdc8754745b2dff62ed09ea5671e19cc42d778a5e537e')
validpgpkeys=('C4386A237ED43A475541B9427B2CD0DD4BCFF59B') # Steffen Jaeckel <s_jaeckel@gmx.de>

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

  # Disable unnecessary warnings
  sed -e "s/ -Wsystem-headers//g" -e "s/ -Wshadow//g" -i makefile_include.mk

  # https://github.com/libtom/libtomcrypt/pull/564
  patch -Np1 -i "${srcdir}/0001-makefile-enable-no-undefined.patch"
}

build() {
  cp -rf "${_realname}-${pkgver}" "build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  CPPFLAGS+=" -DLTM_DESC -DGMP_DESC -DUSE_LTM" \
  EXTRALIBS="-ltommath -lgmp" \
  PREFIX="${MINGW_PREFIX}" \
  make -f makefile.shared IGNORE_SPEED=1 all
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  #./test
}

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

  DESTDIR="${pkgdir}" \
  INSTALL_GROUP="root" \
  INSTALL_OPTS="" \
  PREFIX="${MINGW_PREFIX}" \
  make -f makefile.shared install

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