##
##  monotone.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:         monotone
Summary:      Distributed Version Control System
URL:          http://monotone.ca/
Vendor:       Graydon Hoare et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        SCM
License:      GPL
Version:      1.1
Release:      20180327

#   list of sources
Source0:      http://monotone.ca/downloads/%{version}/monotone-%{version}.tar.bz2
Source1:      rc.monotone
Source3:      monotone-setup.sh
Source4:      monotone-colorize.pl
Source5:      monotone-colorize.bashrc
Source6:      monotone.man

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc, gcc::with_cxx = yes, texinfo, pkgconfig
PreReq:       OpenPKG, openpkg >= 20160101, apg, diffutils, perl, perl-mail, perl-term, less
BuildPreReq:  boost, botan1, sqlite, zlib, pcre, lua, libidn
PreReq:       boost, botan1, sqlite, zlib, pcre, lua, libidn

%description
    Monotone is a distributed version control system. It provides
    a simple, single-file RDBMS based transactional version store,
    with fully disconnected operation and an efficient peer-to-peer
    synchronization protocol. It understands history-sensitive merging,
    lightweight branches, integrated code review and third-party
    testing. It uses cryptographic version naming and client-side RSA
    certificates.

%track
    prog monotone = {
        version   = %{version}
        url       = http://monotone.ca/downloads/
        regex     = "(\d+\.\d+)/"
    }

%prep
    %setup -q

%build
    #   configure program
    %{l_shtool} subst \
        -e 's;-Wall -W -Wno-unused;;' \
        -e 's;-ansi -pedantic;;' \
        configure
    libs=""
    case "%{l_platform -t}" in
        *-linux* ) libs="$libs -ldl" ;;
    esac
    CC="%{l_cc}" \
    CXX="%{l_cxx}" \
    CFLAGS="%{l_cflags -O}" \
    CXXFLAGS="%{l_cxxflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags}" \
    LIBS="$libs" \
    ./configure \
        --prefix=%{l_prefix} \
        --infodir=%{l_prefix}/info \
        --mandir=%{l_prefix}/man \
        --disable-nls

    #   build program
    %{l_make} %{l_mflags -O}

%install
    #   install program
    %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"

    #   strip down installation
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    mv $RPM_BUILD_ROOT%{l_prefix}/share/doc/monotone \
       $RPM_BUILD_ROOT%{l_prefix}/share/monotone
    rm -f $RPM_BUILD_ROOT%{l_prefix}/info/dir
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/locale
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/doc

    #   create additional directories for server operation
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/monotone \
        $RPM_BUILD_ROOT%{l_prefix}/var/monotone

    #   install contrib tools
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE monotone-colorize.bashrc} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/monotone/
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE monotone-colorize.pl} \
        $RPM_BUILD_ROOT%{l_prefix}/bin/monotone-colorize

    #   install run-command script
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/man/man1
    %{l_shtool} install -c -m 644 \
        %{SOURCE monotone.man} $RPM_BUILD_ROOT%{l_prefix}/man/man1/mtn.1

    #   install setup script
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/sbin
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e 's;@l_shtool@;%{l_shtool};g' \
        %{SOURCE monotone-setup.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/monotone-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.monotone} \
        $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/monotone/*' \
        '%doc %{l_prefix}/share/monotone/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/monotone'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display initial hint
        ( echo "If you want to use the Monotone daemon for serving"
          echo "a central permanently available database, run:"
          echo "    $RPM_INSTALL_PREFIX/sbin/monotone-setup install \\ "
          echo "        [<server-mail-address> [<server-password>]]"
          echo "and put into your $RPM_INSTALL_PREFIX/etc/rc.conf at least:"
          echo "    monotone_daemon=\"yes\""
          echo "    monotone_daemon_host=\"<server-ip-address>\""
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} monotone status 2>/dev/null`
        [ ".$monotone_active" = .yes ] && %{l_rc} monotone restart
    fi
    exit 0

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

