# This hack is needed for building alternative platform databases
# on a single architecture using rpm's --target flag

%define _arch			%{_target_cpu}

%define realrpmdbepoch %{?rpmdbepoch}%{!?rpmdbepoch:0}
%define	rpmdbdistribution	Fedora Core %{rpmdbname}-%{rpmdbversion}
%define	rpmdbplatform		%{_arch}-%{_vendor}-%{_os}
%define	rpmdbpath		/usr/lib/rpmdb/%{rpmdbplatform}/redhat

Summary: The entire rpm database for the Fedora Core distribution.
Name: rpmdb-%{rpmdbname}
Epoch: %{realrpmdbepoch}
Version: %{rpmdbversion}
Release: 0.%(date +"%%Y%m%d")
Group: Development/System
Copyright: GPL
BuildRoot: /%{_tmppath}/%{name}-%{version}-%{release}-root
Obsoletes: rpmdb-redhat
Provides: rpmdb-redhat

%description
The %{name} package contains a copy of the rpm database for all packages
from the Fedora Core distribution.

%prep
[ -d %{rpmdbtree} ] || {
    echo "This specfile needs access to %{rpmdbtree}/%{_arch}/Fedora/RPMS"
    exit 1
}

%build

%install
rm -rf $RPM_BUILD_ROOT
mkdir -p ${RPM_BUILD_ROOT}%{rpmdbpath}
mkdir -p ${RPM_BUILD_ROOT}%{_sysconfdir}/rpm

cat > ${RPM_BUILD_ROOT}%{_sysconfdir}/rpm/macros.solve <<EOF
#	The path to the dependency universe database. The default value
#	is the rpmdb-fedora location. The macro is usually defined in
#	/etc/rpm/macros.solve, installed with the rpmdb-redhat package.
%_solve_dbpath %{rpmdbpath}

#       The path to the dependency universe packages. This should
#       be a path to the packages contained in the solve database.
%_solve_pkgsdir /mnt/redhat/test/latest-i386/Fedora/RPMS/

#	The output binary package file name template used when suggesting
#	binary packages that solve a dependency. The macro is usually defined
#	in /etc/rpm/macros.solve, installed with the rpmdb-redhat package.
#
# XXX	Note: escaped %%% for use in headerSprintf()
%_solve_name_fmt	%{?_solve_pkgsdir}%%%{NAME}-%%%{VERSION}-%%%{RELEASE}.%%%{ARCH}.rpm
EOF

MYRPM="rpm --dbpath ${RPM_BUILD_ROOT}%{rpmdbpath}"

%if %{_dbapi}
MYRPM="$MYRPM --define \"_dbapi %{_dbapi}\""
%endif

MYRPM="$MYRPM --define \"__dbi_htconfig hash nofsync %%{__dbi_other} %%{__dbi_perms}\""

eval "$MYRPM --initdb"

if [ -d %{rpmdbtree}/%{_arch}/Fedora/RPMS ]; then
 cd %{rpmdbtree}/%{_arch}/Fedora/RPMS
elif [ -d %{rpmdbtree}/%{_arch} ]; then
 cd %{rpmdbtree}/%{_arch} 
else
 exit 1
fi

eval "$MYRPM -i --justdb --nodeps --noorder --ignorearch --noscripts --notriggers --ignoresize --force *.rpm"
# Used to be *.{%{_arch},noarch}.rpm, but why not include them all? :)

rm -f $RPM_BUILD_ROOT%{rpmdbpath}/__db*


%clean
rm -rf $RPM_BUILD_ROOT

%post
rm -f %{rpmdbpath}/__db* > /dev/null 2>&1 || :

%preun
rm -f %{rpmdbpath}/__db* > /dev/null 2>&1 || :


%files
%defattr(0644,root,root,0755)
%{rpmdbpath}
%{_sysconfdir}/rpm/macros.solve

%changelog
* Wed Jan 15 2003 Tim Powers <timp@redhat.com>
- remove stale db lock files if they exist in %%post and %%preun

* Fri Sep 24 1999 Jeff Johnson <jbj@redhat.com>
- rpm db build needed --notriggers.

* Wed Sep 22 1999 Jeff Johnson <jbj@redhat.com>
- Create.
