##
##  git.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package information
Name:         git
Summary:      Distributed Version Control System
URL:          http://git-scm.com/
Vendor:       Linus Torvalds, Junio C Hamano
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        SCM
License:      GPL
Version:      2.51.0
Release:      20250822

#   package options
%option       with_doc     no
%option       with_svn     no

#   list of sources
Source0:      https://www.kernel.org/pub/software/scm/git/git-%{version}.tar.gz
Source1:      rc.git
Source2:      git-notify
Source3:      gitconfig
Patch0:       git.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  gcc, make, perl-openpkg
BuildPreReq:  bash, perl, diffutils
PreReq:       bash, perl, diffutils
BuildPreReq:  zlib, openssl3, curl, expat, libiconv, pcre2
PreReq:       zlib, openssl3, curl, expat, libiconv, pcre2
BuildPreReq:  python
PreReq:       python
%if "%{with_doc}" == "yes"
BuildPreReq:  asciidoc, xmlto
%endif
%if "%{with_svn}" == "yes"
PreReq:       subversion-perl, perl-sys
%endif

%description
    GIT is a "directory content manager" designed to handle absolutely
    massive projects with speed and efficiency. GIT falls in the
    category of distributed source code management tools. Every GIT
    working directory is a full-fledged repository with full revision
    tracking capabilities, not dependent on network access to a central
    server.

%track
    prog git = {
        version   = %{version}
        url       = http://git-scm.com/downloads
        regex     = version.>\s*(\d+(\.\d+)+)
    }

%prep
    %setup -q
    %patch -p0

%build
    #   build Git core
    find . -name "*.[ch]" -print |\
    xargs %{l_shtool} subst \
        -e 's;struct option;struct git_option;g'
    ( echo "GITWEB_CONFIG         = %{l_prefix}/etc/git/gitweb.config.pl"
      echo "GITWEB_BASE_URL       = /openpkg-cgi/gitweb.d"
      echo "GITWEB_CSS            = /openpkg-cgi/gitweb.d/static/gitweb.css"
      echo "GITWEB_JS             = /openpkg-cgi/gitweb.d/static/gitweb.js"
      echo "GITWEB_LOGO           = /openpkg-cgi/gitweb.d/static/git-logo.png"
      echo "GITWEB_FAVICON        = /openpkg-cgi/gitweb.d/static/git-favicon.png"
      echo "GITWEB_PROJECTROOT    = %{l_prefix}/var/git"
      echo "PYTHON_PATH           = %{l_prefix}/bin/python"
      echo "NEEDS_CRYPTO_WITH_SSL = YesPlease"
    ) >config.mak
    ( echo "ac_cv_header_libintl_h=no"
    ) >config.cache
    CC="%{l_cc} %{l_cppflags openssl3 .} %{l_ldflags openssl3 .}" \
    CFLAGS="%{l_cflags -O} `curl-config --cflags`" \
    CPPFLAGS="%{l_cppflags openssl3 .}" \
    LDFLAGS="%{l_ldflags openssl3 .}" \
    LIBS="`curl-config --libs` -lssl -lcrypto -lz" \
    ./configure \
        --cache-file=./config.cache \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --with-gitconfig=%{l_prefix}/etc/git/gitconfig \
        --with-gitattributes=%{l_prefix}/etc/git/gitattributes \
        --with-openssl=%{l_prefix} \
        --with-libpcre2=%{l_prefix} \
        --with-curl=%{l_prefix} \
        --with-expat=%{l_prefix} \
        --with-iconv=%{l_prefix} \
        --with-zlib=%{l_prefix} \
        --with-shell=%{l_prefix}/bin/bash \
        --with-perl=%{l_prefix}/bin/perl \
        --without-python \
        --without-tcltk \
        --disable-pthreads
    %{l_make} %{l_mflags}
%if "%{with_doc}" == "yes"
    ( cd Documentation
      %{l_make} %{l_mflags} man
    ) || exit $?
%endif

%install
    #   install Git core
    %{l_make} %{l_mflags} install \
        DESTDIR=$RPM_BUILD_ROOT
