##
##  gerrit.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 versions
%define       V_gerrit       3.1.2
%define       V_bouncycastle 170

#   package information
Name:         gerrit
Summary:      Code Review System
URL:          https://www.gerritcodereview.com/
Vendor:       Google Inc.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Web
License:      Commercial
Version:      %{V_gerrit}
Release:      20211129

#   package source
Source0:      http://gerrit-releases.storage.googleapis.com/gerrit-%{V_gerrit}.war
Source1:      http://www.bouncycastle.org/download/bcprov-jdk15on-%{V_bouncycastle}.jar
Source2:      rc.gerrit
Source3:      gerrit.config

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       java, JAVA-JDK

%description
    Gerrit is a web based code review system, facilitating online code
    reviews for projects using the Git version control system. Gerrit
    makes reviews easier by showing changes in a side-by-side display,
    and allowing inline comments to be added by any reviewer. Gerrit
    simplifies Git based project maintainership by permitting any
    authorized user to submit changes to the master Git repository,
    rather than requiring all approved changes to be merged in by
    hand by the project maintainer. This functionality enables a more
    centralized usage of Git.

%track
    prog gerrit:gerrit = {
        version   = %{V_gerrit}
        url       = https://gerrit-releases.storage.googleapis.com/index.html
        regex     = Gerrit\s+(\d+\.\d+(?:\.\d+)?)
    }
    prog gerrit:bouncycastle = {
        version   = %{V_bouncycastle}
        url       = http://www.bouncycastle.org/latest_releases.html
        regex     = bcprov-jdk15on-(__VER__)\.jar
    }

%prep
    %setup -q -T -c

%build

%install
    #   create installation tree
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/share/gerrit \
        $RPM_BUILD_ROOT%{l_prefix}/var/gerrit/site \
        $RPM_BUILD_ROOT%{l_prefix}/var/gerrit/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/gerrit/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/gerrit/tmp \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/gerrit

    #   install program
    %{l_shtool} install -c -m 644 \
        %{SOURCE0} $RPM_BUILD_ROOT%{l_prefix}/libexec/gerrit/gerrit.war
    %{l_shtool} install -c -m 644 \
        %{SOURCE1} $RPM_BUILD_ROOT%{l_prefix}/libexec/gerrit/bcprov.jar

    #   install configuration
    eval `%{l_prefix}/bin/java-toolkit -e`
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        -e "s;@l_javahome@;$JAVA_HOME;g" \
        %{SOURCE gerrit.config} \
        $RPM_BUILD_ROOT%{l_prefix}/share/gerrit/

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

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
       cmd="TMPDIR=$RPM_INSTALL_PREFIX/var/gerrit/tmp; export TMPDIR;"
       cmd="cd $RPM_INSTALL_PREFIX/var/gerrit/site &&"
       cmd="$cmd mkdir review;"
       cmd="$cmd mkdir review/etc;"
       cmd="$cmd cp $RPM_INSTALL_PREFIX/share/gerrit/gerrit.config review/etc/;"
       cmd="$cmd $RPM_INSTALL_PREFIX/bin/java -jar $RPM_INSTALL_PREFIX/libexec/gerrit/gerrit.war"
       cmd="$cmd init -d review --no-auto-start --batch"
       su - %{l_rusr} -c "$cmd"
    fi

%preun
    if [ $1 -eq 0 ]; then
        rm -rf $RPM_INSTALL_PREFIX/var/gerrit/site/* >/dev/null 2>&1 || true
    fi

