arch=""
mono=""

if ab_match_arch amd64; then
    arch=x64
elif ab_match_arch arm64; then
    arch=arm64
elif ab_match_arch riscv64; then
    arch=riscv64
elif ab_match_arch ppc64el; then
    arch=ppc64le
    # Note: CoreCLR has no ppc64le support
    mono="--use-mono-runtime"
elif ab_match_arch loongarch64; then
    # FIXME: compilation will fail when not using specific -march -mtune combination on LA464 CPU
    # possible compiler bug, when happened will result in NPE in either ilc or crossgen2:
    #   ilc throws System.NullReferenceException
    #   corssgen2 exited with code 139 (SIGSEGV)
    arch=loongarch64
    CFLAGS=${CFLAGS/-mtune=la664/-mtune=loongarch64}
    CXXFLAGS=${CXXFLAGS/-mtune=la664/-mtune=loongarch64}
elif ab_match_arch loongarch64_nosimd; then
    arch=loongarch64
    CFLAGS=${CFLAGS/-mtune=loongarch64/-mtune=la464}
    CXXFLAGS=${CXXFLAGS/-mtune=loongarch64/-mtune=la464}
else
    aberr "Unsupported architecture."
fi

abinfo "Installing build artifacts ..."
temp=$(mktemp -d)
mkdir -pv "$temp"/artifacts
tar -xzvf /opt/Private.SourceBuilt.Artifacts.* -C "$temp"/artifacts
mkdir -pv "$temp"/prereqs/packages/archive/
cp -v /opt/Private.SourceBuilt.Artifacts.* "$SRCDIR"/prereqs/packages/archive/

abinfo "Preparing .NET SDK source build ..."
"$SRCDIR"/prep-source-build.sh --no-sdk --no-artifacts --no-bootstrap \
    --with-sdk /usr/lib/dotnet/ \
    --with-packages "$temp"/artifacts/

abinfo "Building .NET SDK for $arch ..."
"$SRCDIR"/build.sh -sb --os linux --rid linux-$arch --arch $arch \
    --branding release -v n \
    --with-sdk /usr/lib/dotnet/ $mono

abinfo "Collecting generated tarballs ..."
mkdir -pv "$SRCDIR"/output
tar -xzvf "$SRCDIR"/artifacts/assets/Release/dotnet-sdk-* -C "$SRCDIR"/output
tar -xzvf "$SRCDIR"/artifacts/assets/Release/dotnet-symbols-sdk-* -C "$SRCDIR"/output

mkdir -pv "$PKGDIR"/opt/
mv -v "$SRCDIR"/artifacts/assets/Release/Private.SourceBuilt.Artifacts.* \
    "$PKGDIR"/opt/

# Note: may required for 2xx SDK band bootstrap
mv -v "$SRCDIR"/artifacts/assets/Release/dotnet-symbols-* \
    "$PKGDIR"/opt/

# Note: dotnet use different version number in SDK and runtime
abinfo "Saving version info for later use ..."
ls "$SRCDIR"/output/shared/Microsoft.NETCore.App > "$SRCDIR"/runtime-version.txt
ls "$SRCDIR"/output/sdk > "$SRCDIR"/sdk-version.txt
ls "$SRCDIR"/output/shared/Microsoft.AspNetCore.App > "$SRCDIR"/aspnet-version.txt
