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

_realname=x264
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=0.161.r3039.544c61f
pkgrel=2
pkgdesc="Library for encoding H264/AVC video streams (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.videolan.org/developers/x264.html"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-nasm"
             "git")
depends=("${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-l-smash"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-ffms2"))
options=('strip' 'staticlibs')
_commit="544c61f082194728d0391fb280a6e138ba320a96"
source=("${_realname}"::"git+https://code.videolan.org/videolan/${_realname}.git#commit=${_commit}"
        0001-beautify-pc.all.patch
        0002-install-avisynth_c.h.mingw.patch)
sha256sums=('SKIP'
            '8e92f4d6c1924664e7c9f5b797070a35f8eabbfce431f996167fe426fc833dda'
            '51d7efd8eb504d42767c04cfe8abb9c7ccbe84d76e7150c5c46535507763d1b8')

pkgver() {
  cd ${srcdir}/${_realname}

  ./version.sh | grep X264_POINTVER | sed -r 's/^#define X264_POINTVER "([0-9]+\.[0-9]+)\.([0-9]+)M? (.*)"$/\1.r\2.\3/'
}

prepare() {
  cd ${_realname}
  patch -p1 -i ${srcdir}/0001-beautify-pc.all.patch
  patch -p1 -i ${srcdir}/0002-install-avisynth_c.h.mingw.patch
}

build() {
  [[ -d "build-${MINGW_CHOST}" ]] && rm -rf "build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"
  ../${_realname}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --enable-static \
    --enable-shared

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
}
