##
##  amavisd.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_amavis  2.6.6
%define       V_logsumm 0.6

#   package information
Name:         amavisd
Summary:      Interface Daemon between MTA and Content Checker
URL:          http://www.ijs.si/software/amavisd/
Vendor:       Mark Martinec
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        Mail
License:      GPL
Version:      %{V_amavis}
Release:      20110520

#   package options
%option       with_milter no

#   list of sources
Source0:      http://www.ijs.si/software/amavisd/amavisd-new-%{version}.tar.gz
Source1:      http://download.openpkg.org/components/versioned/amavisd/amavislogsumm-%{V_logsumm}
Source2:      rc.amavisd
Patch0:       amavisd.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, sed
PreReq:       OpenPKG, openpkg >= 20160101, spamassassin, file
PreReq:       perl, perl-net, perl-util, perl-mail, perl-time
PreReq:       perl-sys, perl-comp, perl-conv
%if "%{with_milter}" == "yes"
BuildPreReq:  milter
PreReq:       milter
%endif

%description
    amavisd-new is a high-performance interface between mailer (MTA) and
    content checkers: virus scanners, and/or SpamAssassin. It is written
    in Perl for maintainability, without paying a significant price
    for speed. It talks to MTA via (E)SMTP or LMTP, or by using helper
    programs. Best with Postfix, works with Exim, sendmail/milter, or
    with any MTA as a SMTP relay.

%track
    prog amavisd = {
        version   = %{version}
        url       = http://www.ijs.si/software/amavisd/
        regex     = amavisd-new-(\d+\.\d+\.\d+)\.tar\.gz
    }
    prog amavislogsumm = {
        version   = %{V_logsumm}
        url       = http://download.openpkg.org/components/versioned/amavisd/
        regex     = amavislogsumm-(__VER__)
    }

%prep
    %setup -q -n amavisd-new-%{version}
    cp %{SOURCE amavislogsumm-%{V_logsumm}} amavislogsumm
    %patch -p0

%build
    #   adjust configuration file
    %{l_shtool} subst \
        -e "s;'clamscan';'%{l_prefix}/bin/clamscan';g" \
        -e "s;'uvscan';'%{l_prefix}/bin/uvscan';g" \
        -e "s;'sweep';'%{l_prefix}/bin/sweep';g" \
        -e "s;'f-prot';'%{l_prefix}/bin/f-prot';g" \
        -e 's;/var/run/clamav/clamd;%{l_prefix}/var/clamav/clamd.sock;g' \
        -e 's;/var/amavis;%{l_prefix}/var/amavisd;g' \
        -e 's;/var/virusmails;%{l_prefix}/var/amavisd/virusmails;g' \
        -e 's;/usr/bin/sendmail;%{l_prefix}/sbin/sendmail;g' \
        -e 's; -C/etc/sendmail\.orig\.cf;;g' \
        -e 's;/usr/bin/exim;%{l_prefix}/bin/exim;g' \
        -e 's;/usr/local/sbin;%{l_prefix}/sbin;g' \
        -e 's;/usr/local/bin;%{l_prefix}/bin;g' \
        -e 's;\(daemon_user *= \)[^;]*;\1q{%{l_rusr}};' \
        -e 's;\(daemon_group *= \)[^;]*;\1q{%{l_rgrp}};' \
        -e 's;\(\$DO_SYSLOG *= \)[^;]*;\10;' \
        -e '/^\$MYHOME/s!$!\n$LOGFILE = "$MYHOME/amavisd.log";!' \
        -e 's;\(\$path *=.*\)/usr/local/sbin\(.*\);\1%{l_prefix}/sbin:%{l_prefix}/bin:/usr/local/sbin\2;' \
        amavisd.conf

    #   adjust program
    %{l_shtool} subst \
        -e '1s;/usr/bin/perl;%{l_prefix}/bin/perl;' \
        -e 's;/var/amavis;%{l_prefix}/var/amavisd;g' \
        -e 's;/usr/lib/spamassassin;%{l_prefix}/lib/spamassassin;g' \
        amavisd

    #   optionally build MILTER
%if "%{with_milter}" == "yes"
    ( cd helper-progs
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      LDFLAGS="%{l_ldflags}" \
      ./configure \
          --prefix="%{l_prefix}" \
          --with-sendmail="%{l_prefix}/sbin/sendmail" \
          --with-runtime-dir="%{l_prefix}/var/amavisd" \
          --with-sockname="%{l_prefix}/var/amavisd/amavisd.sock" \
          --with-user="%{l_rusr}" \
          --with-milterinc="%{l_prefix}/include/milter" \
          --with-milterlib="%{l_prefix}/lib"
      %{l_make} %{l_mflags} \
          AMAVIS_USER="%{l_rusr}" \
          AMAVIS_GROUP="%{l_mgrp}"
    ) || exit $?
%endif

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/amavisd \
        $RPM_BUILD_ROOT%{l_prefix}/share/amavisd \
        $RPM_BUILD_ROOT%{l_prefix}/var/amavisd \
        $RPM_BUILD_ROOT%{l_prefix}/var/amavisd/db \
        $RPM_BUILD_ROOT%{l_prefix}/var/amavisd/tmp \
        $RPM_BUILD_ROOT%{l_prefix}/var/amavisd/var \
        $RPM_BUILD_ROOT%{l_prefix}/var/amavisd/virusmails

    #   install programs
    %{l_shtool} install -c -m 755 \
        amavisd $RPM_BUILD_ROOT%{l_prefix}/sbin/
    %{l_shtool} install -c -m 755 \
        -e 's;/usr/bin/perl;%{l_prefix}/bin/perl;g' \
        amavislogsumm $RPM_BUILD_ROOT%{l_prefix}/sbin/
%if "%{with_milter}" == "yes"
    %{l_shtool} install -c -s -m 755 \
        helper-progs/amavis-milter $RPM_BUILD_ROOT%{l_prefix}/sbin/
%endif

    #   install default configuration
    %{l_shtool} install -c -m 644 \
        amavisd.conf $RPM_BUILD_ROOT%{l_prefix}/etc/amavisd/

    #   install documentation
    %{l_shtool} install -c -m 644 \
        README_FILES/amavisd-new-docs.html \
        $RPM_BUILD_ROOT%{l_prefix}/share/amavisd/amavisd.html

    #   install run-command script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.amavisd} $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/amavisd/amavisd.conf' \
        '%doc %{l_prefix}/share/amavisd/*' \
        '%attr(0750,%{l_rusr},%{l_mgrp}) %{l_prefix}/var/amavisd' \
        '%attr(0750,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/amavisd/*'

%files -f files

%clean

%pre
    #   before upgrade, save status and stop service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} amavisd status 2>/dev/null | tee %{l_tmpfile}`
    %{l_rc} amavisd stop 2>/dev/null
    exit 0

%post
    [ $1 -eq 2 ] || exit 0
    #   after upgrade, restore status
    eval `cat %{l_tmpfile}`; rm -f %{l_tmpfile}
    [ ".$amavisd_active" = .yes ] && %{l_rc} amavisd start
    exit 0

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

