%define _ssldir %(openssl-config --openssldir)
%def_enable _sieve

Name: dovecot
Version: 1.0
Release: alt2.cvs20060619

Summary: Dovecot secure IMAP/POP3 server
License: GPL
Group: System/Servers
Url: http://%name.org/

Packager: Sergey Ivanov <seriv@altlinux.ru>

PreReq: shadow-utils, service, openssl >= 0.9.6g-alt2

###########################################
# Relations with other POP3/IMAP server pkgs (like courier-imap)

# Provide the abstract service names (which are virtual pkg names),
# specify their origin (our pkg name as the epoch + version-release):
Provides: IMAPD = %name:%version-%release
Provides: POP3D = %name:%version-%release

# End of the statements to describe relations with other POP3/IMAP server pkgs
########################################

Source0: %name-20060619.tar.gz
Source1: %name.init
Source2: %name.pam
Source3: %name-sieve-20060622.tar.bz2

# ALT patches
Patch0: %name-1.0-alpha4-alt-mkcert.patch
Patch1: %name-1.0-beta9-alt-conf.patch
Patch2: %name-1.0-beta9-alt-Make-with-sieve.patch

BuildPreReq: automake_1.9 
# Automatically added by buildreq on Thu Jun 22 2006
# BuildRequires: flex gcc-c++ gcc-g77 glibc-devel-static libldap-devel libMySQL-devel libpam-devel libsqlite3-devel linux-libc-headers openssl pkg-config postgresql-devel zlib-devel
# We don't need gcc-g77!
BuildRequires: flex gcc-c++ glibc-devel-static libldap-devel libMySQL-devel libpam-devel libsqlite3-devel linux-libc-headers openssl pkg-config postgresql-devel zlib-devel

%description
Dovecot is an IMAP/POP3 server for Linux/UNIX-like systems, written with
security primarily in mind. Although it's written with C, it uses
several coding techniques to avoid most of the common pitfalls.

Dovecot can work with standard mbox and maildir formats and it's fully
compatible with UW-IMAP and Courier IMAP servers as well as mail
clients accessing the mailboxes directly.

%prep

%setup -q -n %name-%version.beta9
%if_enabled _sieve
%setup -n %name-%version.beta9 -D -T -a 3
%endif
%patch0 -p1
%patch1 -p1
%if_enabled _sieve
%patch2 -p1
%endif


%build
%set_automake_version 1.9

export CPPFLAGS="`pkg-config --cflags-only-I openssl`"
export LDFLAGS="`pkg-config --libs-only-L openssl`"

%__aclocal
%__autoheader
%__libtoolize --force
%__automake --add-missing
%__autoconf

%if_enabled _sieve
  %configure \
	    --localstatedir=%_var                   \
	    --libexecdir=%_libdir                   \
	    --with-moduledir=%_libdir/%name/modules \
	    --sysconfdir=%_sysconfdir/%name         \
	    --enable-ipv6                           \
	    --without-deliver                       \
	    --with-rawlog                           \
	    --with-ssl=openssl                      \
	    --with-pop3d                            \
	    --with-pgsql                            \
	    --with-mysql                            \
	    --with-sqlite                           \
	    --with-ldap                             \
	    --with-cyrus-sasl2
  %make dovecot-config
  cd dovecot-sieve
  %__aclocal
  %__autoheader
  %__libtoolize --force
  %__automake --add-missing
  %__autoconf
  %configure --libexecdir=%_libdir
  cd ..
%else
  %configure \
	    --localstatedir=%_var                   \
	    --libexecdir=%_libdir                   \
	    --with-moduledir=%_libdir/%name/modules \
	    --sysconfdir=%_sysconfdir/%name         \
	    --enable-ipv6                           \
	    --with-rawlog                           \
	    --with-ssl=openssl                      \
	    --with-pop3d                            \
	    --with-pgsql                            \
	    --with-mysql                            \
	    --with-sqlite                           \
	    --with-ldap                             \
	    --with-cyrus-sasl2
  %make_build
%endif

%install
%make_install DESTDIR=%buildroot install

