##
##  trac.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:         trac
Summary:      Issue and Version Control Tracking System
URL:          http://trac.edgewall.org/
Vendor:       Edgewall
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        SCM
License:      GPL
Version:      1.0.9
Release:      20150912

#   package options
%option       with_apache  no
%option       with_wsgi    no

#   list of sources
Source0:      http://ftp.edgewall.com/pub/trac/Trac-%{version}.tar.gz
Source1:      trac.wsgi
Source2:      trac.conf
Source3:      rc.trac
Source4:      trac-setup.sh

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  python, python-setup, python-www, python-db, python-db::with_sqlite = yes
PreReq:       python, python-setup, python-www, python-db, python-db::with_sqlite = yes
PreReq:       subversion-python
%if "%{with_apache}" == "yes"
PreReq:       apache
PreReq:       apache::with_mod_proxy = yes
%endif
%if "%{with_wsgi}" == "yes"
PreReq:       apache
PreReq:       apache-wsgi
%endif

%description
    Trac is an enhanced wiki and issue tracking system for software
    development projects. Trac uses a minimalistic approach to web-based
    software project management. Trac provides an interface to VCS, an
    integrated Wiki and convenient reporting facilities. Trac allows
    wiki markup in issue descriptions and commit messages, creating
    links and seamless references between bugs, tasks, changesets, files
    and wiki pages. A timeline shows all project events in order, making
    the acquisition of an overview of the project and tracking progress
    very easy.

%track
    prog trac = {
        version   = %{version}
        url       = http://trac.edgewall.org/wiki/TracDownload
        regex     = Trac-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -n Trac-%{version}

%build

%install
    #   install product
    %{l_prefix}/bin/python setup.py install \
        --root=$RPM_BUILD_ROOT \
        --prefix=%{l_prefix}

    #   install Apache/WSGI configuration
%if "%{with_apache}" == "yes" || "%{with_wsgi}" == "yes"
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/trac \
        $RPM_BUILD_ROOT%{l_prefix}/etc/apache/apache.d
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE trac.wsgi} $RPM_BUILD_ROOT%{l_prefix}/etc/trac/
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE trac.conf} $RPM_BUILD_ROOT%{l_prefix}/etc/trac/
    echo "Include \"%{l_prefix}/etc/trac/trac.conf\"" >trac.conf
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        trac.conf $RPM_BUILD_ROOT%{l_prefix}/etc/apache/apache.d/
%endif

    #   install setup utility
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE trac-setup.sh} $RPM_BUILD_ROOT%{l_prefix}/bin/trac-setup

    #   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.trac} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   install global configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/share/trac/plugins \
        $RPM_BUILD_ROOT%{l_prefix}/share/trac/templates

    #   create runtime directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/trac/env \
        $RPM_BUILD_ROOT%{l_prefix}/var/trac/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/trac/cache \
        $RPM_BUILD_ROOT%{l_prefix}/var/trac/log

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
%if "%{with_apache}" == "yes" || "%{with_wsgi}" == "yes"
        '%config %{l_prefix}/etc/trac/*' \
%endif
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/trac' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/trac/*'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   create sample project environment
        ( echo "Creating 'sample' Trac project environment with"
          echo "authenticating username 'admin' and password 'admin'"
          echo "by running the following command under user %{l_rusr}:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/trac-setup \\ "
          echo "      create sample \"OpenPKG Trac Sample Project\" admin admin"
          echo "You can now find the project environment under:"
          echo "    $RPM_INSTALL_PREFIX/var/trac/env/sample/"
          echo "    http://127.0.0.1:8080/sample/"
        ) | %{l_rpmtool} msg -b -t notice
        su %{l_rusr} -c \
            "$RPM_INSTALL_PREFIX/bin/trac-setup create sample \"OpenPKG Trac Sample Project\" admin admin"
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} trac status 2>/dev/null`
        [ ".$trac_active" = .yes ] && %{l_rc} trac restart
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        #   before erase, stop service and remove created files
        %{l_rc} trac stop 2>/dev/null
        rm -rf $RPM_INSTALL_PREFIX/var/trac/env/sample >/dev/null 2>&1 || true
        rm -rf $RPM_INSTALL_PREFIX/var/trac/log/* >/dev/null 2>&1 || true
        rm -rf $RPM_INSTALL_PREFIX/var/trac/run/* >/dev/null 2>&1 || true
        rm -rf $RPM_INSTALL_PREFIX/var/trac/cache/* >/dev/null 2>&1 || true
    fi
    exit 0

