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

_realname=libbotan
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.3.0
pkgrel=1
pkgdesc='Crypto and TLS Library for Modern C++ (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://botan.randombit.net/'
msys2_repository_url='https://github.com/randombit/botan'
msys2_references=(
  'archlinux: botan'
)
license=('spdx:BSD-2-Clause')
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}-cc"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python-imagesize")
             #"${MINGW_PACKAGE_PREFIX}-doxygen"
             #"${MINGW_PACKAGE_PREFIX}-graphviz")
source=("https://botan.randombit.net/releases/Botan-${pkgver}.tar.xz"{,.asc}
        '002-winsock-link.patch')
noextract=("Botan-${pkgver}.tar.xz")
sha256sums=('368f11f426f1205aedb9e9e32368a16535dc11bd60351066e6f6664ec36b85b9'
            'SKIP'
            'cf9974f076d91e8f58f7d9688b7ab78cc790720923c73b293871fc7b83fd55c6')
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}/002-winsock-link.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-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"*
}
