#!/bin/sh

# SPDX-FileCopyrightText: 2021 Andrius Štikonas <andrius@stikonas.eu>
# SPDX-FileCopyrightText: 2021 Samuel Tyler <samuel@samuelt.me>
#
# SPDX-License-Identifier: GPL-3.0-or-later

set -ex

# Check tarball checksums
checksum-transcriber sources
sha256sum -c sources.SHA256SUM

mkdir build src
cd build

# Extract
cp ${DISTFILES}/${pkg}.tar.gz ../src/
gzip -d -f ../src/${pkg}.tar.gz
tar xf ../src/${pkg}.tar
rm -r ../src
cd ${pkg}

# Patch
patch -Np1 -i ../../patches/mes-libc.patch
patch -Np1 -i ../../patches/coreutils.patch

# Build
make CC=tcc AR="tcc -ar" LDFLAGS="-static" bzip2

# Install
cp bzip2 ${PREFIX}/bin/bzip2
cp bzip2 ${PREFIX}/bin/bunzip2
chmod 755 ${PREFIX}/bin/bzip2
chmod 755 ${PREFIX}/bin/bunzip2

# Test
bzip2 --help

cd ../..

# Checksums
if match x${UPDATE_CHECKSUMS} xTrue; then
    sha256sum -o ${pkg}.checksums \
        /usr/bin/bzip2

    cp ${pkg}.checksums ${SRCDIR}
else
    sha256sum -c ${pkg}.checksums
fi
