#
# Copyright (c) 2005, 2006, 2007 Ralf Corsepius, Ulm, Germany.
# This file and all modifications and additions to the pristine
# package are under the same license as the package itself.
#

%define RT3_BINDIR		%{_sbindir}
%define RT3_LIBDIR		%{perl_vendorlib}
%define RT3_WWWDIR		%{_datadir}/rt3/html
%define RT3_LOGDIR		%{_localstatedir}/log/rt3
%define RT3_CACHEDIR		%{_localstatedir}/cache/rt3
%define RT3_LOCALSTATEDIR	%{_localstatedir}/lib/rt3

Name:		rt3
Version:	3.6.5
Release:	1
Summary:	Request tracker 3

Group:		Applications/Internet
License:	GPLv2+
URL:		http://www.bestpractical.com/rt
Source0:	http://www.bestpractical.com/pub/rt/release/rt-%{version}.tar.gz
Source3:	rt3.conf.in
Source4:	README.fedora.in

Patch0:		rt-3.6.1-config.diff
Patch1:		rt-3.4.1-I18N.diff
Patch2:		rt-3.6.0-Makefile.diff

BuildArch:	noarch
BuildRoot:	%{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)

# For Debian compatibility
Provides:	request-tracker3 = %{version}-%{release}

BuildRequires:	/usr/bin/pod2man
BuildRequires:	/usr/sbin/apachectl

Requires:  perl(:MODULE_COMPAT_%(eval "`%{__perl} -V:version`"; echo $version))

Requires: perl-DBD-MySQL mysql-server

Requires(postun): /bin/rm

# rpm doesn't catch these:
Requires: perl(Apache::Session)
Requires: perl(Calendar::Simple)
Requires: perl(File::Temp)
Requires: perl(GD::Text)
Requires: perl(GD::Graph::bars) perl(GD::Graph::pie)
Requires: perl(HTML::Mason)
Requires: perl(HTML::FormatText)
Requires: perl(HTML::TreeBuilder)
Requires: perl(HTTP::Server::Simple::Mason)
Requires: perl(I18N::LangTags::List)
Requires: perl(Locale::Maketext::Fuzzy)
Requires: perl(LWP::MediaTypes)
Requires: perl(MIME::Entity)
Requires: perl(mod_perl2)
Requires: perl(Module::Versions::Report)
Requires: perl(Regexp::Common)
Requires: perl(Tree::Simple)
Requires: perl(URI::URL)

# Split out. Technically, not actually necessary, but ... let's keep it for now.
Requires: rt3-mailgate

%description
RT is an enterprise-grade ticketing system which enables a group of people
to intelligently and efficiently manage tasks, issues, and requests submitted
by a community of users.

%package mailgate
Summary: rt3's mailgate utility.
Group:   Applications/Internet
# rpm doesn't catch these:
Requires:	perl(Pod::Usage)
Requires:	perl(HTML::TreeBuilder)
Requires:	perl(HTML::FormatText)

%description mailgate
%{summary}

%prep
%setup -q -n rt-%{version}

sed -e 's,@RT3_CACHEDIR@,%{RT3_CACHEDIR},' %{SOURCE4} \
  > README.fedora

# Fixup the tarball shipping with broken permissions
find . \( -name '*.pm' -o -name '*.pm.in' -o -name '*.po' -o -name '*.pod' \) \
  -exec chmod a-x {} \;
chmod -x UPGRADING README C* aclocal.* config.* *.ac *.in
find etc -type f -exec chmod a-x {} \;

%patch0 -p1
%patch1 -p1
%patch2 -p1

# Patch backups added by rpm disturb
find -name '*.orig' -exec rm -f {} \;

cat << \EOF > %{name}-prov
#!/bin/sh
%{__perl_provides} $* |\
    sed -e '/^perl(RT)$/d' \
    	-e '/^perl(HTML::Mason/d' \
        -e '/^perl(Log::Dispatch/d'