%if "%{with_doc}" == "yes"
    ( cd Documentation
      %{l_shtool} mkdir -f -p -m 755 \
          $RPM_BUILD_ROOT%{l_prefix}/man/man1 \
          $RPM_BUILD_ROOT%{l_prefix}/man/man5 \
          $RPM_BUILD_ROOT%{l_prefix}/man/man7
      %{l_shtool} install -c -m 644 \
          *.1 $RPM_BUILD_ROOT%{l_prefix}/man/man1/
      %{l_shtool} install -c -m 644 \
          *.5 $RPM_BUILD_ROOT%{l_prefix}/man/man5/
      %{l_shtool} install -c -m 644 \
          *.7 $RPM_BUILD_ROOT%{l_prefix}/man/man7/
    ) || exit $?
%endif
%if "%{with_svn}" == "yes"
    mv  $RPM_BUILD_ROOT%{l_prefix}/libexec/git/git-svn \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/git/git-svn.pl
    (   echo "#!/bin/sh"
        echo "LD_LIBRARY_PATH=\"%{l_prefix}/lib/subversion-perl:/usr/lib:/lib\""
        echo "export LD_LIBRARY_PATH"
        echo "exec %{l_prefix}/libexec/git/git-svn.pl \"\$@\""
    ) >$RPM_BUILD_ROOT%{l_prefix}/libexec/git/git-svn
    chmod 755 $RPM_BUILD_ROOT%{l_prefix}/libexec/git/git-svn
%else
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/libexec/git/git-svn
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/lib/perl/vendor_perl/*/Git/SVN
%endif
    version=`perl -V:version | sed -e "s;.*'\\(.*\\)'.*;\\1;"`
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/lib/perl/vendor_perl/$version
    mv  $RPM_BUILD_ROOT%{l_prefix}/share/perl5/* \
        $RPM_BUILD_ROOT%{l_prefix}/lib/perl/vendor_perl/$version/
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/perl5

    #   install git-notify(1) addon utility
    %{l_shtool} install -c -m 755 \
        -e 's;/usr/bin/perl;%{l_prefix}/bin/perl;' \
        %{SOURCE git-notify} \
        $RPM_BUILD_ROOT%{l_prefix}/bin/

    #   install GIT web interface
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/cgi/gitweb.d/static \
        $RPM_BUILD_ROOT%{l_prefix}/etc/git
    %{l_shtool} install -c -m 755 \
        gitweb/gitweb.cgi $RPM_BUILD_ROOT%{l_prefix}/cgi/
    %{l_shtool} install -c -m 644 \
        gitweb/static/* \
        $RPM_BUILD_ROOT%{l_prefix}/cgi/gitweb.d/static/
    (   echo "##"
        echo "##  gitweb.config.pl -- gitweb Perl configuration "
        echo "##"
        echo ""
        echo "1;"
    ) >gitweb.config.pl
    %{l_shtool} install -c -m 755 \
        gitweb.config.pl $RPM_BUILD_ROOT%{l_prefix}/etc/git/

    #   provide HTTP backend CGI under canonical path for Apache
    ln  $RPM_BUILD_ROOT%{l_prefix}/libexec/git/git-http-backend \
        $RPM_BUILD_ROOT%{l_prefix}/cgi/git-http-backend

    #   install GIT bash programmable completion
    %{l_shtool} install -c -m 644 \
        contrib/completion/git-completion.bash \
        $RPM_BUILD_ROOT%{l_prefix}/etc/git/git.bashrc

    #   install default configuration
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE gitconfig} $RPM_BUILD_ROOT%{l_prefix}/etc/git/

    #   install run-command script
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.git} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   strip down installation
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    strip $RPM_BUILD_ROOT%{l_prefix}/libexec/git-core/* >/dev/null 2>&1 || true
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/doc
    rmdir $RPM_BUILD_ROOT%{l_prefix}/share >/dev/null 2>&1 || true

    #   create additional directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/git/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/git/db

    #   determine installation files
    %{l_prefix}/bin/perl-openpkg -F perl-openpkg-files fixate cleanup
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} `cat perl-openpkg-files` \
        '%config %{l_prefix}/etc/git/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/git/*'

%files -f files

%clean

