#!/bin/sh

. $Y_PORT_UTILS/sig.sh

SRC_FILENAME="${GNU_PROJECT}-${Y_PORT_VERSION}.tar.gz"
RELEASE_BASE="https://ftp.gnu.org/gnu/${GNU_PROJECT}"
RELEASE_URL="$RELEASE_BASE/$SRC_FILENAME"

gnu_fetch() {
    source_dir=$2
    build_dir=$3

    mkdir -p $source_dir
    cd $source_dir

    if [ ! -f .source-ready ]; then
        curl -LO $RELEASE_URL
        verify_sha256 $SRC_SHA256 $SRC_FILENAME

        tar xf $SRC_FILENAME

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

        touch $source_dir/.source-ready
    fi
}

gnu_compile() {
    source_dir=$2
    build_dir=$3

    mkdir -p $build_dir
    cd $build_dir

    if [ ! -f Makefile ]; then
        CC="clang" CFLAGS="-DNO_ARCHIVES -fPIC -target $Y_TRIPLE --sysroot $Y_SYSROOT" \
            $source_dir/${GNU_PROJECT}-${Y_PORT_VERSION}/configure \
            --prefix=/usr \
            --build=x86_64-pc-linux-gnu \
            --host=x86_64-unknown-yggdrasil \
            $GNU_CONFIGURE_OPTIONS
    fi

    make -j
}

gnu_install() {
    build_dir=$3
    cd $build_dir
    make -j install DESTDIR=$Y_SYSROOT >/dev/null
}
