# You can use these rpmbuild parameters:
# --with dspam		to build with pydspam support
# --without clamav	to build without libclamav support

%define CHROOTDIR /var/spool/vscan
%define BASE_LIBS glibc,libgcc,expat,libstdc++,zlib,bzip2-libs
%define ARCHIVERS tar,arc,unace,unrar,rar,zoo,unarj,arj,unzip,zip,gzip,bzip2
%define ANTIVIRS clamav,clamav-libs,avglinux,nod32ls,nod32lfs,kav4mailservers-linux
%define ANTISPAMS bogofilter,qsf

Summary:   SAGATOR - antivir/antispam gateway for smtp server
Name:      sagator
Version:   0.8.1
Release:   1%{?dist}
Source:    http://www.salstar.sk/pub/antivir/snapshots/sagator-%{version}.tar.bz2
URL:       http://www.salstar.sk/pub/antivir/sagator/
License:   GPL
Group:     System Environment/Daemons
#BuildArch: noarch
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
%if %_vendor == "suse"
Requires:  python >= 2.2.2, aaa_base, smtp_daemon
BuildRequires: python-devel >= 2.2.2, aaa_base, ed
%else
Requires:  python >= 2.2.2, initscripts, smtpdaemon
BuildRequires: python-devel >= 2.2.2, initscripts, ed
%endif

%description
This program is an email antivirus/antispam gateway. It is an interface to
the postfix, sendmail, or any other smtpd, which runs antivirus and/or
spamchecker. Its modular architecture can use any combination of
antivirus/spamchecker according to configuration.

It has some internal checkers (string_scanner and regexp_scanner). Sagator
can parse MIME mails and decompress archives, if it is configured so.

Features:
    * simple chroot support
    * modular antivirus/spamchecker support
    * you don't need any perl modules or any other modules, only python
    * you can return any quarantined mail to mailq/user mailbox
    * mailbox/maildir scanning and mailbox cleaning
    * nice statistics via WWW or MRTG
    * easy installation

%if %{!?_without_clamav:1}%{?_without_clamav:0}
%package libclamav
Summary:   ClamAV native support via libclamav
Group:     Libraries
#BuildArch: i386
Requires:  %{name}
%if %_vendor == "suse"
Requires:	clamav >= 0.88.7
BuildRequires:	clamav
%else
Requires:	clamav-lib >= 0.88.7
BuildRequires:	clamav-devel
%endif

%description libclamav
This is a ClamAV native support via libclamav.
%endif

%if %{!?_with_dspam:0}%{?_with_dspam:1}
%package pydspam
Summary: DSPAM python module for sagator
Group: Libraries
#BuildArch: i386
Requires: %{name}

%description pydspam
pydspam.so is an library (module) for python to use DSPAM antispam.
It is designed for sagator.
%endif

%prep
%setup -q -n %{name}

%build
./configure --prefix %{_prefix} --libdir %{_libdir} --filelist
make

%install
make DESTDIR=%{buildroot} PREFIX=%{_prefix} install
rm -f %{buildroot}%{_libdir}/sagator/config/sgconf.py[co]
mkdir -p %{buildroot}/var/spool/vscan/tmp/quarantine

%pre
id vscan > /dev/null 2>&1 || adduser -r -M vscan

%post
#exec &>/dev/null
# Adding postfix config
if [ $1 = 2 ]; then # upgrade
  [ -f /etc/sysconfig/sagator ] && . /etc/sysconfig/sagator || true
  # update configuration
  %{_libdir}/sagator/updatecfg.py || true
  if [ "$RESTART" = "auto" ]; then
    # restart sagator
    /etc/init.d/sagator condrestart || true
  fi
else # install
  if [ -x /sbin/chkconfig ]; then
    chkconfig --add sagator
  fi
  mkdir -p %{CHROOTDIR}
  /usr/sbin/mkchroot.sagator --auto
  if grep -q "^#begin: sagator" /etc/postfix/main.cf >/dev/null; then
    echo "Postfix main configuration already done."
  else
    echo -e \
      "#begin: sagator\ncontent_filter=smtp:[127.0.0.1]:27\n#end: sagator\n" \
      >> /etc/postfix/main.cf
  fi
  if grep -q "^#begin: sagator" /etc/postfix/master.cf >/dev/null; then
    echo "Postfix master configuration already done."
  else
    cat >> /etc/postfix/master.cf << EOF
