# Build recipe for git.
#
# Copyright (c) 2017-2019, 2021-2023 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=git
version=2.40.0
release=1

# Define a category for the output of the package name
pkgcategory=devel

tarname=${program}-${version}.tar.gz
tarname_manpages=${program}-manpages-${version}.tar.gz

# Remote source(s)
fetch="
  https://mirrors.edge.kernel.org/pub/software/scm/git/$tarname
  https://mirrors.edge.kernel.org/pub/software/scm/git/$tarname_manpages
"

description="
A fast, scalable, distributed revision control system.

Git is a fast, scalable, distributed revision control system with an
unusually rich command set that provides both high-level operations
and full access to internals.

Git is an Open Source project covered by the GNU General Public
License version 2 (some parts of it are under different licenses,
compatible with the GPLv2). It was originally written by Linus
Torvalds with help of a group of hackers around the net.
"

homepage=https://www.git-scm.org
license="GPLv2 only, LGPLv2.1"

# Source documentation
docs="COPYING LGPL-2.1 README*"
docsdir="${docdir}/${program}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    cat << EOF >> config.mak
NEEDS_CRYPTO_WITH_SSL=1
INSTALL_SYMLINKS=1
EOF

    ./configure CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS" \
    LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --docdir=$docsdir \
     --with-libpcre2 \
     --with-tcltk \
     --with-python=python3 \
     --build="$(gcc -dumpmachine)"

    make -j${jobs} V=1 INSTALLDIRS=vendor
    make -j${jobs} V=1 INSTALLDIRS=vendor \
     DESTDIR="$destdir" install

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"

            # Unpack provided man-pages
            tar xf "${tardir}/$tarname_manpages"

            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi
    unset -v tarname_manpages

    # Clean up inventory file for Perl

    # This line removes 'destdir' adding the suffix '.lz' for manpages
    find "${destdir}/" -type f -name '.packlist' | while read -r file
    do
         sed -i \
          -e "s|${destdir}||g" \
          -e "s|\.[0-9][a-z]*|&.lz|g" \
          $file
    done

    find "${destdir}/" -type f -name 'perllocal.pod' -exec rm -f {} +

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

