# Detect the distribution in use
%global __despace head -n 1 | tr -d '[:space:]' | sed -e 's/[(].*[)]//g'
%global __lower4 cut -c 1-4 | tr '[:upper:]' '[:lower:]'
%global __distfile %([ -f /etc/SuSE-release ] && echo /etc/SuSE-release || echo /etc/redhat-release)
%global __distinit %(sed -e 's/ release .*//' -e 's/\\([A-Za-z]\\)[^ ]*/\\1/g' %{__distfile} | %{__despace} | %{__lower4})
%global __distvers %(sed -e 's/.* release \\([^. ]*\\).*/\\1/' %{__distfile} | %{__despace})
# Identify CentOS Linux and Scientific Linux as rhel
%if "%{__distinit}" == "c" || "%{__distinit}" == "cl" || "%{__distinit}" == "sl" || "%{__distinit}" == "sls"
%global __distinit rhel
%endif
# Dist tag for Fedora is still "fc"
%if "%{__distinit}" == "f"
%global __distinit fc
%endif

# perl_vendorlib/perl_vendorarch not defined by default prior to F-2
%{!?perl_vendorlib: %global perl_vendorlib %(eval "`perl -V:installvendorlib`"; echo $installvendorlib)}

# The MIME::tools package will build with MIME::Base64 ≥ 2.20, but some tests
# will fail with MIME::Base64 < 3.03. So if the MIME::Base64 version on the
# system is older than 3.03, we build with MIME::XBase64 instead. We can't just
# require an updated version of MIME::Base64 because it's bundled with the
# distribution perl package and cannot be conveniently updated.

# Note: MIME::tools > 5.420 require File::Temp ≥ 0.17 or later, which is not
# available in Fedora prior to Fedora 9 (it's a core module)

Summary:	Perl modules for parsing (and creating!) MIME entities
Name:		perl-MIME-tools
Epoch:		1
Version:	5.420
Release:	9.%{__distinit}%{__distvers}
License:	GPL+ or Artistic
Group:		Development/Libraries
URL:		http://search.cpan.org/dist/MIME-tools/
Source0:	http://search.cpan.org/CPAN/authors/id/D/DS/DSKOLL/MIME-tools-%{version}.tar.gz
Patch0:		MIME-tools-5.420-utf8.patch
Requires:	perl(IO::Stringy) >= 1.211, perl-MailTools >= 1.50, perl(MIME::QuotedPrint)
BuildRequires:	perl(IO::Stringy) >= 1.211, perl-MailTools >= 1.50, perl(MIME::QuotedPrint)
BuildArch:	noarch
Buildroot:	%{_tmppath}/%{name}-%{version}-%{release}-root-%(id -nu)
BuildRequires:	perl(ExtUtils::MakeMaker), perl(Test::More)
Requires:	perl(:MODULE_COMPAT_%(eval "`perl -V:version`"; echo $version))

# See if we can use the MIME::Base64 bundled with perl
%global use_mb64 %(perl -MMIME::Base64 -e 'print $MIME::Base64::VERSION >= 3.03 ? 1 : 0')
%if %{use_mb64}
BuildRequires:	perl(MIME::Base64) >= 3.03
%else
BuildRequires:	perl(MIME::XBase64) >= 3.03
%endif

%description
MIME-tools - modules for parsing (and creating!) MIME entities. Modules in this
toolkit: Abstract message holder (file, scalar, etc.), OO interface for
decoding MIME messages, an extracted and decoded MIME entity, Mail::Field
subclasses for parsing fields, a parsed MIME header (Mail::Header subclass),
parser and tool for building your own MIME parser, and utilities.

