# $Id: PKGBUILD 139437 2015-09-07 08:50:09Z mtorromeo $
# Maintainer: Massimiliano Torromeo <massimiliano.torromeo@gmail.com>
# Contributor: James Miller <james@pocketrent.com>

pkgname=hhvm
pkgver=3.9.1
pkgrel=1

_thirdparty_commit=5cfbd0ea334de25115546a08a9dbd2954c6f5ed5
_folly_commit=879db739d6bb0cd6a7035d4606c34c877077c88e
_thrift_commit=b5635e3675023979c87d8b16182402c1ffba32a4
_proxygen_commit=885e8444298343ddddbc6948f6bdb7f1d9edc540
_webscalesql_commit=04456ee8cb3f537537873504b33cdea17e346b12
_mcrouter_commit=8303e732b94469d213266dc512cece4860875305
_squangle_commit=2661b7b4ffd2533f1cead7c45d369177d44315ff
_wangle_commit=6ce19ed9d2a8dd2b68d2b23a180ceb107cb53091

pkgdesc="Virtual Machine, Runtime, and JIT for PHP"
arch=('x86_64')
url="http://hhvm.com"
license=('PHP')
depends=('boost-libs' 'google-glog' 'libmysqlclient' 'libmemcached' 'libzip'
         'libxslt' 'intel-tbb' 'libmcrypt' 'oniguruma' 'jemalloc' 'curl' 'libvpx'
         'libdwarf' 'imagemagick' 'libedit' 'sqlite' 'libyaml' 'fribidi' 're2'
         'gperf' 'c-client' 'unixodbc')
makedepends=('git' 'cmake' 'gcc' 'boost' 'gflags' 'python2' 'pfff' 'mongodb'
             'ragel' 'libmariadbclient')
source=("https://github.com/facebook/hhvm/archive/HHVM-$pkgver.tar.gz"
        "https://github.com/hhvm/hhvm-third-party/archive/$_thirdparty_commit/hhvm-third-party-$_thirdparty_commit.tar.gz"
        "https://github.com/facebook/folly/archive/$_folly_commit/folly-$_folly_commit.tar.gz"
        "https://github.com/facebook/fbthrift/archive/$_thrift_commit/thrift-$_thrift_commit.tar.gz"
        "https://github.com/facebook/proxygen/archive/$_proxygen_commit/proxygen-$_proxygen_commit.tar.gz"
        "https://github.com/webscalesql/webscalesql-5.6/archive/$_webscalesql_commit/webscalesql-$_webscalesql_commit.tar.gz"
        "https://github.com/facebook/mcrouter/archive/$_mcrouter_commit/mcrouter-$_mcrouter_commit.tar.gz"
        "https://github.com/facebook/squangle/archive/$_squangle_commit/squangle-$_squangle_commit.tar.gz"
        "https://github.com/facebook/wangle/archive/$_wangle_commit/wangle-$_wangle_commit.tar.gz"
        'hhvm.tmpfile'
        'hhvm.service'
        'hhvm@.service'
        'php.ini'
        'server.ini'
        'gcc51.patch'
        'libstdcxx-dual-abi.patch')
install=hhvm.install
backup=(etc/hhvm/{php,server}.ini)
options+=('!buildflags')

prepare() {
    cd "$srcdir"/$pkgname-HHVM-$pkgver

    sed -r 's/service hhvm (start|stop|restart)/systemctl \1 hhvm.service/' \
        -i hphp/tools/oss-repo-mode

    patch -p1 -i "$srcdir"/gcc51.patch

    # Fix sendmail path
    sed -r 's#/usr/lib/sendmail#/usr/bin/sendmail#g' -i \
        hphp/runtime/base/runtime-option.cpp

    rm -rf third-party
    ln -s "$srcdir"/hhvm-third-party-$_thirdparty_commit third-party

    cd third-party/folly
    rm -rf src
    ln -s "$srcdir"/folly-$_folly_commit src
    patch -d src -p1 -i "$srcdir"/libstdcxx-dual-abi.patch

    cd ../thrift
    rm -rf src
    ln -s "$srcdir"/fbthrift-$_thrift_commit src

    cd ../proxygen
    rm -rf src
    ln -s "$srcdir"/proxygen-$_proxygen_commit src

    cd ../mcrouter
    rm -rf src
    ln -s "$srcdir"/mcrouter-$_mcrouter_commit src

    cd ../wangle
    rm -rf src
    ln -s "$srcdir"/wangle-$_wangle_commit src

    cd ../squangle
    rm -rf src squangle
    ln -s "$srcdir"/squangle-$_squangle_commit src
    ln -s src/squangle

    cd ../webscalesqlclient
    rm -rf src webscalesql-5.6
    ln -s "$srcdir"/webscalesql-5.6-$_webscalesql_commit webscalesql-5.6
    ln -s "$srcdir"/webscalesql-5.6-$_webscalesql_commit src
}

