# http://trac.vinelinux.org/wiki/WellKnownUigGid
%define         registry_uid  38
%define         registry_name dbmail

%define         services dbmail-imapd dbmail-pop3d dbmail-lmtpd dbmail-timsieved

Name:           dbmail
Version:        2.2.18
Release:        1%{?_dist_release}
Summary:        A database backend mail storage system
Summary(ja):    メールストレージシステムのデータベースバックエンド

Group:          System Environment/Daemons
# db_getopot.c is licensed MIT
License:        GPLv2+ and MIT
URL:            http://www.dbmail.org
Source0:        http://www.dbmail.org/download/2.2/dbmail-%{version}.tar.gz
Source1:        dbmail-imapd
Source2:        dbmail-pop3d
Source3:        dbmail-lmtpd
Source4:        dbmail-timsieved
Source5:        dbmail.cron
Source6:        dbmail.logrotate
Source7:        README.dbmail
Source10:	find-provides-dbmail.sh

Patch0:         0001-backport-fix-to-prevent-showing-mailboxes-more-than.patch

BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root

BuildRequires:  gmime22-devel >= 2.2.0

BuildRequires:  fileutils, openssl-devel >= 0.9.7a
BuildRequires:  glib2-devel >= 2.8
BuildRequires:  libsieve-devel >= 2.1.13
BuildRequires:  mysql-devel >= 4.1.3, postgresql-devel
BuildRequires:  openldap-devel
BuildRequires:  sqlite3-devel
BuildRequires:  zlib-devel

Requires:       glib2
Requires:       initscripts
Requires:       /usr/sbin/sendmail
Requires(pre):  shadow-utils
Requires(postun):shadow-utils
Requires:       sqlite3
Obsoletes:      dbmail-sqlite < 2.2.5
Provides:       dbmail-sqlite = %{version}


%description
Dbmail is the name of a group of programs that enable the possiblilty of
storing and retrieving mail messages from a database.

Currently dbmail supports the following database backends:
MySQL
PostgreSQL
SQLite

Please see /usr/share/doc/dbmail-*/README.dbmail for specific information on
installation and configuration.

%prep
%setup -q

## %patch0 -p 1 -b .duplicate-email-boxes

# we don't need README.solaris and we don't want it caught up in the %%doc
# README* wildcard - but we do want our shiny new README.dbmail file to be
# installed
rm -f README.solaris
install -p -m 644 %SOURCE7 .

# make a couple of changes to the default dbmail.conf file:
# 1. default driver/authdriver sqlite/sql (sqlite, if supported)
# 2. effective uid/gid to dbmail/dbmail
sed -i 's/\(^driver\W*=\)\(\W*$\)/\1 sqlite/' dbmail.conf
sed -i -e 's,\(^db\W*=\)\(.*$\),\1 %{_localstatedir}/lib/dbmail/dbmail.db,'   \
       -e 's/\(^authdriver\W*=\)\(\W*$\)/\1 sql/'                             \
       -e 's/\(^EFFECTIVE_USER\W*=\)\(.*$\)/\1 dbmail/'                       \
       -e 's/\(^EFFECTIVE_GROUP\W*=\)\(.*$\)/\1 dbmail/' dbmail.conf

%define _use_internal_dependency_generator 0
%define __find_provides %{SOURCE10}
chmod +x %{__find_provides}

%build
%configure --disable-rpath \
           --disable-static \
           --with-ldap \
           --with-mysql \
           --with-pgsql \
           --with-sqlite \
           --with-sieve

make %{?_smp_mflags}

