#!/bin/sh

. $Y_PORT_UTILS/sig.sh

set -e

SRC_FILENAME="llvm-project-${Y_PORT_VERSION}.src.tar.xz"
SRC_SHA256="324d483ff0b714c8ce7819a1b679dd9e4706cf91c6caf7336dc4ac0c1d3bf636"
RELEASE_BASE="https://github.com/llvm/llvm-project/releases/download/llvmorg-${Y_PORT_VERSION}"
RELEASE_URL="$RELEASE_BASE/$SRC_FILENAME"

source_dir=$2
build_dir=$3

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

    curl -LO $RELEASE_URL
    verify_sha256 $SRC_SHA256 $SRC_FILENAME

    tar xf $SRC_FILENAME

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

    touch $source_dir/.source-ready
fi