## Cleanup

# We will make our own %%doc set
%__rm -rf %buildroot%_docdir/%name

## Install

%__mkdir_p -m 0755 %buildroot%_sysconfdir/pam.d
%__mkdir_p -m 0755 %buildroot%_initdir
%__mkdir_p -m 0755 %buildroot%_sysconfdir/%name
%__mkdir_p -m 0755 %buildroot%_datadir/%name
%__mkdir_p -m 0755 %buildroot%_ssldir/{certs,private}

# Base directory
%__mkdir_p -m 0700 %buildroot%_var/run/%name

# Chroot for imap-login
%__mkdir_p -m 0750 %buildroot%_var/run/%name/login

# Init script
%__install -m 0755 %SOURCE1 %buildroot%_initdir/%name

# PAM config
%__install -m 0600 %SOURCE2 %buildroot%_sysconfdir/pam.d/%name

# Default config
%__mv -f %buildroot%_sysconfdir/%name/%name{-example,}.conf

# OpenSSL stuff
# Need more working on it.
## %__mv doc/dovecot-openssl.cnf %buildroot%_sysconfdir/%name
%__install -m 0755 doc/mkcert.sh %buildroot%_datadir/%name/mkcert

# Ghosts. How to include it in package and remove on
# package remove without checking of size mismatch?
touch %buildroot%_ssldir/certs/%name.pem
touch %buildroot%_ssldir/private/%name.pem
touch %buildroot%_var/run/%name/ssl-parameters.dat

# Done

%pre
%_sbindir/groupadd -r -f %name 2>/dev/null ||:
%_sbindir/useradd -r -n -g %name -d %_var/run/%name \
 -s /dev/null -c 'Dovecot secure IMAP server' %name 2>/dev/null ||:

%post
# adjust config for generating SSL certs
#HOSTNAME=`hostname -f`
#  %__subst "s|^CN=.*$|CN=$HOSTNAME|g;s|^emailAddress=.*$|emailAddress=root@$HOSTNAME|g" %_sysconfdir/%name/.cnf

# generate SSL certs
#    if [ ! -f %buildroot%_ssldir/certs/imapd.pem ]; then
#        echo -n "Generating SSL cert for imapd-ssl: "
#        cmd="%_datadir/%name/mk"$i"cert"
#        $cmd >/dev/null 2>&1
#        echo "%buildroot%_ssldir/certs/imapd.pem - done."
#    fi

echo "Generating SSL cert for imapd-ssl"
%_datadir/%name/mkcert

%post_service %name

%preun
%preun_service %name
#%postun
#if id %name >/dev/null 2>&1; then
# 	userdel %name
#fi

#if sg %name -c true >/dev/null 2>&1; then
#	groupdel nntpcache
#fi

%files
%config(noreplace) %_sysconfdir/%name/%name.conf
%config(noreplace) %_sysconfdir/pam.d/%name
%config(noreplace) %_initdir/%name

%dir %attr(0700,root,root) %_var/run/%name
%dir %attr(0750,root,%name) %_var/run/%name/login

