# Maintainer: Giovanni Scafora <giovanni@archlinux.org>
# Maintainer: Zach Bacon <11doctorwhocanada@gmail.com>
# Contributor: Gilles CHAUVIN <gcnweb@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=live-media
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2021.04.06
pkgrel=1
pkgdesc="A set of C++ libraries for multimedia streaming (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('LGPL')
url="http://live555.com/liveMedia"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=("rsync")
options=('staticlibs')
source=("https://download.videolan.org/contrib/live555/live.${pkgver}.tar.gz"
        0001-Add-a-pkg-config-file-for-the-shared-libraries.patch
        0002-expose-server_string.patch
        0003-mingw-static-libs.patch
        0004-live555-formatmessage.patch
        0005-Fix-testProgs-static-lib-link-order.patch
        0006-mingw-NO_GETIFADDRS.patch)
sha256sums=('0bd182fac7d6297d924af4405ce840515a38fa5438cb1a4b90fcb6bc737b36d6'
            '8611cb5eb31571d4026a66d47a98f2a08abf3b6c3ac373ee0c28638a3bf3c4dc'
            '84e1df79f5a2496068d37211dcd4c4a8c72c779cdf2f9d75d61847c08bb0eb91'
            '6111b8119b80eca6a2fdd1c116f2e3509d62098d3c36944bd1bf83bb03342c87'
            '58255bbd2e43112e36647d1194aa0cfa98d0e9bf58a8a41d59e1e81e1589028a'
            '9fb73d9da3c9448cc47ef14a3978861ed0d442822f48481ae8d858db1b04ae8e'
            '1a52a453eff64204d06f7616da19ce8def5798194ed20e93cc470cd0007a35ba')

prepare() {
  cd ${srcdir}/live

  chmod -R a+w *
  find . -name "*.rej" -exec rm -f {} \;
  find . -name "*.orig" -exec rm -f {} \;

  [[ -f live555.pc.in ]] && rm -f live555.pc.in
  patch -p1 -i ${srcdir}/0001-Add-a-pkg-config-file-for-the-shared-libraries.patch
  patch -p1 -i ${srcdir}/0002-expose-server_string.patch
  patch -p1 -i ${srcdir}/0003-mingw-static-libs.patch
  patch -p1 -i ${srcdir}/0004-live555-formatmessage.patch
  patch -p1 -i ${srcdir}/0005-Fix-testProgs-static-lib-link-order.patch
  patch -p1 -i ${srcdir}/0006-mingw-NO_GETIFADDRS.patch
}

build() {
  cd ${srcdir}/live
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}
  # This cp is necessary since these are symlink targets but get extracted
  # later. While this shouldn't matter (symlink targets are allowed not to
  # exist on Windows as per Unix), it currently does as various MSYS2 tools
  # pre-date working Windows symlinks.
  cp ${srcdir}/live/COPYING* .
  rsync -rav ${srcdir}/live/* .
  ./genMakefiles mingw

  make CC=gcc
}

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

  make install DESTDIR=${pkgdir} PREFIX=${MINGW_PREFIX}
  make install_shared_libraries DESTDIR=${pkgdir} PREFIX=${MINGW_PREFIX}

  for dir in BasicUsageEnvironment groupsock liveMedia UsageEnvironment; do
    install -dm755 ${pkgdir}${MINGW_PREFIX}/{bin,lib,include/${dir}}
    install -m644 ${dir}/*.a "${pkgdir}${MINGW_PREFIX}/lib"
    install -m644 ${dir}/include/*.h* "${pkgdir}${MINGW_PREFIX}/include/${dir}"
  done

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/pkgconfig"
  install -m644 ${pkgdir}/pkgconfig/live555.pc "${pkgdir}${MINGW_PREFIX}/share/pkgconfig"
  sed -i "s|^prefix=$|prefix=${MINGW_PREFIX}|" "${pkgdir}${MINGW_PREFIX}/share/pkgconfig/live555.pc"
  sed -i "s|^libdir=$|libdir=${MINGW_PREFIX}/lib|" "${pkgdir}${MINGW_PREFIX}/share/pkgconfig/live555.pc"
  rm -rf ${pkgdir}/pkgconfig

  for testprog in `find testProgs -type f -perm 755`; do
    install ${testprog} "${pkgdir}${MINGW_PREFIX}/bin"
  done
}