%install
rm -rf $RPM_BUILD_ROOT
make install DESTDIR=$RPM_BUILD_ROOT
mkdir -p $RPM_BUILD_ROOT/%{_mandir}/man{1,5,8}
mkdir -p $RPM_BUILD_ROOT/%{_initrddir}
mkdir -p $RPM_BUILD_ROOT/%{_sysconfdir}/cron.daily
mkdir -p $RPM_BUILD_ROOT/%{_sysconfdir}/logrotate.d
mkdir -p $RPM_BUILD_ROOT/%{_localstatedir}/lib/dbmail
install -p -m 755 %SOURCE1 $RPM_BUILD_ROOT/%{_initrddir}
install -p -m 755 %SOURCE2 $RPM_BUILD_ROOT/%{_initrddir}
install -p -m 755 %SOURCE3 $RPM_BUILD_ROOT/%{_initrddir}
install -p -m 755 %SOURCE4 $RPM_BUILD_ROOT/%{_initrddir}
install -p -m 755 %SOURCE5 $RPM_BUILD_ROOT/%{_sysconfdir}/cron.daily/dbmail
install -p -m 644 %SOURCE6 $RPM_BUILD_ROOT/%{_sysconfdir}/logrotate.d/dbmail
install -p -m 600 dbmail.conf $RPM_BUILD_ROOT/%{_sysconfdir}/
install -p -m 644 man/*1 $RPM_BUILD_ROOT/%{_mandir}/man1/
install -p -m 644 man/*5 $RPM_BUILD_ROOT/%{_mandir}/man5/
install -p -m 644 man/*8 $RPM_BUILD_ROOT/%{_mandir}/man8/
# remove libtool archives and -devel type stuff (but leave loadable modules)
find $RPM_BUILD_ROOT -name \*\.la -print | xargs rm -f
rm -f $RPM_BUILD_ROOT/%{_libdir}/dbmail/libdbmail.so

%clean
rm -rf $RPM_BUILD_ROOT

%pre
/usr/sbin/groupadd -g %registry_uid -r %registry_name &> /dev/null || :
/usr/sbin/useradd -u %registry_uid -r -s /sbin/nologin -d / -M           \
                  -c "DBMail Daemon Account" -g %registry_name %registry_name    \
                  &> /dev/null || :

%post
for s in %services; do
  /sbin/chkconfig --add $s &> /dev/null || :
done
/sbin/ldconfig

%preun
if [ $1 = 0 ]; then
  for s in %services; do
    /sbin/service $s stop &> /dev/null || : 
    /sbin/chkconfig --del $s &> /dev/null || :
  done
fi
exit 0

%postun
if [ "$1" -eq "0" ]; then
  /sbin/ldconfig
  /usr/sbin/userdel  %registry_name &> /dev/null || :
  /usr/sbin/groupdel %registry_name &> /dev/null || :
elif [ "$1" -ge "1" ]; then
  for s in %services; do
    /sbin/service $s condrestart &> /dev/null || : 
  done
fi	

%files
%defattr(-,root,root,-)
%doc AUTHORS BUGS ChangeLog COPYING INSTALL README* THANKS
%{_sbindir}/*
%{_mandir}/man1/*
%{_mandir}/man5/*
%{_mandir}/man8/*
%dir %{_libdir}/dbmail
%{_libdir}/dbmail/libauth_sql*
%{_libdir}/dbmail/libdbmail*
%{_libdir}/dbmail/libsort_sieve*
%config(noreplace) %{_sysconfdir}/dbmail.conf
%{_initrddir}/dbmail-*
%config(noreplace) %{_sysconfdir}/cron.daily/dbmail
%config(noreplace) %{_sysconfdir}/logrotate.d/dbmail
%doc sql/sqlite/*
%{_libdir}/dbmail/libsqlite*
%dir %attr(0775,root,dbmail) /var/lib/dbmail

%package auth-ldap
Summary:        A database backed mail storage system - ldap authentication plugin
Summary(ja):        A database backed mail storage system - ldap authentication plugin
Group:          System Environment/Daemons
Requires:       dbmail = %{version}-%{release}, openldap

%description auth-ldap
This is the auth-ldap libraries for authentication against a ldap server with
dbmail.

%files auth-ldap
%defattr(-,root,root,-)
%attr(-,root,root) %{_libdir}/dbmail/libauth_ldap*

%package mysql
Summary:        A database backed mail storage system - mysql backend
Summary(ja):        A database backed mail storage system - mysql backend
Group:          System Environment/Daemons
Requires:       dbmail = %{version}-%{release}, mysql-server

%description mysql
This is the mysql libraries for dbmail.

%files mysql
%defattr(-,root,root,-)
%doc sql/mysql/*
%{_libdir}/dbmail/libmysql*

%package pgsql
Summary:        A database backed mail storage system - postgresql backend
Summary(ja):        A database backed mail storage system - postgresql backend
Group:          System Environment/Daemons
Requires:       dbmail = %{version}-%{release}, postgresql-server

%description pgsql
This is the postgresql libraries for dbmail.

%files pgsql
%defattr(-,root,root,-)
%doc sql/postgresql/*
%{_libdir}/dbmail/libpgsql*

%changelog
* Sat Jun 16 2012 Yoji TOYODA <bsyamato@sea.plala.or.jp> 2.2.18-1
- update to 2.2.18

* Sun Apr 11 2010 Yoji TOYODA <bsyamato@sea.plala.or.jp> 2.2.15-1
- new upstream release
- add BuildRequires: zlib-devel
- change BuildRequires: gmime-devel -> gmime22-devel
- drop Patch0

* Mon Sep 14 2009 Daisuke SUZUKI <daisuke@linux.or.jp> 2.2.11-1
- initial build for Vine Linux

* Fri Aug 21 2009 Tomas Mraz <tmraz@redhat.com> - 2.2.11-9
- rebuilt with new openssl

* Fri Jul 24 2009 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 2.2.11-8
- Rebuilt for https://fedoraproject.org/wiki/Fedora_12_Mass_Rebuild

* Mon Jul 06 2009 Bernard Johnson <bjohnson@symetrix.com> - 2.2.11-7
- fix left out ? in comparison

* Sun Jul 05 2009 Bernard Johnson <bjohnson@symetrix.com> - 2.2.11-6
- fix conditional comparison to be 0 for no value

* Sun Jul 05 2009 Bernard Johnson <bjohnson@symetrix.com> - 2.2.11-5
- patch to remove duplicate email boxes from being listed
- consider cron file a config file
- add -b option to cron job to rebuild body/header/envelope cache tables
- change order of redirection in cron job
- fix typo in dbmail-pop3d that causes LSB info to not be recognized
- add provides for dbmail-sqlite
- conditional to compile with gmime22 when needed

* Thu Mar 19 2009 Bernard Johnson <bjohnson@symetrix.com> - 2.2.11-4
- build agaist old gmime22 (bz #490316)

* Tue Feb 24 2009 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 2.2.11-3
- Rebuilt for https://fedoraproject.org/wiki/Fedora_11_Mass_Rebuild

* Thu Feb 05 2009 Bernard Johnson <bjohnson@symetrix.com> - 2.2.11-2
- change BR from openssl to openssl-devel

* Tue Feb 03 2009 Bernard Johnson <bjohnson@symetrix.com> - 2.2.11-1
- v 2.2.11
- updated summaries
- fix bug in dbmail-pop3d init script

* Mon Jul  7 2008 Tom "spot" Callaway <tcallawa@redhat.com> - 2.2.9-2
- fix conditional comparison

* Thu Apr 24 2008 Bernard Johnson <bjohnson@symetrix.com> - 2.2.9-1
- v 2.2.9

* Mon Feb 18 2008 Fedora Release Engineering <rel-eng@fedoraproject.org> - 2.2.8-2
- Autorebuild for GCC 4.3

* Fri Jan 18 2008 Bernard Johnson <bjohnson@symetrix.com> - 2.2.8-1
- 2.2.8-1

* Thu Dec 06 2007 Release Engineering <rel-eng at fedoraproject dot org> - 2.2.7-2
 - Rebuild for deps

* Wed Oct 31 2007 Bernard Johnson <bjohnson@symetrix.com> - 2.2.7-1
- 2.2.7-1
- removed unused thread references patch
- removed unused hup patch
- removed unused gmime segv patch
- license clarification
- dbmail: Initscript Review (bz #246901)

* Tue Aug 28 2007 Fedora Release Engineering <rel-eng at fedoraproject dot org> - 2.2.5-7
- Rebuild for selinux ppc32 issue.

* Tue Jul 03 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.5-6
- patch to fix SEGV in dbmail-imapd

* Sat Jun 23 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.5-5
- patch to reopen logs files on -HUP
- patch to send error when thread references requested
- don't filter libdbmail.so*

* Sat Jun 23 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.5-4
- kill ld.so config
- filter private libraries from provides (bz#245326)

* Wed Jun 20 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.5-3
- assign uid from package user registry (bz#244611)

* Tue Jun 05 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.5-2
- fix %%setup directory

* Tue Jun 05 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.5-1
- 2.2.5
- change method of restarting daemons to that suggested in dbmail bug #600

* Wed May 23 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.5-0.1.rc3
- update to svn 2.2.5rc3
- remove unneccessary patches
- make sqlite default driver for better out of the box experience

* Thu Mar 23 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.4-4
- actually APPLY the short write patch

* Thu Mar 22 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.4-3
- patch to eliminate short write messages
- use /sbin/service instead of running init scripts directly
- requires for initscripts because daemon function in initfile requires it
- modern tarballs do not require xmlto and asciidoc to build the docs
- change conditionals to give everything sqlite support unless it's built in
  the fedora buildsystem and %%{fedora} < 4

* Tue Mar 20 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.4-2
- patch to fix expunge bug

* Tue Mar 13 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.4-1
- v. 2.2.4
- remove umask patch as it's included upstream now

* Wed Feb 28 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.3-1
- v. 2.2.3
- tab removal in dbmail.conf no longer required
- libsqlite.so in not built anymore unless specified, remove fix
- libauth-ldap.so wasn't be built properly, fixed
- rework umask patch, still want a stronger umask on log files

* Tue Feb 20 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.2-9
- fix source0 location

* Tue Feb 20 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.2-8
- change /etc/dbmail.conf to mode 0600
- remove README.solaris, create README.fedora
- add ref to README.fedora in %%desc

* Tue Feb 20 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.2-7
- make macro tests a little more readable
- change dbmail-database to dbmail-database-driver; more descriptive
- reduce gmime reqs to 2.1.19
- specify sqlite req at 3 or greater

* Sun Feb 18 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.2-6
- remove bogus require for main package on mysql
- virtual depend with exact %%{version}-%%{release}
- remove extra mysql-devel BR
- update description to include sqlite if built with sqlite
- for mysql, 4.1.3 is required, not just 4.1
- add requires for vixie-cron
- move database specific docs to database subpackages

* Sun Feb 18 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.2-5
- fix perms on man pages

* Sat Feb 17 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.2-4
- fix a few things in scriptlets for consistency
- send error output from logrotate HUP to /dev/null
- explicitly require initscripts since they all use the daemon function
- use explicit %%{version}-%%{release} for provides

* Mon Feb 05 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.2-3
- fix typo in logrotate script
- patch umask for log files to be something more reasonable

* Sat Jan 31 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.2-2
- add some conditionals for not building sqlite on some product releases
- substitude \t for tab in sed so that rpmlint doesn't complain about mixing
  tabs and spaces

* Sat Jan 31 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.2-1
- add logrotate for dbmail.err
- sub packages depend on %%{version}-%%{release}
- update to 2.2.2
- remove mailbox2dbmail patch
- translate tabs to space in dbmail.conf
- remove errno race patch

* Sat Jan 13 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.1-7
- revert to older fedora-usermgmt macros so rpm will work with older os

* Fri Jan 12 2007 Bernard Johnson <bjohnson@symetrix.com> 2.2.1-6
- add patch to fix errno race condition
- don't delete libsort_sieve.so, it's a module

* Thu Dec 14 2006 Bernard Johnson <bjohnson@symetrix.com> 2.2.1-5
- fix my local svn that caused x bit on init files to sneak in

* Thu Dec 14 2006 Bernard Johnson <bjohnson@symetrix.com> 2.2.1-4
- cleanup of spec file
- use fedora-usermgmt hooks
- split and build all database libraries
- kill modules/.libs from the module load path

* Tue Dec 05 2006 Bernard Johnson <bjohnson@symetrix.com> 2.2.1-3
- leave the right .so files for modules

* Mon Nov 27 2006 Bernard Johnson <bjohnson@symetrix.com> 2.2.1-2
- update with Fedora Extras style spec file

* Sat Nov 18 2006 Bernard Johnson <bjohnson@symetrix.com> 2.2.1-1.sc
- version 2.2.1

* Mon Nov 15 2006 Bernard Johnson <bjohnson@symetrix.com> 2.2.0-4.sc
- release 2.2.0-1.sc