# Maintainer: David Macek <david.macek.0@gmail.com>

_realname=port-scanner
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.3
pkgrel=8
pkgdesc="A multi threaded TCP port scanner from SecPoint.com (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.secpoint.com/multi-threaded-tcp-port-scanner.html"
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")
source=("https://www.secpoint.com/freetools/threaded-port-scanner-${pkgver}.zip"
        "pathtools.c"
        "pathtools.h"
        "paths.patch"
        "0001-use-CC-from-environment.patch")
sha256sums=('768c595fba7ba7e81da35e1bba1118bf08a1d689c6e419804d2109fc64177436'
            'ebf471173f5ee9c4416c10a78760cea8afaf1a4a6e653977321e8547ce7bf3c0'
            '1585ef1b61cf53a2ca27049c11d49e0834683dfda798f03547761375df482a90'
            '7984cc958e87a519e3aa18a5bdd3e53f91e36f3b2aaed2e44acc6192272528e7'
            '66c18874119a4c36116f579f13b97aab538db399be61001f105a56ea1daeb03b')
DLAGENTS=('https::/usr/bin/curl -qb "" -fLC - -H user-agent:Mozilla -o %o %u')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

prepare() {
  test ! -d "${startdir}/../mingw-w64-pathtools" || {
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.c" "${srcdir}/pathtools.c" &&
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.h" "${srcdir}/pathtools.h"
  } || exit 1

  cd "${srcdir}/threaded-port-scanner-${pkgver}"
  cp -fHv "${srcdir}"/pathtools.[ch] ./

  apply_patch_with_msg \
    paths.patch \
    0001-use-CC-from-environment.patch

  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}"
  cp * "${srcdir}/build-${MSYSTEM}"
}

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

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  install -Dm755 pscan.exe "${pkgdir}${MINGW_PREFIX}/bin/pscan.exe"
  install -Dm644 oui.txt "${pkgdir}${MINGW_PREFIX}/share/port-scanner/oui.txt"
  install -Dm644 port-numbers.txt "${pkgdir}${MINGW_PREFIX}/share/port-scanner/port-numbers.txt"
}
