# XXX use of _libdir appears incorrect, should be _libexecdir?
%define _usrlibdir      %{_prefix}/lib
%define _libdir         %{_usrlibdir}/nmh

%{expand: %%define _sysconfdir %{_sysconfdir}/nmh}

%define dbdir db2
%define dblib $RPM_BUILD_DIR/%{name}-%{version}/%{dbdir}

Summary: A capable mail handling system with a command line interface.
Name: nmh
Obsoletes: mh
Provides: mh
Version: 1.0.4
Release: 18
Copyright: freeware
Group: Applications/Internet
Source0: ftp://ftp.mhost.com/pub/nmh/nmh-%{version}.tar.gz
Source1: procmailrc.example
Patch0: nmh-1.0.3-config.patch
Patch1: nmh-1.0.3-buildroot.patch
Patch3: nmh-1.0.3-compat21.patch
Patch4: nmh-1.0.4-bug7246.patch

# internalized db2 
Source2: db2-glibc-2.1.3.tar.gz
Patch80: db2-glibc-2.1.3.patch
Patch81: db2-2.4.14-db2.patch
Patch82: db2-2.4.14-s390.patch
Patch83: db2-2.4.14-nodebug.patch
Patch84: db2-2.4.14-fileid_64bit_fix.patch
BuildRoot: %{_tmppath}/%{name}-root
Prereq: fileutils
BuildPreReq: libtermcap-devel

%description
Nmh is an email system based on the MH email system and is intended to
be a (mostly) compatible drop-in replacement for MH.  Nmh isn't a
single comprehensive program.  Instead, it consists of a number of
fairly simple single-purpose programs for sending, receiving, saving,
retrieving and otherwise manipulating email messages.  You can freely
intersperse nmh commands with other shell commands or write custom
scripts which utilize nmh commands.  If you want to use nmh as a true
email user agent, you'll want to also install exmh to provide a user
interface for it--nmh only has a command line interface.

If you'd like to use nmh commands in shell scripts, or if you'd like to
use nmh and exmh together as your email user agent, you should install
nmh.

%prep
%setup -q -a 2
%patch0 -p1
%patch1 -p1
%patch3 -p1
%patch4 -p1

# XXX add promailrc.example
cp %SOURCE1 .

cd %{dbdir}
%patch80 -p1 -b .db2
%patch81 -p1 -b .fpic
%patch82 -p1 -b .nodebug
%patch84 -p1 -b .64fix

%build

## to db
cd %{dbdir}
(cd include && ln -sf db.h db_185.h)
CFLAGS="$RPM_OPT_FLAGS" make libdb2.a

## back to main dir
cd $RPM_BUILD_DIR/%{name}-%{version}

export LIBS="-ldb2 -L%{dblib}"
export CFLAGS="$RPM_OPT_FLAGS -I/usr/include/gdbm"
%configure --with-editor=/bin/vi --with-mts=sendmail --enable-nmh-pop

make

%install
rm -rf ${RPM_BUILD_ROOT}

# XXX unnecessary because DOT_LOCKING is disabled
make DESTDIR=${RPM_BUILD_ROOT} MAIL_SPOOL_GRP=$(id -gn) install

