%global gitver      2.25.1
%global cachedir    %{_localstatedir}/cache/%{name}
%global filterdir   %{_libexecdir}/%{name}/filters
%global scriptdir   %{_localstatedir}/www/cgi-bin
%global cgitdata    %{_datadir}/%{name}
%global gpg_cgit    AB9942E6D4A4CFC3412620A749FC7012A5DE03AE
%global gpg_git     96E07AF25771955980DAD10020D04E5A713660A7
%global _python_bytecompile_extra 0
%global use_perl_interpreter    1
%bcond_with     highlight
%bcond_without  httpd_filesystem
Name:                cgit
Version:             1.2.3
Release:             2
Summary:             A fast web interface for git
License:             GPLv2
URL:                 https://git.zx2c4.com/cgit/
Source0:             https://git.zx2c4.com/cgit/snapshot/%{name}-%{version}.tar.xz
Source1:             https://www.kernel.org/pub/software/scm/git/git-%{gitver}.tar.xz
Source2:             cgitrc
Source3:             README.SELinux
Source90:            gpgkey-%{gpg_cgit}.asc
Source91:            gpgkey-%{gpg_git}.asc
Source92:            https://git.zx2c4.com/cgit/snapshot/%{name}-%{version}.tar.asc
Source93:            https://www.kernel.org/pub/software/scm/git/git-%{gitver}.tar.sign
Patch0:              cgit-0.9.1-highlightv3.patch
Provides:            bundled(git) = %gitver
%if %{with highlight}
BuildRequires:       highlight
%endif
BuildRequires:       asciidoc gcc gnupg2 libcurl-devel openssl-devel lua-devel make zlib-devel
BuildRequires:       gettext lzip
%if %{use_perl_interpreter}
BuildRequires:       perl-interpreter
%else
BuildRequires:       perl
%endif
BuildRequires:       perl(ExtUtils::MakeMaker) strace tidy unzip xz zstd
%if %{with httpd_filesystem}
Requires:            httpd-filesystem
%endif
Requires:            webserver
%description
Cgit is a fast web interface for git.  It uses caching to increase performance.

%prep
gpghome="$(mktemp -qd)"
verify_source() {
    local src=$1 key=$2 ext=$3 tar
    tar=${src/%.xz/}
    xz -dc $src > $tar
    gpg2 --dearmor --quiet --batch --yes $key >/dev/null
    gpgv2 --homedir "$gpghome" --quiet --keyring $key.gpg $tar$ext $tar
    rm -f $key.gpg $tar
}
verify_source %{SOURCE0} %{SOURCE90} .asc
verify_source %{SOURCE1} %{SOURCE91} .sign
rm -rf "$gpghome"
%autosetup -a 1 -p1
rm -rf git
mv git-%{gitver} git
cp -p %{SOURCE3} .
cat << \EOF > cgit.conf
V = 1
CFLAGS = %{optflags}
LDFLAGS = %{?__global_ldflags}
CACHE_ROOT = %{cachedir}
CGIT_SCRIPT_PATH = %{scriptdir}
CGIT_SCRIPT_NAME = cgit
CGIT_DATA_PATH = %{cgitdata}
COPYTREE = %{__cp} -rp
docdir = %{docdir}
filterdir = %{filterdir}
prefix = %{_prefix}
EOF
cat << \EOF > git/config.mak
V = 1
CFLAGS = %{optflags}
LDFLAGS = %{?__global_ldflags}
EOF
grep -rl '#!.*/env' filters/ | xargs -r sed -Ei 's@^(.+/)env (.+)$@\1\2@'
find contrib -type f | xargs -r chmod -x
cat > httpd.conf <<EOF
Alias /cgit-data /usr/share/cgit
ScriptAlias /cgit /var/www/cgi-bin/cgit
<Directory "/usr/share/cgit">
    Require all granted
</Directory>
EOF

%build
%make_build all doc-man doc-html
%if %{with highlight}
highlight --print-style --style-outfile=stdout >> cgit.css
%endif

%install
%make_install install install-man
install -d -m0755 %{buildroot}%{_sysconfdir}/httpd/conf.d
install -p -m0644 %{SOURCE2} %{buildroot}%{_sysconfdir}/cgitrc
install -p -m0644 httpd.conf %{buildroot}%{_sysconfdir}/httpd/conf.d/cgit.conf
install -d -m0755 %{buildroot}%{cachedir}

%check
%ifnarch loongarch64
make test
%endif

%files
%doc README* contrib *.html
%license COPYING
%config(noreplace) %{_sysconfdir}/cgitrc
%if ! %{with httpd_filesystem}
%dir %{_sysconfdir}/httpd
%dir %{_sysconfdir}/httpd/conf.d
%endif
%config(noreplace) %{_sysconfdir}/httpd/conf.d/cgit.conf
%dir %attr(-,apache,root) %{cachedir}
%{cgitdata}
%{filterdir}
%exclude %{filterdir}/*.py[co]
%{scriptdir}/*
%{_mandir}/man*/*

%changelog
* Tue Jun 6 2023 huajingyun <huajingyun@loongson.cn> - 1.2.3-2
- diable check for loongarch64

* Tue Sep 7 2021 zhengyaohui <zhengyaohui1@huawei.com> - 1.2.3-1
- package init
