#!/usr/bin/env bash

# fail hard
set -o pipefail
# fail harder
set -eu

OUT_PREFIX=$1

dep_formula=${0#$WORKSPACE_DIR/} # this is the original script, e.g. pypy-5.3.1
dep_name=$(basename $BASH_SOURCE) # this is us
dep_version=${dep_formula##*"/${dep_name}-"} # "subtract" our name from full version name
dep_package=${dep_name}${dep_version_prefix:-}-v${dep_version}${dep_version_suffix:-}
dep_dirname=${dep_package}-linux64
dep_archive_name=${dep_dirname}.tar.bz2
dep_url=https://bitbucket.org/pypy/pypy/downloads/${dep_archive_name}

echo "Building PyPy3…"

curl -L "${dep_url}" | tar jx -C "${OUT_PREFIX}" --strip-components 1 # extract to $OUT_PREFIX, drop the first directory level, which is the archive name

ln "$OUT_PREFIX/bin/pypy3" "$OUT_PREFIX/bin/python"
