#!/usr/bin/env bash

# we need libgmp-dev
needed=( libgmp-dev )
missing=$(comm -1 -3 <(dpkg-query -W -f '${package}\n' | sort) <(IFS=$'\n'; echo "${needed[*]}" | sort))
if [[ "$missing" ]]; then
	apt-get update -qq || { echo "Failed to 'apt-get update'. You must build this formula using Docker."; exit 1; }
	apt-get install -q -y $missing
fi

dep_name=$(basename $BASH_SOURCE)

OUT_PREFIX=$1
export PATH=${OUT_PREFIX}/bin:${PATH}

# we need to declare the required version of libcassandra
dep_formula=${0#$WORKSPACE_DIR/}
dep_version=${dep_formula##*"/${dep_name}-"}
series=$(php-config --version | cut -d. -f1,2) # get "5.5", "5.6", "7.0" etc for the php requirement in the manifest
case "$dep_version" in
	*)
		MANIFEST_REQUIRE="${MANIFEST_REQUIRE:-"{\"heroku-sys/php\":\"${series}.*\",\"heroku-sys/libcassandra\":\"^2.7.0\",\"heroku-sys/libcassandra-abi\":\"^2\"}"}"
		;;
esac

CONFIGURE_EXTRA="--with-cassandra=${OUT_PREFIX}"

source $(dirname $BASH_SOURCE)/../pecl
