# $Id: PKGBUILD 145820 2015-11-03 15:32:08Z foutrelis $
# Maintainer: Massimiliano Torromeo <massimiliano.torromeo@gmail.com>
# Contributor: James Miller <james@pocketrent.com>

pkgname=hhvm
pkgver=3.10.1
pkgrel=2

_thirdparty_commit=25006693fa6f5f09123d915225bc1b240102dfd6
_folly_commit=3a6c9d3896badace174e6933c0a764f740e921c8
_mcrouter_commit=2d51823b9516c9609da3c7c5022357ec33cd25f6
_proxygen_commit=080b2b157915f7e970d9c406659ad4f8f9f0bedd
_squangle_commit=8a074e1550c904816debb2221a829e0a4b1b6261
_thrift_commit=181044fd78e0a26e77fb519e1cbd10238c2e32d6
_wangle_commit=c1e434b725ca7e8336a0401f4bed548ea1aefc78
_webscalesql_commit=04456ee8cb3f537537873504b33cdea17e346b12

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=('cdb7002f3cb8252f706fdf1ec7fc03a52a56cd2d8d9c3ee0951d82a1acf80fad'
            '1dd63ae9304bd49a5f43e04ff64866f03e826e287c6387a0cc1a31e67c7279eb'
            'ffca3a5d1867f104fd6a6699312d6c12ad6364cec24c05d6d5901ef79e974f59'
            '945e7100d14f44dd82f006742b5c5d703f979f034f701d14a99f2b2f1aaec5c6'
            'f8debd25e76a4c9a645e5aa7b49568085f88445ac869e57c2580b343289f5dc3'
            '6cb667f5e21d32955fdc359a5c490aa5c3d13490a51a82d66325994e0b031b82'
            '235dc77e49f067da3032925d4742758cbce0bc95c5b694c0891287aca888e152'
            '163be1619b94bdd758f8e06d10909ef12e6c3c6ebd53a27b33d0b37d9b4ef7ea'
            'a5534f1d6f975117ec27cfb354052ebb88fda3a7339b13de33c30ea468e84b1e'
            'c356010a6d6b976f387bb205a75ea07d5f40593a8010483f2ed0f66f112331bc'
            '8b50d1ef9f5f726e6d8d469a8c84d85ad63f8b507b97d258b4d751a0e3e221df'
            '59c640602929dac0aa34d06c668ed69361eb4b7b47a77f9aa0badb4d0b61571c'
            '3e3093f817706c238fad021483f114fd4ce0b45d84097dcb7870157fc9ec769f'
            '5b53bc57965e1c5151d720dc7f63f1b2e8ebd5e758b2ef0be3b74df38ebcbce0'
            '3a7d1cfa7fb87365bbfc65975b8a96627c34d5389eb0de9c360f195cb717dfd0'
            '8fe2192e3e10d46a77bbe4fda7ed588eecda22fd83d5952c79985f642bf00d68')
