# Maintainer: Nazar Mishturak <nazar m x at gmail dot com>

_realname=libbotan
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.19.3
pkgrel=2
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
msys2_references=(
  'archlinux: botan'
)
url='https://botan.randombit.net/'
pkgdesc='Botan is a C++ cryptography library released under the permissive Simplified BSD license (mingw-w64)'
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-xz"
         $([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-openmp"))
makedepends=("${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python-imagesize"
             "${MINGW_PACKAGE_PREFIX}-cc")
             #"${MINGW_PACKAGE_PREFIX}-doxygen"
             #"${MINGW_PACKAGE_PREFIX}-graphviz")
license=('BSD')
options=('strip' 'staticlibs' 'docs')
source=("https://botan.randombit.net/releases/Botan-${pkgver}.tar.xz"{,.asc}
        '001-boost-link.patch'
        '002-winsock-link.patch'
        '003-enable-shared.patch')
noextract=("Botan-${pkgver}.tar.xz")
sha256sums=('dae047f399c5a47f087db5d3d9d9e8f11ae4985d14c928d71da1aff801802d55'
            'SKIP'
            'c4dfd71c7e6a9592e2a95634e3fabfc071c1110a96ba218c4a3565fa14cb34da'
            '12f676d9c8f66decd7dc52a6a70f0c044737636837c79ae02f96d8d5a2168c46'
            'eb412004200932ab3b7e8649b81225f2afc44cb06d154adc166b167c73997787')
validpgpkeys=('621DAF6411E1851C4CF9A2E16211EBF1EFBADFBC') # Botan Distribution Key

prepare() {
  cd "${srcdir}"
  # Tarball contains Botan-${pkgver}.tgz/.travis.yml symlink
  bsdtar -xJf "Botan-${pkgver}.tar.xz" || true
  cd "Botan-${pkgver}"
  patch -p1 -i "${srcdir}/001-boost-link.patch"
  patch -p1 -i "${srcdir}/002-winsock-link.patch"
  patch -p1 -i "${srcdir}/003-enable-shared.patch"
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  cp -rf "${srcdir}"/Botan-${pkgver} "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  local BUILD_TYPE_FLAGS=''
  if check_option 'debug' 'y'; then
    BUILD_TYPE_FLAGS='--with-debug-info'
  fi

  # See https://github.com/randombit/botan/issues/2582 for an explanation
  # on why --disable-modules=thread_utils is here.
  MSYS2_ARG_CONV_EXCL="--prefix=;--libdir=;--bindir=" \
  ${MINGW_PREFIX}/bin/python configure.py \
    --disable-modules=thread_utils \
    --os=mingw \
    --cc=${CC} \
    --prefix=${MINGW_PREFIX} \
    --cpu=${CARCH} \
    ${BUILD_TYPE_FLAGS} \
    --link-method=copy \
    --without-doxygen \
    --with-sphinx \
    --without-rst2man \
    --with-boost \
    --with-openmp \
    --with-bzip2 \
    --with-lzma \
    --with-sqlite3 \
    --with-zlib

  make
}

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

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--libdir=;--bindir=" \
  DESTDIR="${pkgdir}" \
  make install

  MSYS2_ARG_CONV_EXCL="-p" python -m compileall \
    -o 0 -o 1 -q -s"${pkgdir}" -p"/" "${pkgdir}${MINGW_PREFIX}/lib/python"*
}
