# $Id$
# Maintainer: Maxime Gauduin <alucryd@archlinux.org>
# Contributor: Bartłomiej Piotrowski <bpiotrowski@archlinux.org>
# Contributor: kfgz <kfgz@interia.pl>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - patch to attempt fixing ARM hackery by bandwagoning developers

pkgname=x265
pkgver=2.5
pkgrel=1
pkgdesc='Open Source H265/HEVC video encoder'
arch=('i686' 'x86_64')
url='https://bitbucket.org/multicoreware/x265'
license=('GPL')
depends=('gcc-libs')
makedepends=('yasm' 'cmake')
provides=('libx265.so')
source=("https://bitbucket.org/multicoreware/x265/downloads/x265_${pkgver}.tar.gz"
        arm.patch)
sha256sums=('2e53259b504a7edb9b21b9800163b1ff4c90e60c74e23e7001d423c69c5d3d17'
            'c3e80d7261d5fef72bfea5c5dd4613c1e997315dc7d0d9465f99356287a8d46b')

prepare() {
  cd x265_${pkgver}

  patch -p1 -i ../arm.patch
  [[ $CARCH == "aarch64" ]] && CFLAGS+=" -fPIC" && CXXFLAGS+=" -fPIC"
  [[ $CARCH == "arm" ]] && CONFIG="-DENABLE_ASSEMBLY=OFF"

  for d in 8 $([[ $CARCH == 'x86_64' ]] && echo "10 12"); do
    if [[ -d build-$d ]]; then
      rm -rf build-$d
    fi
    mkdir build-$d
  done
}

build() {
  if [[ $CARCH == x86_64 ]]; then

    cd x265_${pkgver}/build-12

    cmake ../source \
      -DCMAKE_INSTALL_PREFIX='/usr' \
      -DHIGH_BIT_DEPTH='TRUE' \
      -DMAIN12='TRUE' \
      -DEXPORT_C_API='FALSE' \
      -DENABLE_CLI='FALSE' \
      -DENABLE_SHARED='FALSE'
    make

    cd ../build-10

    cmake ../source \
      -DCMAKE_INSTALL_PREFIX='/usr' \
      -DHIGH_BIT_DEPTH='TRUE' \
      -DEXPORT_C_API='FALSE' \
      -DENABLE_CLI='FALSE' \
      -DENABLE_SHARED='FALSE'
    make

    cd ../build-8

    ln -s ../build-10/libx265.a libx265_main10.a
    ln -s ../build-12/libx265.a libx265_main12.a

    cmake ../source \
      -DCMAKE_INSTALL_PREFIX='/usr' \
      -DENABLE_SHARED='TRUE' \
      -DENABLE_HDR10_PLUS='TRUE' \
      -DEXTRA_LIB='x265_main10.a;x265_main12.a' \
      -DEXTRA_LINK_FLAGS='-L.' \
      -DLINKED_10BIT='TRUE' \
      -DLINKED_12BIT='TRUE'
    make

  else

    cd x265_${pkgver}/build-8

    cmake ../source \
      -DCMAKE_INSTALL_PREFIX='/usr' \
      -DENABLE_SHARED='TRUE' \
      $CONFIG

  fi
}

package() {
  cd x265_${pkgver}/build-8

  make DESTDIR="${pkgdir}" install
}