{ cd ${RPM_BUILD_ROOT}

# XXX tmac.h is needed by nmh man pages, move to %{_mandir}
  echo '.\" ---> This file is in %{_mandir} because it is sourced by nmh man pages.' > .%{_mandir}/tmac.h
  cat .%{_sysconfdir}/tmac.h >> .%{_mandir}/tmac.h
  perl -pi -e 's/^\.so \/etc\/nmh\/tmac.h/.so tmac.h/' .%{_mandir}/man*/*
  rm -f .%{_sysconfdir}/tmac.h

# XXX unnecessary because DOT_LOCKING is disabled
# chown root.mail .%{_bindir}/inc
# chmod 2755 .%{_bindir}/inc
  chmod g-s .%{_bindir}/inc
}

%clean
rm -rf ${RPM_BUILD_ROOT}

%post
if [ ! -d %{_bindir}/mh -a ! -L %{_bindir}/mh ] ; then
    ln -s . %{_bindir}/mh
fi
if [ ! -d %{_usrlibdir}/mh -a ! -L %{_usrlibdir}/mh ] ; then
    ln -s nmh %{_usrlibdir}/mh
fi
if [ -d /etc/smrsh -a ! -L /etc/smrsh/slocal ] ; then
    ln -sf %{_libdir}/slocal /etc/smrsh/slocal
fi

%triggerpostun -- mh, nmh <= 0.27-7
if [ ! -d %{_bindir}/mh -a ! -L %{_bindir}/mh ] ; then
    ln -s . %{_bindir}/mh
fi
if [ ! -d %{_usrlibdir}/mh -a ! -L %{_usrlibdir}/mh ] ; then
    ln -s nmh %{_usrlibdir}/mh
fi

%preun
if [ $1 = 0 ]; then
    [ ! -L %{_bindir}/mh ] || rm -f %{_bindir}/mh
    [ ! -L %{_usrlibdir}/mh ] || rm -f %{_usrlibdir}/mh
    [ ! -d /etc/smrsh -a -L /etc/smrsh/slocal ] || rm -f /etc/smrsh/slocal
fi

%files
%defattr(-,root,root)
%doc COMPLETION-* COPYRIGHT DIFFERENCES FAQ MAIL.FILTERING README TODO VERSION
%doc procmailrc.example
%{_bindir}/*
%dir %{_sysconfdir}
%config %{_sysconfdir}/*

# XXX use of _libdir appears incorrect, should be libexecdir?
%dir %{_libdir}
%{_libdir}/*

%{_mandir}/tmac.h
%{_mandir}/man[158]/*

%changelog
* Wed Jan 22 2003 Tim Powers <timp@redhat.com>
- rebuilt

* Fri Nov 29 2002 Florian La Roche <Florian.LaRoche@redhat.de>
- hack to build again

* Fri Jun 21 2002 Tim Powers <timp@redhat.com>
- automated rebuild

* Wed Jun 19 2002 Florian La Roche <Florian.LaRoche@redhat.de>
- do not strip apps

* Thu May 23 2002 Tim Powers <timp@redhat.com>
- automated rebuild

* Thu Jan 31 2002 Jeremy Katz <katzj@redhat.com>
- whack in a static db2

* Sat Jan 26 2002 Florian La Roche <Florian.LaRoche@redhat.de>
- db1-devel is gone

* Mon Jun 25 2001 Florian La Roche <Florian.LaRoche@redhat.de>
- add build deps

* Wed Feb 14 2001 Florian La Roche <Florian.LaRoche@redhat.de>
- enable pop support #20258

* Wed Jan 24 2001 Trond Eivind Glomsrød <teg@redhat.com>
- add fileutils to Prereq: (#24667)

* Thu Aug 17 2000 Jeff Johnson <jbj@redhat.com>
- summaries from specspo.

* Wed Aug 16 2000 Jeff Johnson <jbj@redhat.com>
- segfault with malformed "Subject:yadda" (#7246)

* Wed Jul 12 2000 Prospector <bugzilla@redhat.com>
- automatic rebuild

* Thu Jun 22 2000 Bill Nottingham <notting@redhat.com>
- talk to sendmail, not to a local SMTP server

* Thu Jun 15 2000 Jeff Johnson <jbj@redhat.com>
- FHS packaging.
- remove /etc/nmh/tmac.h, as it is now in %{_mandir}/tmac.h.
- Exclude ia64 for now.

* Sun Apr 16 2000 Bernhard Rosenkraenzer <bero@redhat.com>
- 1.0.4

* Mon Feb 28 2000 Jeff Johnson <jbj@redhat.com>
- security fix for mime remote exploit.
- permit tmac.h to be sourced for nmh man pages to "work".
- add procmailrc.example in /usr/doc

* Tue Feb 22 2000 Jeff Johnson <jbj@redhat.com>
- diddle %preun exit code to return 0 when sendmail is not installed (#9651).

* Mon Feb  7 2000 Bill Nottingham <notting@redhat.com>
- handle compressed manpages

* Tue Jun  1 1999 Jeff Johnson <jbj@redhat.com>
- add /etc/smrsh/slocal symlink to permit slocal functionality. (#3181).

* Mon May 31 1999 Jeff Johnson <jbj@redhat.com>
- update to nmh-1.0.

* Sun Apr 18 1999 <ewt@redhat.com>
- fixed mh compatibility symlinks on removal

* Sun Mar 21 1999 Cristian Gafton <gafton@redhat.com>
- auto rebuild in the new build environment (release 7)

* Tue Jan 26 1999 Jeff Johnson <jbj@redhat.com>
- restore /usr/bin files to package.

* Wed Jan 13 1999 Jeff Johnson <jbj@redhat.com>
- fix reversed args in strncpy within security patch (#821).

* Sun Jan 10 1999 Jeff Johnson <jbj@redhat.com>
- turn off remnant setgid root.mail (which packaged as setgid root.root because
  of %defattr in %files) (bug #769).
- glibc-2.1 compatibility.

* Sat Jul 18 1998 Jeff Johnson <jbj@redhat.com>
- update to 0.27 (security patches too)

* Sun May  3 1998 Alan Cox <alan@redhat.com>
- Fixed the obvious security holes. Im not sure I got them all, I may
  have broken a couple of things in the process. The code is such a
  complete pile of donkey poo it is hard to be sure.

* Fri Apr 24 1998 Prospector System <bugs@redhat.com>
- translations modified for de, fr, tr

* Sun Apr 19 1998 Erik Troan <ewt@redhat.com>
- removed /usr/bin/mh and /usr/lib/mh from file list -- create them in
  scripts instead to avoid conflicts with the old mh package

* Sat Apr 11 1998 Donnie Barnes <djb@redhat.com>
- various spec file cleanups

* Wed Apr 8 1998 Bryan C. Andregg <bandregg@redhat.com>
- Fixed symlink install bug

* Tue Mar 31 1998 Bryan C. Andregg <bandregg@redhat.com>
- Added symlinks: /usr/bin/mh -> /usr/bin, /usr/lib/mh -> /usr/lib/nmh

* Wed Mar 25 1998 Bryan C. Andregg <bandregg@redhat.com>
- Initial build