EOF
%define __perl_provides %{_builddir}/rt-%{version}/%{name}-prov
chmod +x %{__perl_provides}

cat << \EOF > %{name}-req
#!/bin/sh
%{__perl_requires} $* |\
    sed -e '/^perl(Encode::compat)$/d' \
        -e '/^perl(RT::/d'
EOF
%define __perl_requires %{_builddir}/rt-%{version}/%{name}-req
chmod +x %{__perl_requires}

# Propagate rpm's directories to config.layout
cat << \EOF >> config.layout

#   Fedora directory layout.
<Layout Fedora>
  bindir:		%{RT3_BINDIR}
  sysconfdir:		%{_sysconfdir}/rt3
  libdir:		%{RT3_LIBDIR}
  manualdir:		${datadir}/doc
  localstatedir:	%{RT3_LOCALSTATEDIR}
  htmldir:		%{RT3_WWWDIR}
  logfiledir:		%{RT3_LOGDIR}
  masonstatedir:	%{RT3_CACHEDIR}/mason_data
  sessionstatedir:	%{RT3_CACHEDIR}/session_data
  customdir:		%{_prefix}/local/lib/rt3
  custometcdir:		%{_prefix}/local/etc/rt3
  customhtmldir:	${customdir}/html
  customlexdir:		${customdir}/po
  customlibdir:		${customdir}/lib
</Layout>
EOF

# Comment out the Makefile trying to change groups/owners
# Fix DESTDIR support
sed -i \
	-e 's,	chgrp,	: chrgp,g' \
	-e 's,	chown,	: chown,g' \
	-e 's,$(DESTDIR)/,$(DESTDIR),g' \
Makefile.in

%build
%configure \
--with-apachectl=/usr/sbin/apachectl \
--with-web-user=apache --with-web-group=apache \
--enable-layout=Fedora --with-modperl2 \
--libdir=%{RT3_LIBDIR}
make %{?_smp_mflags}

# Generate man-pages
/usr/bin/pod2man bin/rt-mailgate > bin/rt-mailgate.1
/usr/bin/pod2man bin/mason_handler.fcgi > bin/mason_handler.fcgi.1

%install
rm -rf ${RPM_BUILD_ROOT}
make install DESTDIR=${RPM_BUILD_ROOT}

# Cleanup the mess rt's configuration leaves behind
rm -f ${RPM_BUILD_ROOT}%{_docdir}/README

# Win32 stuff
rm -f ${RPM_BUILD_ROOT}%{RT3_BINDIR}/mason_handler.svc

# We don't want CPAN
rm -f ${RPM_BUILD_ROOT}%{_sbindir}/rt-test-dependencies

# An installed testsuite without infrastructure
rm -rf ${RPM_BUILD_ROOT}%{RT3_LIBDIR}/t

# Bogus
rm -f ${RPM_BUILD_ROOT}%{RT3_LIBDIR}/RT.pm.in