%_libdir/%name
%_sysconfdir/%name
%_datadir/%name
%_sbindir/*

# Delivery 
%if_enabled _sieve
%_libdir/%name/sievec
%endif

%attr(0600,root,root) %ghost %config(missingok,noreplace) %verify(not md5 size mtime) %_var/run/%name/ssl-parameters.dat
%attr(0600,root,root) %ghost %config(missingok,noreplace) %verify(not md5 size mtime) %_ssldir/certs/%name.pem
%attr(0600,root,root) %ghost %config(missingok,noreplace) %verify(not md5 size mtime) %_ssldir/private/%name.pem

%doc doc/*.txt INSTALL AUTHORS doc/USE-WIKI-INSTEAD ChangeLog
%doc COPYING* TODO README NEWS doc/*.conf

%changelog
* Fri Jun 23 2006 Sergey Ivanov <seriv@altlinux.org> 1.0-alt2.cvs20060619
- Reverted to CVS version of 2006.06.19: recent changes with strict UID policy
  don't work on my mailboxes.

* Thu Jun 22 2006 Sergey Ivanov <seriv@altlinux.org> 1.0-alt1.cvs20060622
- updated to beta9, see Changelog. Sieve delivery agent now renamed to sievec. 
  Also closes bug #9634. 

* Wed Apr 12 2006 Sergey Ivanov <seriv@altlinux.org> 1.0-alt1.cvs20060412
- updated to beta7, see Changelog; now with working sieve-like delivery

* Fri Mar 03 2006 Sergey Ivanov <seriv@altlinux.org> 1.0-alt1.cvs20060303
- s/%_libexecdir\/%name/%_libdir\/%name/ in %files section

* Mon Feb 27 2006 Sergey Ivanov <seriv@altlinux.org> 1.0-alt0.cvs20060227
- updated to beta3 from cvs, with memory leak and other fixes;
  disabled sieve-like local delivery agent for now.

* Wed Feb 15 2006 Sergey Ivanov <seriv@altlinux.org> 1.0-alt0.cvs20060215
- updated to beta3 from cvs, with security fixes

* Mon Jan 30 2006 Sergey Ivanov <seriv@altlinux.org> 1.0-alt0.cvs20060130
- built from dovecot cvs 2006-01-30 and with sieve local delivery agent

* Sun Dec 04 2005 Sergey Ivanov <seriv@altlinux.ru> 1.0-alt0.cvs20051204
- 1.0-alpha4 of nightly cvs builds at 12/04/2005

* Mon Nov 21 2005 Sergey Ivanov <seriv@altlinux.ru> 0.99.14-alt5
- Fix postgresql-devel dependency, removed version number binding;
  fix %_libexecdir - %_libdir confusion;
  fix documentation installation. 

* Tue Oct 11 2005 Sergey Ivanov <seriv@altlinux.ru> 0.99.14-alt4
- removed undefined macro from commented-out text; 
  removed conflicting relation to other POP3/IMAP servers

* Fri Aug 05 2005 Sergey Ivanov <seriv@altlinux.ru> 0.99.14-alt3
- Fix #7479

* Sun May 22 2005 Sergey Ivanov <seriv@altlinux.ru> 0.99.14-alt2
- Buildreq fix: removed version binding for libpq-devel

* Fri Feb 18 2005 Sergey Ivanov <seriv@altlinux.ru> 0.99.14-alt1
-  Message address fields are now parsed differently, fixing some
  issues with spaces. Affects only clients which use FETCH ENVELOPE
  command.
- Message MIME parser was somewhat broken with missing MIME boundaries
- mbox: Don't allow X-UID headers in mails to override the UIDs we
  would otherwise set. Too large values can break some clients and
  cause other trouble.
- passwd-file userdb wasn't working
- PAM crashed with 64bit systems
- non-SSL inetd startup wasn't working
- If UID FETCH notices and skips an expunged message, don't return
  a NO reply. It's not needed and only makes clients give error
  messages.

* Thu Jan 06 2005 Sergey Ivanov <seriv@altlinux.ru> 0.99.13-alt1
- Update to new version. From it's changelog:
  * GNUTLS support hasn't been working for a while, so it's not even
    tried to be used anymore unless explicitly wanted.
  + Added CRAM-MD5 authentication mechanism. Patch by Joshua Goodall
  + Added SMD5 and LDAP-MD5 password schemes and changed MD5 scheme to
    use LDAP-MD5 if the password isn't in MD5crypt format. Patch by
    Joshua Goodall
  + Workaround for some POP3 client bugs: if message doesn't contain the
    "end of headers" empty line, add it automatically.
  + vpopmail supports now all password schemes, most importantly
    MD5crypt works now without support from libc's crypt()
  - SQL and LDAP authentication was broken
  - SEARCH UNKEYWORD wasn't working

* Sun Dec 05 2004 Sergey Ivanov <seriv@altlinux.ru> 0.99.12-alt1
- Updated to new version. From changelog of 0.99.12:
    - Fix memory leaks in LDAP, MySQL and PGSQL userdb/passdb
    - Fix hanging when parsing mails that have over 4096 bytes in one
      line (SMTP servers normally don't allow over 1000 bytes so it
      shouldn't be much of a problem)
    - FETCH BODYSTRUCTURE sometimes gave a wrong reply
      (eg. with FETCH (BODYSTRUCTURE RFC822.SIZE) if it wasn't cached)
    - Never return more than one INBOX in LIST even if there are such
      files. They don't work anyway and it just confuses clients.
    - mbox: Don't allow creating INBOX directory by creating/renaming
      mailboxes under it. They just wouldn't work.
    - POP3: Don't return PLAIN in SASL list. We don't support initial SASL
      responses, so it only breaks with most clients that try to use it.
    - IMAP and POP3 login processes may have sent each line in two IP
      packets, one with the data and another with CR+LF. Some clients
      didn't work because of this. 

* Thu Sep 23 2004 Sergey Ivanov <seriv@altlinux.ru> 0.99.11-alt1
- Updated to 0.99.11

* Mon Aug 02 2004 Sergey Ivanov <seriv@altlinux.ru> 0.99.10.9-alt1
- Update to 0.99.10.9

* Fri Jul 30 2004 Sergey Ivanov <seriv@altlinux.ru> 0.99.10.8-alt1
- Updated to 0.99.10.8

* Sat Jan 10 2004 Anton V. Denisov <avd@altlinux.org> 0.99.10.4-alt1.1
- Explicitly use automake-1.4 for build and run %%__automake before 
  %%configure (hope this fix build with new autotools and GCC).

* Mon Dec 15 2003 Anton V. Denisov <avd@altlinux.org> 0.99.10.4-alt1
- Updated to 0.99.10.4 (bugfix release).

* Tue Dec 09 2003 Anton V. Denisov <avd@altlinux.org> 0.99.10.2-alt1
- Initial release for ALT Linux Sisyphus.

* Mon Dec 08 2003 Anton V. Denisov <avd@altlinux.org> 0.99.10.2-alt0.2
- Built with pop3 daemon and enable it in config.
- Add into %%summary and %%descrition info about POP3 protocol.
- Minor improvements in %%files section.
- PreReq tuned.

* Tue Nov 11 2003 Anton V. Denisov <avd@altlinux.org> 0.99.10.2-alt0.1
- Updated to 0.99.10.2 (bugfix release).
- Removed auth-no-homedir.patch (no longer need).
- Updated our patches for new version.
- Add Packager tag.
- added %postun for user removal and commented it out.
- TODO is still todo.

* Mon Nov 10 2003 Anton V. Denisov <avd@altlinux.org> 0.99.10-alt0.1
- New version 0.99.10.
- Applied upstream bugfix patch.
- Added alt-conf-paths.patch
- Updated alt-mkcert.patch
- Updated %%description.
- Updated buildrequires.
- PAM config renamed: imap->%name
- SSL/TLS certs renamed.
- Additional flags for %%configure.
- Temporary build with --without-pop3d (should we?)
- Use default config instead of our.
- Mark %_initdir/%name as %%config(noreplace) (should we?).
- Init script updated.
- Corrected permissions for %_var/run/%name and %_var/run/%name/login.
- Other minor updates in spec file.
- TODO:
  + build and split modules (like postfix2 package).
  + other.

* Sun Nov 09 2003 Anton V. Denisov <avd@altlinux.org> 0.99.4-alt0.2
- Initial build for ALT Linux.
- Spec file cleaned up and improved (courier-imap.spec as example).
- Automatically added BuildRequires.
- %%confugure with additional keys.
- PAM configs added.
- Create user for imap-login process.
- added sample default config
- SSL/TLS certs generation during package install (need more working)
- TODO:
  + check FHS and ALT policy compliance
  + with/without logic of build (do we need shadow-auth support?)

* Sun Dec  1 2002 Seth Vidal <skvidal@phy.duke.edu>
- 0.99.4 and fix startup so it starts imap-master not vsftpd :)

* Tue Nov 26 2002 Seth Vidal <skvidal@phy.duke.edu>
- first build
