# Maintainer: Renato Silva <br.renatosilva@gmail.com>
# Contributor: David Macek <david.macek.0@gmail.com>

_realname=silc-toolkit
pkgbase=mingw-w64-${_realname}
pkgname=(${MINGW_PACKAGE_PREFIX}-${_realname})
pkgver=1.1.12
pkgrel=6
pkgdesc="Secure Internet Live Conferencing (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="http://silcnet.org/"
msys2_repository_url="https://github.com/silc/silc"
license=('spdx:BSD-3-Clause OR GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-libsystre")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools")
options=('!emptydirs')
source=(#https://github.com/silc/silc/archive/${_realname//-/.}.${pkgver}.tar.gz
        "https://downloads.sourceforge.net/project/silc/silc/toolkit/sources/silc-toolkit-${pkgver}.tar.bz2"
        001-remove-outdated-mno-cygwin-gcc-option.patch
        002-fixes-for-64-bit.patch
        003-fix-references-to-slash-usr.patch
        004-link-with-regex-and-silc.patch
        005-fix-some-compiler-check.patch
        006-disable-dllimport-for-silcske.patch
        007-ucrt-fixes.patch)
sha256sums=('be170288f79fbe34971fc26fe762a8329621e11a117f20c218857f2bbad24d6e'
            '0d415556adeba40b1fc6b6660e612f8c96507c07ab025916acb726932e144aa4'
            '68f96d3d20c7cca3b8be7480ff632947c8aed5f8dd9fb731978f61a41ed0ff70'
            '652362d5f316c4e7030d574b59a6199fbb44aed398229aed8b91b54cfa7fe696'
            'cf0cd49f2d471cd71510a1959d4dcdd8f33b3a7c4ab70820a3ef44972b2ce931'
            '184c10c6e066c61f8bdcee84be7aa28a7ab906353fe082b6328faf8fbfb7799b'
            'adc6f10cbaf6e48a6fa03d4fd65c604ede480382ea0ce7cad5ff44b3ae87b3be'
            'e128c02b2e52cdaaa57b0993676a539784b0b8cb8d5ec6159c67a4906b3b3e63')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

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

  apply_patch_with_msg \
    001-remove-outdated-mno-cygwin-gcc-option.patch \
    002-fixes-for-64-bit.patch \
    003-fix-references-to-slash-usr.patch \
    004-link-with-regex-and-silc.patch \
    005-fix-some-compiler-check.patch \
    006-disable-dllimport-for-silcske.patch \
    007-ucrt-fixes.patch

  sed "s/SILC_ADD_CC_FLAGS(SILC_CRYPTO, -fno-regmove)//g" -i configure.ac

  autoreconf -fiv
}

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

  CFLAGS+=" -Wno-int-conversion -Wno-incompatible-pointer-types -Wno-incompatible-function-pointer-types" \
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --with-simdir=${MINGW_PREFIX}/lib/silc/modules \
    --with-win32

  make
}

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

  make DESTDIR="${pkgdir}" install

  # Licenses
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/GPL \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/GPL
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/BSD \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/BSD
}