build() {
    cd "$srcdir"/$pkgname-HHVM-$pkgver
    msg2 "Building hhvm"

    cmake -Wno-dev \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_INSTALL_PREFIX=/usr \
        -DCMAKE_INSTALL_LIBDIR=lib \
        -DCMAKE_PREFIX_PATH="$srcdir" \
        -DENABLE_MONGO:BOOL=ON \
        -DMYSQL_UNIX_SOCK_ADDR=/run/mysqld/mysqld.sock \
        .

    make

    for hacktool in hackificator remove_soft_types; do
        cd "$srcdir"/$pkgname-HHVM-$pkgver/hphp/hack/tools/$hacktool
        make depend
        make
    done
}

# check() {
#     cd "$srcdir"/$pkgname-HHVM-$pkgver/hphp/test
#     ./run --threads 8 quick
# }

package() {
    cd "$srcdir"/$pkgname-HHVM-$pkgver
    make DESTDIR="$pkgdir/" install

    cd hphp/hack/bin
    for bin in hh_* tools/*; do
        install -Dm755 $bin "$pkgdir"/usr/bin/$(basename $bin)
    done

    cd "$srcdir"
    install -Dm644 hhvm.tmpfile "$pkgdir"/usr/lib/tmpfiles.d/hhvm.conf
    install -Dm644 hhvm.service "$pkgdir"/usr/lib/systemd/system/hhvm.service
    install -Dm644 hhvm@.service "$pkgdir"/usr/lib/systemd/system/hhvm@.service

    install -Dm644 php.ini "$pkgdir"/etc/hhvm/php.ini
    install -Dm644 server.ini "$pkgdir"/etc/hhvm/server.ini
}

sha256sums=('b9e98bdea1923ed2bd36ecf9481f45f3afe1b1e57903437cae05273a019e4a62'
            '730d60164d5274011cded79ffef1da41f3d667521a6823f7dd91bd912f147406'
            '3e76945da52a79efa5ead9ffbcca21b8cdb1df85c1648154a94475ee30cc4afb'
            '72aed344806debe232ac3ed6196e6d1dc11f42926d44b31d9c3d962da5c88a37'
            'ed8f576d1a0d2cd52f9f24dd0de8ff6fb100cf0ddff0e1d0789f4f10654da676'
            '6cb667f5e21d32955fdc359a5c490aa5c3d13490a51a82d66325994e0b031b82'
            'e102aa2453337bd5b1c01e41ae7c60e8f472227dfe4674041a67b600c4b567cb'
            '5db586f6cb02f4f44089a054276ce2da185fa2de0f4eb85f9fcb0469629fb386'
            '8cd4166f06925452f70989ef01e579fc1d3989687ecc02ab449d7210bd8758fd'
            'c356010a6d6b976f387bb205a75ea07d5f40593a8010483f2ed0f66f112331bc'
            '8b50d1ef9f5f726e6d8d469a8c84d85ad63f8b507b97d258b4d751a0e3e221df'
            '59c640602929dac0aa34d06c668ed69361eb4b7b47a77f9aa0badb4d0b61571c'
            '3e3093f817706c238fad021483f114fd4ce0b45d84097dcb7870157fc9ec769f'
            '5b53bc57965e1c5151d720dc7f63f1b2e8ebd5e758b2ef0be3b74df38ebcbce0'
            '3a7d1cfa7fb87365bbfc65975b8a96627c34d5389eb0de9c360f195cb717dfd0'
            '8fe2192e3e10d46a77bbe4fda7ed588eecda22fd83d5952c79985f642bf00d68')