#begin: sagator
127.0.0.1:26	inet n - n - 30 smtpd
  -o content_filter=
  -o myhostname=sagator.`hostname`
  -o local_recipient_maps=  -o relay_recipient_maps=
  -o mynetworks=127.0.0.0/8  -o mynetworks_style=host
  -o smtpd_restriction_classes=  -o smtpd_client_restrictions=
  -o smtpd_helo_restrictions=  -o smtpd_sender_restrictions=
  -o smtpd_recipient_restrictions=permit_mynetworks,reject
  -o receive_override_options=no_unknown_recipient_checks,no_header_body_checks
#end: sagator
EOF
  fi
  # reload postfix
  /etc/init.d/postfix reload || true
  # start sagator
  /etc/init.d/sagator start || true
fi

%preun
#exec &>/dev/null
if [ $1 = 0 ]; then # uninstall
  [ "%{CHROOTDIR}" != "/" ] && rm -rf %{CHROOTDIR}/*
  if [ "$1" = 0 ] ; then
    if [ -x /sbin/chkconfig ]; then
      chkconfig --del sagator
    fi
  fi
  # Removing postfix config
  mv /etc/postfix/main.cf /etc/postfix/main.cf.tmp
  sed '/^#begin: sagator/,/^#end: sagator/d' \
    < /etc/postfix/main.cf.tmp > /etc/postfix/main.cf
  mv /etc/postfix/master.cf /etc/postfix/master.cf.tmp
  sed '/^#begin: sagator/,/^#end: sagator/d' \
    < /etc/postfix/master.cf.tmp > /etc/postfix/master.cf
  /etc/init.d/postfix reload || true
  # Remove mrtg.config
  cp -f /etc/crontab /etc/crontab.tmp && \
    sed '/^#begin: sagator/,/^#end: sagator/d' \
    < /etc/crontab.tmp > /etc/crontab
  rm -f /etc/crontab.tmp
fi

%triggerin -- mrtg
# add sagator's mrtg configuration
[ ! -d '/var/www/mrtg/sagator' ] && mkdir '/var/www/mrtg/sagator'
if [ ! -f /var/www/mrtg/sagator/index.html ]; then
  cat /usr/share/doc/sagator-%{version}/mrtg-index.html \
    | sed 's/statistics</statistics for '`hostname`'</g' \
    > /var/www/mrtg/sagator/index.html
  echo 'You will be able to see your stats at http://'`hostname`'/mrtg/sagator/'
fi
if grep -q "^#begin: sagator" /etc/crontab >/dev/null; then
  echo "Crontab configuration already done."
else
cat << EOF >> /etc/crontab
#begin: sagator
0-59/5 * * * * root /usr/bin/mrtg /etc/mrtg/sagator.cfg
#end: sagator
EOF
fi

%triggerun -- mrtg
# Remove mrtg.config
cp -f /etc/crontab /etc/crontab.tmp && \
  sed '/^#begin: sagator/,/^#end: sagator/d' \
  < /etc/crontab.tmp > /etc/crontab
rm -f /etc/crontab.tmp

%triggerin -- %{BASE_LIBS},%{ARCHIVERS},%{ANTIVIRS},%{ANTISPAMS}
/usr/sbin/mkchroot.sagator --auto --quiet || true

#%triggerpostun -- %{BASE_LIBS},%{ARCHIVERS},%{ANTIVIRS},%{ANTISPAMS}
#/usr/sbin/mkchroot.sagator --auto || true

%clean
rm -rf %{buildroot}

%files -f filelist
%defattr(-,root,root)
%config(noreplace) %verify(not md5 size mtime) %attr(600,root,vscan) %{_sysconfdir}/sagator.conf
%config(noreplace) %verify(not md5 size mtime) %{_sysconfdir}/sysconfig/sagator
%config(noreplace) %verify(not md5 size mtime) %{_sysconfdir}/logrotate.d/sagator
%config(noreplace) %verify(not md5 size mtime) %{_sysconfdir}/httpd/conf.d/sagator.conf
%config(noreplace) %verify(not md5 size mtime) %{_sysconfdir}/mrtg/sagator.cfg
%dir /var/www/mrtg/sagator
%config(noreplace) %verify(not md5 size mtime) /var/www/mrtg/sagator/index.html
%{_sysconfdir}/init.d/*
%config(noreplace) %verify(not md5 size mtime) %attr(755,root,root) %{_sysconfdir}/cron.daily/sagator
%doc doc/README doc/FAQ doc/*.txt doc/*.html config/sagator.conf-template
%doc TODO COPYING ChangeLog scripts test
%{_bindir}/*
%{_sbindir}/*
%dir %{_libdir}/sagator
%{_libdir}/sagator/*.py*
%dir %attr(750,root,vscan) %{_libdir}/sagator/config
%{_libdir}/sagator/config/*.py*
%dir %{_libdir}/sagator/av
%{_libdir}/sagator/av/*.py*
%dir %{_libdir}/sagator/as
%{_libdir}/sagator/as/*.py*
%dir %{_libdir}/sagator/interscan
%{_libdir}/sagator/interscan/*.py*
%dir %{_libdir}/sagator/srv
%{_libdir}/sagator/srv/*.py*
%dir %{_libdir}/sagator/srv/web
%{_libdir}/sagator/srv/web/.htaccess
%{_libdir}/sagator/srv/web/*.py*
%{_mandir}/man*/*
%{_datadir}/locale/*/LC_MESSAGES/sagator.mo
%dir /var/spool/vscan
%attr(1777,vscan,vscan) %dir /var/spool/vscan/tmp
%attr(0770,vscan,vscan) %dir /var/spool/vscan/tmp/quarantine
%if %(test -d /etc/yum.repos.d && echo 1 || echo 0)
%config(noreplace) %verify(not md5 size mtime) /etc/yum.repos.d/sagator.repo
%endif

%if %{!?_without_clamav:1}%{?_without_clamav:0}
%files libclamav
%defattr(-,root,root)
%{_libdir}/sagator/libclamav.so
%endif

%if %{!?_with_dspam:0}%{?_with_dspam:1}
%files pydspam
%defattr(-,root,root)
%{_libdir}/sagator/pydspam.so
%endif

%changelog
* Sun Nov 20 2005 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- suse support
- proper pathes for logwatch 0.7 scripts

* Sun Aug 28 2005 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- configuration moved into config directory

* Fri Aug 05 2005 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- script documentation added

* Mon Jul 18 2005 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- new description
- changed dependecy from postfix to smtpdaemon

* Sun Jun 26 2005 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- changes recommended by Fedora Extras Packaging Guidelines

* Mon May 30 2005 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- web directory added

* Mon Apr 11 2005 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- postinstall script fix to update master.cf properly

* Mon Dec 20 2004 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- Documentation moved into doc directory.
- man pages added
- dspam module

* Sun Nov  7 2004 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- chkconfig is started only on install (not upgrade)
- sagator is restarted only when RESTART=auto is configured in sysconfig
- You will be able ... message will show only on install
- Fedora Core 3 yum.repos.d autodetect

* Thu Aug 26 2004 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- logrotate script added

* Thu Aug  6 2004 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- srv/* added
- updatecfg.py in postinstall script

* Tue Jul  6 2004 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- mrtg script added

* Mon May 31 2004 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- cron script is now as configuration script (not replaced)

* Thu Apr 29 2004 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- added triggers

* Tue Apr 13 2004 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- /var/spool/vscam/tmp permission fix

* Wed Feb  4 2004 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- postfix requirement added
- fixed scriptlet fail when postfix is not started
- testing programs added to documentation

* Wed Jan 28 2004 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- configs are not replaced

* Mon Jan 19 2004 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- added sysconfig file
- added logrotate files
- config moved into /etc and symlink into /usr/lib/sagator

* Fri Oct 31 2003 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- added mrtg.cfg and index.html
- making of chroot removed on upgrading

* Thu Sep 18 2003 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- postfix is not reloaded, when sagator is upgrading

* Tue Jun 10 2003 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>
- first release
