#!/bin/sh -ue
NAME=git
VERSION=2.48.1
RELEASE=3
SOURCE="https://mirrors.edge.kernel.org/pub/software/scm/git/git-${VERSION}.tar.gz"

build() {
    cd "$SRC/git-${VERSION}" || die "cannot enter source directory"
    _curl_libs="-Wl,-Bstatic -lcurl -lbearssl -lzstd -lz"

    if command -v pkg-config >/dev/null 2>&1; then
        _pc_libs=$(pkg-config --static --libs libcurl 2>/dev/null || true)
        if [ -n "$_pc_libs" ]; then
            _curl_libs="$_pc_libs"
        fi
    fi

    msg "disabling templates to avoid tar compatibility issues"
    cat > templates/Makefile <<'TMPL_EOF'
install:
	@true
clean:
	@true
all:
	@true
TMPL_EOF

    msg "creating config.mak"
    cat > config.mak <<CFGEOF
SHELL_PATH = /bin/sh
NO_GETTEXT = YesPlease
NO_SVN_TESTS = YesPlease
NO_TCLTK = YesPlease
NO_EXPAT = YesPlease
NO_NSEC = YesPlease
NO_OPENSSL = YesPlease
NO_PYTHON = YesPlease
NO_PERL = YesPlease
NO_SYS_POLL_H = 1
NO_CROSS_DIRECTORY_HARDLINKS = 1
NO_INSTALL_HARDLINKS = 1
NO_REGEX = NeedsStartEnd
NO_INSTALL_TEMPLATES = 1
# Force static linking
CC = ${CC:-cc}
LDFLAGS = -static
BASIC_LDFLAGS =
CC_LD_DYNPATH =
EXTLIBS += -lpthread -lrt
CFGEOF

    msg "building with gmake"
    gmake \
        SHELL=/bin/sh \
        prefix="$PREFIX" \
        CURL_LIBCURL="$_curl_libs" \
        V=1 \
        -j"$NPROC" \
        all || die "gmake build failed"

    msg "installing"
    gmake prefix="$PREFIX" DESTDIR="$PKG" install || die "gmake install failed"
}

. ${0%/*}/../../libsh/libdmake.sh
