%define _prefix /usr
%define _sysconfdir /etc

%if %{?php_package_name:0}%{!?php_package_name:1}
# if php_package_name is not defined
%define php_package_name php
%endif

Summary: Codepot code repository manager
Name: @PACKAGE_NAME@
Version: @PACKAGE_VERSION@
Release: %{php_package_name}.1%{?dist}
License: GPL
Group: Applications/Utilities
#BuildArch: noarch
Source0: %{name}-%{version}.tar.gz

Requires: httpd %{php_package_name} %{php_package_name}-ldap %{php_package_name}-mysql %{php_package_name}-gd subversion subversion-perl mod_dav_svn mod_perl perl perl-LDAP perl-Config-Simple perl-URI perl-DBI perl-Digest-SHA1 perl-Mail-Sendmail
# %{php_package_name}-pecl-svn > 1.2.0
# %{php_package_name}-pgsql perl-DBD-Pg

BuildRequires: subversion-devel neon-devel %{php_package_name}-devel
BuildRoot: %{_tmppath}/%{name}-%{version}-root

%description
Codepot code repository manager

%package peclsvn
Summary: PHP Subversion extension
Group: Development/Languages
#BuildArch: %{_target_platform}
# %{PACKAGE_VERSION} is deprecated. %{Version} is supported in some
# recent distros. Never mind. Let's just resort to the autoconf replacement.
Requires: @PACKAGE_NAME@ = @PACKAGE_VERSION@
Conflicts: %{php_package_name}-pecl-svn

%description peclsvn
This package contains svn.so for PHP. You can install this package
if your system doesn't have a proper Subversion extenstion required by Codepot.
It is also safe to install this RPM package without Codepot by ignoring the
dependency.

%prep
%setup -q

%build
./configure \
	--prefix=/usr \
	--libdir=%{_libdir} \
	--sysconfdir=/etc \
	--with-wwwdir=/var/www/html/codepot \
	--with-cfgdir=/etc/codepot \
	--with-depotdir=/var/lib/codepot \
	--with-logdir=/var/log/codepot \
	--with-cachedir=/var/cache/codepot \
	--with-phpextdir=%{_libdir}/php/modules \
	--with-phpextinidir=%{_sysconfdir}/php.d
make

%install
make install DESTDIR=$RPM_BUILD_ROOT

%clean
rm -rf $RPM_BUILD_ROOT

%files
%defattr(-,root,root)

/var/www/html/codepot
/usr/sbin/codepot-user
/usr/sbin/codepot-mon-httpd

%config(noreplace) /etc/codepot/codepot.ini
/etc/codepot/codepot.mysql 
/etc/codepot/codepot.pgsql 
/etc/codepot/codepot.a2ldap
/etc/codepot/codepot.httpd
/etc/codepot/start-commit
/etc/codepot/pre-commit
/etc/codepot/post-commit
/etc/codepot/pre-revprop-change
/etc/codepot/post-revprop-change
/etc/codepot/cloc.pl
/etc/codepot/perl/Codepot/AccessHandler.pm
/etc/codepot/perl/Codepot/AuthenHandler.pm

%dir %attr(-,apache,apache) /var/lib/codepot/svnrepo
%dir %attr(-,apache,apache) /var/lib/codepot/files
%dir %attr(-,apache,apache) /var/lib/codepot/issuefiles
%dir %attr(-,apache,apache) /var/lib/codepot/attachments
%dir %attr(-,apache,apache) /var/lib/codepot/usericons
%dir %attr(-,apache,apache) /var/log/codepot
%dir %attr(-,apache,apache) /var/cache/codepot

%files peclsvn
%{_libdir}/php/modules/svn.so
%config(noreplace) %{_sysconfdir}/php.d/svn.ini

%pre
exit 0

%post
[ $1 -eq 1 ] && {
	# Fresh installation.

	# install the httpd configuration file for the first installation
	install -D -m 0644 %{_sysconfdir}/codepot/codepot.httpd %{_sysconfdir}/httpd/conf.d/codepot.conf

	# change the selinux context on data directories
	chcon -Rv --type=httpd_sys_content_t /var/lib/codepot >/dev/null 2>&1
	chcon -Rv --type=httpd_sys_content_t /var/cache/codepot >/dev/null 2>&1
}
exit 0

%preun
exit 0

%postun
[ $1 -eq 0 ] && {
	# Uninstallation
	rm -f %{_sysconfdir}/httpd/conf.d/codepot.conf	
}
exit 0
