#!/bin/bash

set -e

here=$(dirname $(realpath "$0" 2> /dev/null || grealpath "$0"))
. "$here"/base.sh || (echo "Could not source contrib/base.sh" && exit 1)

setup_pkg "libevent" "https://github.com/libevent/libevent.git" "release-2.1.13-stable"

if ! [ -x configure ] ; then
    ./autogen.sh || fail "Could not configure $pkgname"
fi
if ! [ -r config.status ] ; then
    ./configure \
        $AUTOCONF_FLAGS \
        --prefix="$pkgbuilddir/dist" \
        --disable-shared \
        --enable-static \
        --with-pic \
        --disable-samples \
        --disable-libevent-regress \
        CPPFLAGS="-I$parentbuilddir/openssl/dist/include" \
        LDFLAGS="-L$parentbuilddir/openssl/dist/lib" \
        OPENSSL_CFLAGS="-I$parentbuilddir/openssl/dist/include" \
        OPENSSL_LIBS="-L$parentbuilddir/openssl/dist/lib -lssl -lcrypto" \
        PKG_CONFIG_PATH="$parentbuilddir/openssl/dist/lib/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}" || fail "Could not configure $pkgname"
fi
make -j$WORKER_COUNT || fail "Could not build $pkgname"
make install || fail "Could not install $pkgname"
popd_pkg
