##
##  gitlist.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 version
%define       V_gitlist      1.1.0
%define       V_gitlist_snap 20220115

#   package information
Name:         gitlist
Summary:      Git Repository Browser
URL:          http://gitlist.org/
Vendor:       Klaus Silveira
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        SCM
License:      MIT
Version:      %{V_gitlist}
Release:      20220115

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/gitlist/gitlist-%{V_gitlist_snap}.tar.gz
Source1:      gitlist-apache.conf
Source2:      gitlist-config.ini
Patch0:       gitlist.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       git
PreReq:       git-apache
PreReq:       apache
PreReq:       apache-php
PreReq:       apache-php::with_mhash = yes
PreReq:       apache-php::with_ctype = yes
PreReq:       apache-php::with_simplexml = yes
PreReq:       apache-php::with_json = yes

%description
    GitList is an elegant and modern web interface for interacting with
    multiple Git repositories. It allows you to browse Git repositories
    using your favorite web browser, viewing files under different
    revisions, commit history, diffs. It also generates RSS feeds for
    each Git repository, allowing you to stay up-to-date with the latest
    changes anytime, anywhere.

%track
    prog gitlist:release = {
        version   = %{V_gitlist}
        url       = https://github.com/klaussilveira/gitlist/releases
        regex     = (\d+\.\d+\.\d+)\.tar\.gz
    }
    prog gitlist:snapshot = {
        version   = %{V_gitlist_snap}
        url       = http://download.openpkg.org/components/versioned/gitlist/
        regex     = gitlist-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -n gitlist
    %patch -p0

%build
    #   get rid of problematic and not necessary files
    rm -rf vendor/symfony/finder/Symfony/Component/Finder/Tests
    rm -rf vendor/twig/twig/test

    #   remove useless files
    rm -rf .git
    rm -f .*.orig
    rm -f README.md INSTALL.md LICENSE.txt
    rm -f config.ini-example

    #   change path to configuration file
    %{l_shtool} subst \
        -e 's;config\.ini;%{l_prefix}/etc/gitlist/gitlist.ini;g' \
        index.php
    %{l_shtool} subst %{l_value -s -a} \
        src/GitList/Application.php

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/gitlist \
        $RPM_BUILD_ROOT%{l_prefix}/etc/apache/apache.d \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/gitlist \
        $RPM_BUILD_ROOT%{l_prefix}/var/gitlist/cache

    #   install configuration files
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE gitlist-config.ini} $RPM_BUILD_ROOT%{l_prefix}/etc/gitlist/gitlist.ini
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE gitlist-apache.conf} $RPM_BUILD_ROOT%{l_prefix}/etc/apache/apache.d/gitlist.conf

    #   install program files
    cp -rp * .[a-z]* \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/gitlist/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/gitlist/*' \
        '%attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/var/gitlist/*'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   after install, restart Apache
        eval `%{l_rc} apache status 2>/dev/null`
        [ ".$apache_active" = .yes ] && %{l_rc} apache restart
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart Apache
        eval `%{l_rc} apache status 2>/dev/null`
        [ ".$apache_active" = .yes ] && %{l_rc} apache restart
    fi
    exit 0

%postun
    if [ $1 -eq 0 ]; then
        #   after erase, restart Apache
        eval `%{l_rc} apache status 2>/dev/null`
        [ ".$apache_active" = .yes ] && %{l_rc} apache restart

        #   after erase, remove cache files
        rm -rf $RPM_INSTALL_PREFIX/var/gitlist/cache/* >/dev/null 2>&1 || true
    fi
    exit 0

