#!/bin/sh

. $Y_PORT_UTILS/sig.sh

set -e

SRC_FILENAME="v${Y_PORT_VERSION}.tar.gz"
SRC_SHA256="c0bac12a6596f2315341790a7f386f9162a5b1f98db9ec40d883fce64e231942"
URL_BASE="https://github.com/JuliaMath/openlibm/archive/refs/tags"
RELEASE_URL="$URL_BASE/$SRC_FILENAME"

source_dir=$2
build_dir=$3

if [ ! -f $build_dir/.source-ready ]; then
    mkdir -p $build_dir
    cd $build_dir

    curl -LO $RELEASE_URL
    verify_sha256 $SRC_SHA256 $SRC_FILENAME

    tar xf $SRC_FILENAME

    cd openlibm-${Y_PORT_VERSION}
    for patch in $1/patches/*.patch; do
        echo Apply $patch
        patch -p1 <$patch
    done

    touch $build_dir/.source-ready
fi
