##
##  petidomo.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:         petidomo
Summary:      Mailing List Server
URL:          http://www.ossp.org/pkg/tool/petidomo/
Vendor:       Peter Simons
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Mail
License:      GPL
Version:      4.0b6
Release:      20181229

#   package options
%option       with_fsl  yes

#   list of sources
Source0:      http://www.ossp.org/pkg/tool/petidomo/petidomo-%{version}.tar.gz
Source1:      petidomo.txt
Source2:      fsl.petidomo
Source3:      rc.petidomo
Patch0:       petidomo.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, bison, flex, perl
PreReq:       OpenPKG, openpkg >= 20160101, MTA
%if "%{with_fsl}" == "yes"
BuildPreReq:  fsl
PreReq:       fsl
%endif

%description
    Petidomo is a mailing list manager very similar to the popular
    package Majordomo. It handles both list un-/subscription and the
    address expansion of list postings. The actual delivery of postings
    is performed by an MTA like Sendmail.

%track
    prog petidomo = {
        version   = %{version}
        url       = http://www.ossp.org/pkg/tool/petidomo/
        regex     = petidomo-(__VER__)\.tar\.gz
    }

%prep
    %setup -q
    %patch -p0
    for i in . ./etc/shtool; do cp %{l_shtool} $i; done

%build
    #   build program
    %{l_shtool} subst \
        -e 's;\./petidomo --masterconf=.*/petidomo\.conf;%{l_prefix}/bin/petidomo --masterconf=%{l_prefix}/etc/petidomo/petidomo.conf;g' \
        petidomo-kickout
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags} %{l_fsl_ldflags}" \
    LIBS="%{l_fsl_libs}" \
    LEX="flex" \
    YACC="bison -y" \
    ./configure \
        --prefix=%{l_prefix} \
        --sysconfdir=%{l_prefix}/etc/petidomo
    %{l_make} %{l_mflags}

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

    #   strip down installation
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/libexec/petidomo
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/etc/petidomo
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/var/petidomo
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/petidomo \
        $RPM_BUILD_ROOT%{l_prefix}/etc/petidomo

    #   install additional files
    %{l_shtool} install -c -m 644 \
        /dev/null $RPM_BUILD_ROOT%{l_prefix}/var/petidomo/petidomo.log
    l_hostname=`%{l_shtool} echo -e '%h%d'`
    for name in `grep "^<file" %{SOURCE petidomo.txt} | sed -e 's;^.*name=";;' -e 's;".*$;;'`; do
        (echo ""; cat %{SOURCE petidomo.txt}; echo "") |\
            sed -e "1,/^<file name=\"$name\">/d" -e "/<\/file>/,\$d" >$name
        %{l_shtool} install -c -m 644 %{l_value -s -a} \
            -e "s;@l_hostname@;${l_hostname};g" \
            $name $RPM_BUILD_ROOT%{l_prefix}/etc/petidomo/
    done

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

    #   install OSSP fsl configuration
    %{l_shtool} mkdir -f -p -m 755 $RPM_BUILD_ROOT%{l_prefix}/etc/fsl
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE fsl.petidomo} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/fsl/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/fsl/fsl.petidomo' \
        '%config %{l_prefix}/etc/petidomo/*' \
        '%attr(775,%{l_musr},%{l_rgrp}) %{l_prefix}/var/petidomo/*' \
        '%attr(6755,%{l_musr},%{l_mgrp}) %{l_prefix}/bin/petidomo'

%files -f files

%clean

%post
    #   add hook into MTA configuration
    if [ ".$1" = .1 ]; then
        aliases_file=`$RPM_INSTALL_PREFIX/bin/openpkg rc --query MTA_aliases_file`
        update_command=`$RPM_INSTALL_PREFIX/bin/openpkg rc --query MTA_aliases_update`
        if [ ".$aliases_file" != . ]; then
            ( echo "petidomo:              \"|$RPM_INSTALL_PREFIX/bin/petidomo --mode=listserv\""
              echo "petidomo-approve:      \"|$RPM_INSTALL_PREFIX/bin/petidomo --mode=approve\""
              echo "petidomo-manager:      postmaster"
              echo "owner-owner:           petidomo-manager"
              echo "users:                 \"|$RPM_INSTALL_PREFIX/bin/petidomo --mode=deliver  --listname=users\""
              echo "users-request:         \"|$RPM_INSTALL_PREFIX/bin/petidomo --mode=listserv --listname=users\""
              echo "users-owner:           postmaster"
              echo "owner-users:           users-owner"
            ) |\
            $RPM_INSTALL_PREFIX/lib/openpkg/rpmtool config \
                -a -i "$RPM_INSTALL_PREFIX:petidomo" $aliases_file
        fi
        if [ ".$update_command" != . ]; then
            eval $update_command
        fi
    fi

%postun
    #   remove hook from MTA configuration
    if [ ".$1" = .0 ]; then
        aliases_file=`$RPM_INSTALL_PREFIX/bin/openpkg rc --query MTA_aliases_file`
        update_command=`$RPM_INSTALL_PREFIX/bin/openpkg rc --query MTA_aliases_update`
        if [ ".$aliases_file" != . ]; then
            $RPM_INSTALL_PREFIX/lib/openpkg/rpmtool config \
                -r -i "$RPM_INSTALL_PREFIX:petidomo" $aliases_file
        fi
        if [ ".$update_command" != . ]; then
            eval $update_command
        fi
    fi

