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

_realname=libbotan
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.17.3
pkgrel=2
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
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")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python-imagesize")
             #"${MINGW_PACKAGE_PREFIX}-doxygen"
             #"${MINGW_PACKAGE_PREFIX}-graphviz")
license=('BSD')
options=('strip' 'staticlibs' 'docs')
source=(https://botan.randombit.net/releases/Botan-${pkgver}.tar.xz
        '001-boost-link.patch'
        '002-winsock-link.patch'
        '003-python3-install.patch')
noextract=("Botan-${pkgver}.tar.xz")
sha256sums=('79123b654445a4abba486e09a431788545c708237382a3e765664c9f55b03b88'
            'c4dfd71c7e6a9592e2a95634e3fabfc071c1110a96ba218c4a3565fa14cb34da'
            '12f676d9c8f66decd7dc52a6a70f0c044737636837c79ae02f96d8d5a2168c46'
            '4c3160fcb47791c34984d2ada5cdb6f0dfc029dfe39822ba969d4672dc2fa170')
validpgpkeys=('621DAF6411E1851C4CF9A2E16211EBF1EFBADFBC')

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-python3-install.patch"
}

build() {
  rm -rf build-${MINGW_CHOST}
  cp -rf Botan-${pkgver} build-${MINGW_CHOST}
  cd build-${MINGW_CHOST}

  sed -i "s|/mingw|${MINGW_PREFIX}|g" src/build-data/os/mingw.txt

  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-shared \
    --disable-modules=thread_utils \
    --os=mingw \
    --cc=gcc \
    --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-${MINGW_CHOST}"
  ./botan-test
}

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