%prep
%setup -q -n MIME-tools-%{version}
%if ! %{use_mb64}
# Need to use MIME::XBase64 module instead of MIME::Base64 module as the MIME::Base64
# supplied with the perl RPM is too old
for f in ChangeLog Makefile.PL META.yml README t/*.t $(/usr/bin/find lib -name '*.pm' -print)
do
	sed -i -e 's/MIME::Base64/MIME::XBase64/g' -e 's/MIME::QuotedPrint/MIME::XQuotedPrint/g' $f
done
%endif

# Fix character encoding
%patch0 -p1

# Remove bogus provide of perl(main)
%global provfilt /bin/sh -c "%{__perl_provides} | grep -Fvx 'perl(main)'"
%define __perl_provides %{provfilt}

# The more useful examples will go in %%{_bindir}
mkdir useful-examples
mv examples/mime{dump,encode,explode,postcard,send} useful-examples

%build
perl Makefile.PL INSTALLDIRS=vendor
make %{?_smp_mflags}

%install
rm -rf %{buildroot}
make pure_install PERL_INSTALL_ROOT=%{buildroot}
find %{buildroot} -type f -name .packlist -exec rm -f {} ';'
find %{buildroot} -depth -type d -exec rmdir {} ';' 2>/dev/null
chmod -R u+w %{buildroot}

# Put the more useful examples in %%{_bindir}
install -d -m 755 %{buildroot}%{_bindir}
install -d -m 755 %{buildroot}%{_mandir}/man1
cd useful-examples
for ex in mime*
do
	install -p -m 755 ${ex} %{buildroot}%{_bindir}/
	pod2man ${ex} > %{buildroot}%{_mandir}/man1/${ex}.1
done
cd -

%check
make test

%clean
rm -rf %{buildroot}

%files
%defattr(-,root,root,-)
%doc README* COPYING ChangeLog
# Adding examples introduces additional deps, but these are all satisfied by
# perl, perl-MIME-tools, and perl-MailTools, which are all deps anyway.
%doc examples/
%{perl_vendorlib}/MIME/
%{_bindir}/mimedump
%{_bindir}/mimeencode
%{_bindir}/mimeexplode
%{_bindir}/mimepostcard
%{_bindir}/mimesend
%{_mandir}/man1/mimedump.1*
%{_mandir}/man1/mimeencode.1*
%{_mandir}/man1/mimeexplode.1*
%{_mandir}/man1/mimepostcard.1*
%{_mandir}/man1/mimesend.1*
%{_mandir}/man3/MIME::*.3pm*

%changelog
* Tue Oct 18 2011 Paul Howarth <paul@city-fan.org> 1:5.420-9
- Use a patch rather than scripted iconv to fix character encoding
- Nobody else likes macros for commands
- Fix dist tag for CentOS 6 and Scientific Linux

* Sat May 29 2010 Paul Howarth <paul@city-fan.org> 1:5.420-8
- Fix dist tag for RHEL-6 Beta

* Sat Feb 27 2010 Paul Howarth <paul@city-fan.org> 1:5.420-7
- Dist tag for Rawhide no longer needs special-casing

* Mon Sep 21 2009 Paul Howarth <paul@city-fan.org> 1:5.420-6
- Define RPM macrops in global scope where appropriate
- Don't query the RPM database to see if we can use MIME::Base64

* Thu Nov 13 2008 Paul Howarth <paul@city-fan.org> 1:5.420-5
- Tweak dist tag macros to work on current Rawhide with three-part releasenum

* Fri Nov 30 2007 Paul Howarth <paul@city-fan.org> 1:5.420-4
- Clarify license as GPL version 1 or later, or Artistic (i.e. same as perl)
- Buildrequire perl(Test::More)
- Add note explaining why we're stuck at an old version
- Install the more useful examples in %%{_bindir} (#201691)

* Sun Mar 18 2007 Paul Howarth <paul@city-fan.org> 1:5.420-2
- Fix dist tag for development builds
- Add buildreq perl(ExtUtils::MakeMaker)
- Fix argument order for find with -depth
- Define %%{__id_u} in a more portable way

* Mon Mar 20 2006 Paul Howarth <paul@city-fan.org> 1:5.420-1
- Support build on CentOS
- Update to 5.420
- Simplify provides-filter

* Tue Jan 17 2006 Paul Howarth <paul@city-fan.org> 1:5.419-2
- Convert ChangeLog to UTF8 encoding
- Don't provide perl(main)

* Fri Dec 23 2005 Paul Howarth <paul@city-fan.org> 1:5.419-1
- Update to 5.419
- Simplify distribution-detection
- Remove __perl_version and __perl_package macros for clarity
- Unonditionally remove buildroot in %%clean & %%install
- Don't use macros in paths for build-time commands, hardcode them instead

* Tue Oct  4 2005 Paul Howarth <paul@city-fan.org> 1:5.418-1
- Update to 5.418
- Fix directory ownerships
- Remove redundant find of empty .bs files
- Remove redundant setting of optimization flags
- Tidy up buildreqs

* Wed May 25 2005 Paul Howarth <paul@city-fan.org> 1:5.417-3
- Rewrite spec file in (mainly) Fedora Extras style
- Include full URL for source
- Fix URL
- Use MODULE_COMPAT dependency style and remove explicit perl dependency
- Remove MANIFEST from %%doc
- Use MIME::Base64 instead of MIME::XBase64 if it's sufficiently recent

* Thu Jan 27 2005 Paul Howarth <paul@city-fan.org> 1:5.417-2
- Bumped epoch number to 1 so that this version appears to be more
  recent than the unmaintained beta version, 6.200_02, that is still
  widely available.

* Thu Jan 27 2005 Paul Howarth <paul@city-fan.org> 5.417-1
- Update to 5.417

* Thu Nov 11 2004 Paul Howarth <paul@city-fan.org> 5.415-1
- Update to 5.415

* Tue Oct 26 2004 Paul Howarth <paul@city-fan.org> 5.414-1
- Initial RPM build