# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=srt
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.5.4
pkgrel=1
pkgdesc="Secure Reliable Transport (SRT) (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.srtalliance.org/"
msys2_repository_url="https://github.com/Haivision/srt"
msys2_references=(
  "cpe: cpe:/a:srtalliance:secure_reliable_transport"
)
license=('spdx:MPL-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
source=("${msys2_repository_url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        0006-no-msvc-compat-headers.patch)
sha256sums=('d0a8b600fe1b4eaaf6277530e3cfc8f15b8ce4035f16af4a5eb5d4b123640cdd'
            '18af8a55e898b12074e797a212eda5e6f3c935dcc37745eb97a60591e80ab64e')
noextract=(${_realname}-${pkgver}.tar.gz)

prepare() {
  [[ -d "${srcdir}"/${_realname}-${pkgver} ]] && rm -rf "${srcdir}"/${_realname}-${pkgver}
  tar -xzf "${srcdir}"/${_realname}-${pkgver}.tar.gz -C "${srcdir}" || true

  cd ${_realname}-${pkgver}

  patch -p1 -i "${srcdir}"/0006-no-msvc-compat-headers.patch
}

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

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      ${_extra_config[@]} \
      -DENABLE_SHARED=ON \
      -DENABLE_STATIC=ON \
      -DENABLE_RELATIVE_LIBPATH=ON \
      -DUSE_ENCLIB=openssl \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build .
}

package() {
  # Shared Install
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  local _WIN_PREFIX=$(cygpath -am ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -e "s|${_WIN_PREFIX}|${MINGW_PREFIX}|g" -i ${_f}
  done
}
