##
##  viewmtn.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:         viewmtn
Summary:      Monotone DVCS Web Viewer
URL:          http://grahame.angrygoats.net/moinmoin/ViewMTN
Vendor:       Grahame Bowland
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        SCM
License:      GPL
Version:      0.07
Release:      20080101

#   list of sources
Source0:      http://grahame.angrygoats.net/source/viewmtn/viewmtn-%{version}.tgz
Source1:      viewmtn.config.py
Source2:      rc.viewmtn
Patch0:       viewmtn.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       python, python-net, python-www
PreReq:       shared-mime-info, icon-theme
PreReq:       highlight, graphviz, fonts-free
PreReq:       monotone, diffutils

%description
    ViewMTN is a web interface to the Monotone distributed version
    control system.

%track
    prog viewmtn = {
        version   = %{version}
        url       = http://grahame.angrygoats.net/source/viewmtn/
        regex     = viewmtn-(__VER__)\.t(?:gz|ar\.gz)
    }

%prep
    %setup -q
    %patch -p0

%build
    #   remove files we do not want to install
    rm -rf _MTN
    rm -f [A-Z]*

    #   adjust paths to Python interpreter
    %{l_shtool} subst \
        -e 's;#!/usr/bin/env python2.4;#!%{l_prefix}/bin/python;g' \
        viewmtn.py
    %{l_shtool} subst \
        -e 's;#!/usr/bin/python;#!%{l_prefix}/bin/python;g' \
        experiments/grapher/grapher.py
    %{l_shtool} subst \
        -e 's;/usr/local/share:/usr/share;%{l_prefix}/share/icon-theme:%{l_prefix}/share/mime;g' \
        fdo/xdgbasedir.py

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

    #   install program files
    rm -f config.py.example
    find . -name "*.orig" -print | xargs rm -f
    cp -rp * $RPM_BUILD_ROOT%{l_prefix}/libexec/viewmtn/

    #   move templates to final location
    mv  $RPM_BUILD_ROOT%{l_prefix}/libexec/viewmtn/templates \
        $RPM_BUILD_ROOT%{l_prefix}/etc/viewmtn/templates
    ln -s ../../etc/viewmtn/templates \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/viewmtn/templates

    #   install default configuration
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE viewmtn.config.py} $RPM_BUILD_ROOT%{l_prefix}/etc/viewmtn/
    ln -s ../../etc/viewmtn/viewmtn.config.py \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/viewmtn/config.py

    #   make sure the graphs can be generated
    ln -s ../../var/viewmtn/graph \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/viewmtn/graph

    #   install run-command script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.viewmtn} $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/viewmtn/*' \
        '%config %{l_prefix}/etc/viewmtn/templates/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/viewmtn/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/viewmtn'

%files -f files

%clean

%post
    #   after upgrade, restart service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} viewmtn status 2>/dev/null`
    [ ".$viewmtn_active" = .yes ] && %{l_rc} viewmtn restart
    exit 0

%preun
    #   before erase, stop service and remove log files
    [ $1 -eq 0 ] || exit 0
    %{l_rc} viewmtn stop 2>/dev/null
    rm -f $RPM_INSTALL_PREFIX/var/viewmtn/viewmtn.log* >/dev/null 2>&1 || true
    exit 0

