#
# The macro rpmdbname should be
#	"redhat" for the distribution cdrom (note: not the vendor!)
#	"powertools" for the powertools cdrom
#	etc
# You'll need to check the other macros too.
#
#
#%dontdefine	rpmdbname		redhat
#%dontdefine	rpmdbversion	        6.1
#%dontdefine	rpmdbtree	        /mnt/somewhere
%define default			SL	
%define rpmdbname Fermi
%define rpmmarketingname Scientific Linux (Fermi)
%define	rpmdbversion            309
%define rpmdbtree /mnt/src/lts30rolling
# This hack is needed for building alternative platform databases
# on a single architecture using rpm's --target flag

%define _arch			%{_target_cpu}

%define	rpmdbdistribution	%{rpmmarketingname} %{rpmdbname}-%{rpmdbversion}
%define	rpmdbplatform		%{_arch}-%{_vendor}-%{_os}
%define	rpmdbpath		/usr/lib/rpmdb/%{rpmdbplatform}/%{rpmdbname}
Summary: The entire rpm database for the %{rpmmarketingname} distribution.
Name: rpmdb-%{rpmdbname}
Version: %{rpmdbversion}
Release: 0.%(date +"%%Y%m%d")
Group: Development/System
Copyright: GPL
BuildRoot: /%{_tmppath}/%{name}-%{version}-%{release}-root

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

%prep
[ -d %{rpmdbtree} ] || {
    echo "This specfile needs access to %{rpmdbtree}/%{_arch}/sites/%{rpmdbname}/Updates"
    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-redhat location. The macro is usually defined in
#	/etc/rpm/macros.solve, installed with the rpmdb-redhat package.
%_solve_dbpath /usr/lib/rpmdb/%{_arch}-%{_vendor}-%{_os}/%{rpmdbname}

#       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-%_{arch}/%{rpmdbname}/Updates/

#	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}/sites/%{rpmdbname}/Updates ]; then
 cd %{rpmdbtree}/%{_arch}/sites/%{rpmdbname}/Updates
elif [ -d %{rpmdbtree}/%{_arch} ]; then
 cd %{rpmdbtree}/%{_arch} 
else
 exit 1
fi

eval "$MYRPM -i --justdb --nodeps --noorder --ignorearch --noscripts --notriggers --ignoresize --force %{rpmdbtree}/%{_arch}/%{default}/RPMS/*{%{_arch},noarch}.rpm %{rpmdbtree}/%{_arch}/sites/%{rpmdbname}/Updates/*{%{_arch},noarch}.rpm"

%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 Apr 7 2004 Connie Sieh <csieh@fnal.gov>
- remove stale db lock files if they exist in %%post and %%preun

* 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.