# Unsupported
rm -f ${RPM_BUILD_ROOT}%{RT3_BINDIR}/*.scgi

# Install apache configuration
mkdir -p ${RPM_BUILD_ROOT}%{_sysconfdir}/httpd/conf.d
sed -e 's,@RT3_WWWDIR@,%{RT3_WWWDIR},g' \
  -e 's,@RT3_BINDIR@,%{RT3_BINDIR},g' \
  %{SOURCE3} > ${RPM_BUILD_ROOT}%{_sysconfdir}/httpd/conf.d/rt3.conf

mkdir -p ${RPM_BUILD_ROOT}%{_mandir}/man1
install -m 0644 bin/rt-mailgate.1 bin/mason_handler.fcgi.1 \
  ${RPM_BUILD_ROOT}%{_mandir}/man1

if [ "%{_bindir}" != "%{RT3_BINDIR}" ]; then
  mkdir -p ${RPM_BUILD_ROOT}%{_bindir}
  mv ${RPM_BUILD_ROOT}%{RT3_BINDIR}/rt \
    ${RPM_BUILD_ROOT}%{_bindir}
fi

install -d -m755 ${RPM_BUILD_ROOT}%{_prefix}/local/etc/rt3
install -d -m755 ${RPM_BUILD_ROOT}%{_prefix}/local/lib/rt3
install -d -m755 ${RPM_BUILD_ROOT}%{_prefix}/local/lib/rt3/html
install -d -m755 ${RPM_BUILD_ROOT}%{_prefix}/local/lib/rt3/po
install -d -m755 ${RPM_BUILD_ROOT}%{_prefix}/local/lib/rt3/lib

install -d -m755 ${RPM_BUILD_ROOT}%{RT3_LOGDIR}
install -d -m755 ${RPM_BUILD_ROOT}%{RT3_LOCALSTATEDIR}

# Fix permissions
find ${RPM_BUILD_ROOT}%{RT3_WWWDIR} \
  -type f -exec chmod a-x {} \;

%check
# The tests don't work:
# - Require to be run as root
# - Require an operational rt3 system
%{?_with_tests:make test}

%clean
rm -rf ${RPM_BUILD_ROOT}

%postun
if [ $1 -eq 0 ]; then
  /bin/rm -rf %{RT3_CACHEDIR}
fi


%files
%defattr(-,root,root,-)
%doc COPYING README UPGRADING README.fedora
%{_bindir}/*
%{_sbindir}/*
%exclude %{_sbindir}/rt-mailgate
%{_mandir}/man1/*
%exclude %{_mandir}/man1/rt-mailgate*
%{RT3_LIBDIR}/*
%{RT3_LOGDIR}

%dir %{_sysconfdir}/rt3
%attr(-,root,root)%{_sysconfdir}/rt3/upgrade
%attr(-,root,root)%{_sysconfdir}/rt3/acl*
%attr(-,root,root)%{_sysconfdir}/rt3/schema*
%attr(-,root,root)%{_sysconfdir}/rt3/init*
%config(noreplace) %attr(0640,root,root) %{_sysconfdir}/rt3/RT_*

%dir %{_datadir}/rt3
%{RT3_WWWDIR}
%config(noreplace) %{_sysconfdir}/httpd/conf.d/rt3.conf

%dir %{RT3_CACHEDIR}
%attr(0770,apache,apache) %{RT3_CACHEDIR}/mason_data
%attr(0770,apache,apache) %{RT3_CACHEDIR}/session_data

%if "%{RT3_LOCALSTATEDIR}" != "%{RT3_CACHEDIR}"
%dir %{RT3_LOCALSTATEDIR}
%endif

%ghost %{_prefix}/local/lib/rt3
%ghost %{_prefix}/local/etc/rt3

%files mailgate
%{_sbindir}/rt-mailgate
%{_mandir}/man1/rt-mailgate*

%changelog
* Wed Oct 17 2007 Ralf Corsépius <rc040203@freenet.de> - 3.6.5-1
- Upstream update.
- Split-out rt-mailgate in to rt3-mailgate (BZ 332731).

* Tue Sep 04 2007 Ralf Corsépius <rc040203@freenet.de> - 3.6.4-1
- Upstream update.

* Tue Sep 04 2007 Ralf Corsépius <rc040203@freenet.de> - 3.6.3-2
- Update license tag.

* Fri Dec 29 2006 Ralf Corsépius <rc040203@freenet.de> - 3.6.3-1
- Upstream update.
- Add perl(GD::*) deps.

* Thu Sep 07 2006 Ralf Corsépius <rc040203@freenet.de> - 3.6.1-3
- Extend /etc/RT_SiteConfig.pm (jpo, BZ 202374)

* Wed Sep 06 2006 Ralf Corsépius <rc040203@freenet.de> - 3.6.1-2
- Mass rebuild.

* Sun Aug 13 2006 Ralf Corsépius <rc040203@freenet.de> - 3.6.1-1
- Upstream update.

* Sat Jul 22 2006 Ralf Corsépius <rc040203@freenet.de> - 3.6.0-4
- Install etc/upgrade to %%{_sysconfdir}/rt3/upgrade.
- Add rt-3.6.0-Makefile.diff.

* Mon Jul 19 2006 Ralf Corsépius <rc040203@freenet.de> - 3.6.0-3
- Move /var/www/rt3 to %%{_datadir}/rt3/html

* Fri Jun 23 2006 Ralf Corsépius <rc040203@freenet.de> - 3.6.0-2
- Require perl(Calendar::Simple).

* Fri Jun 23 2006 Ralf Corsépius <rc040203@freenet.de> - 3.6.0-1
- Upstream update.

* Sun Jan 15 2006 Ralf Corsépius <rc040203@freenet.de> - 3.4.5-1
- Upstream update.

* Wed Dec 07 2005 Ralf Corsépius <rc040203@freenet.de> - 3.4.4-11
- Require perl(HTML::FormatText), perl(HTML::TreeBuilder) (#175176).

* Fri Nov 04 2005 Ralf Corsépius <rc040203@freenet.de> - 3.4.4-10
- Inline rt3-filter-provides.sh, rt3-filter-requires.sh into spec.

* Sat Oct 29 2005 Ralf Corsépius <rc040203@freenet.de> - 3.4.4-9
- Remove "Requires(post): /usr/bin/chcon".
- Add "Requires: perl(:MODULE_COMPAT...)".

* Fri Oct 28 2005 Ralf Corsépius <rc040203@freenet.de> - 3.4.4-8
- Fix typo in setting up localstatedir.
- Own %%{RT3_CACHEDIR}.

* Tue Oct 24 2005 Ralf Corsépius <rc040203@freenet.de> - 3.4.4-7
- Apply patch from Chris Grau to README.fedora.
- Move mason_data, session_data to /var/cache/rt3.
- Install %%{RT3_LOGDIR}.
- Replace README.fedora with README.fedora.in

* Thu Oct 13 2005 Ralf Corsépius <rc040203@freenet.de> - 3.4.4-6
- Remove RT3_USER, RT3_GROUP.

* Thu Oct 13 2005 Ralf Corsépius <rc040203@freenet.de> - 3.4.4-5
- Add Requires: perl(HTML::Mason), perl(HTTP::Server::Simple::Mason).
- Add README.fedora.
- Pass libdir to configure to silence rpmlint.
- Remove %%{_localstatedir}/lib/rt3 upon last package removal.

* Fri Oct 07 2005 Ralf Corsépius <rc040203@freenet.de> - 3.4.4-4
- Fix urls in spec file.

* Tue Sep 27 2005 Ralf Corsépius <rc040203@freenet.de> - 3.4.4-3
- Spec file cosmetics.

* Mon Sep 26 2005 Ralf Corsépius <rc040203@freenet.de> - 3.4.4-2
- FE submission.

* Sun Sep 25 2005 Ralf Corsépius <rc040203@freenet.de> - 3.4.4-1.4
- Install perl modules into %%perlvendorlib.

* Fri Sep 23 2005 Ralf Corsépius <rc040203@freenet.de> - 3.4.4-1.3
- Shift customdir to %%{_prefix}/local/lib/rt3 and %%{_prefix}/local/etc/rt3.
- %%ghost %%{_prefix}/local/lib/rt3 and %%{_prefix}/local/etc/rt3.
- install BIN files to %%{_sbindir}.

* Fri Sep 23 2005 Ralf Corsépius <rc040203@freenet.de> - 3.4.4-1.2
- Use %%RT3_WWWDIR, %%_sbindir, %%_bindir in config.layout.

* Thu Sep 17 2005 Ralf Corsépius <rc040203@freenet.de> - 3.4.4-1.1
- Reflect feedback from Chris Grau.
- Remove SpeedyCGI support.

* Thu Sep 16 2005 Ralf Corsépius <rc040203@freenet.de> - 3.4.4-1
- FE submission candidate.