#!/bin/sh

dbg=	# echo

rpm=/bin/rpm
#rpmi=mrpm
rpmi=/bin/rpm
#rpmi="strace -o xxx /bin/rpm"
rpmb=/usr/bin/rpmbuild
rpmq=/usr/bin/rpmquery
rpmdb=/usr/bin/rpmdb

#rpm=/X/src/rpm402/rpm
#rpmb=/X/src/rpm402/rpmb
#rpmq=/X/src/rpm402/rpmq
#rpmdb=/X/src/rpm402/rpmdb

#rpm=/X/src/rpm307/rpm
#rpmb=$rpm
#rpmq=$rpm
#rpmdb=$rpm

dist=7.0
type=min
arch=`$rpm --eval '%{_arch}'`

top=`pwd`
root=$top/$dist
sudo=sudo
justdb="--justdb --noscripts --notriggers --ignoresize"
dmopts="-l dmalloc.log -i 100 low"
db1=
rc="--rcfile $top/rpmrc-$dist"
sorted=	# "| sort"

min0glob="
basesystem-
bash-[12]
filesystem-
glibc-common-
glibc-2
ldconfig-
libtermcap-2
mktemp-
setup-
termcap-
"

min1glob="
$min0glob
bzip2-[01]
bzip2-libs-
chkconfig-
db1-1
db2-2
db3-3
dev-
diffutils-
e2fsprogs-1
fileutils-
findutils-
gawk-
gdb-
gdbm-1
grep-
gzip-
info-
mount-
ncurses-[45]
procps-2
psmisc-
sed-
shadow-utils-
readline-[24]
rmt-
tar-
textutils-
vim-common-
vim-minimal-
zlib-1
"

min2glob="
$min1glob
cpio-
cracklib-
glib-1
logrotate-
mingetty-
popt-
pwdb-
slang-[01]
which-
words-
"

baseglob="
$min2glob
ash-
console-tools-
gmp-[23]
initscripts-
iputils-
iproute-
kernel-2
losetup-
mkinitrd-
modutils-
pam-0
pamconfig-
sh-utils-
sysklogd-
util-linux-
vixie-cron-
rpm-[34]
rpm-devel-[34]
sash-
SysVinit-
"

develglob="
$baseglob
bzip2-devel-
db1-devel-
db2-devel-
db3-devel-
gdbm-devel-
glibc-devel-
gmp-devel-
kernel-headers-
ncurses-devel-
readline-devel-[24]
tcl-
zlib-devel-
"

buildglob="
$develglob
autoconf-
automake-
bash2-
binutils-
cpp-
cvs-
db3-utils-
egcs-1
egcs-c++-1
gcc-2
gcc-c++-2
gettext-
krb5-configs-
krb5-libs-
libstdc++-
libtool-
make-
m4-
openssl-0
patch-
perl-5
python-1
rpm-build-
tcsh-
"
classes="min0 min1 min2 base devel build"
notallpat="(kernel-[^2h])"

for cmd in $*
do
    [ $# -eq 0 ] && break
    shift
    echo "******** args: $*"
    echo "=======> start $cmd	`date`"
    case $cmd in
    debug)		dbg=echo	;;
    dmalloc)		eval `dmalloc -b $dmopts`	;;
    db1)		db1="--define '%_dbapi 1'"	;;
    5.2|6.2|7.0|7.1|7.2)
	dist=$cmd
	root=$top/$dist
	rc="--rcfile $top/rpmrc-$dist $db1"
	$dbg mkdir -p $root/{dev,X/RPMS,X/SRPMS}
	[ -c $root/dev/null ] || $dbg $sudo mknod $root/dev/null c 1 3
	[ -f $top/rpmrc-$dist ] || cat << EOF > $top/rpmrc-$dist
include:    /usr/lib/rpm/rpmrc
macrofiles: /usr/lib/rpm/macros:/usr/lib/rpm/%{_target}/macros:$top/macros:$top/macros-$dist-$arch
EOF
	[ -f $top/macros-$dist-$arch ] || cat << EOF > $top/macros-$dist-$arch
%_topdir	$root/X
EOF
	;;
    config)
	[ -f $top/macros ] || cat << EOF > $top/macros
%_ntopdir	%{_topdir}/%{name}-%{version}-%{release}
%_builddir	%{_ntopdir}
%_sourcedir	%{_ntopdir}
%_specdir	%{_ntopdir}
%_rpmfilename	%%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm
%_rpmdir	%{_topdir}/RPMS
%_srcrpmdir	%{_topdir}/SRPMS
%_tmppath	%{_topdir}/tmp
EOF
	;;
    manifest)
	rm -f *-$dist-$arch
	pkgs="/$dist/$arch"
 	ls -1 ${pkgs}/*.rpm | egrep "(noarch|$arch).rpm" > list-$dist-$arch
	cat list-$dist-$arch | egrep -v $notallpat > all-$dist-$arch
	$dbg $rpmq $rc -qp --qf "/$dist/SRPMS/%{sourcerpm}\\n" \
`cat all-$dist-$arch` | sort | uniq > all-$dist-$arch-srpms
	for l in $classes ; do
	    glv='$'${l}glob
	    glist="`eval echo $glv`"
	    for g in $glist ; do
		ls -1 ${pkgs}/${g}*.rpm 2> /dev/null
	    done | egrep "(noarch|$arch).rpm" $sorted > $l-$dist-$arch
	    $dbg $rpmq $rc -qp --qf "/$dist/SRPMS/%{sourcerpm}\\n" \
`cat $l-$dist-$arch` | sort | uniq > $l-$dist-$arch-srpms
	done
	;;
    clean)
	$dbg rm -f $top/{rpmrc-$dist,macros-$dist-$arch,macros}
	$dbg $sudo mv $root ${root}-$$ && $dbg $sudo rm -rf ${root}-$$
	;;
    --rebuild)
	for srpm in `cat $type-$dist-$arch-srpms` ; do
	    $dbg $rpmb $rc $cmd $srpm
	done
	;;
    --initdb)
	$dbg $sudo mkdir -p $root/var/lib/rpm
	$dbg $sudo $rpmdb $rc --root $root --initdb
	;;
    --savedb)
	( $dbg cd $root/var/lib && $dbg $sudo tar czvf rpmdb.tar.gz rpm ; )
	;;
    --rebuilddb)
	$dbg $sudo $rpmdb $rc --root $root --rebuilddb -vv
	;;
    --justdb)
	[ -f $type-$dist-$arch ] || {
	    echo $type-$dist-$arch not found
	    exit 1
	}
	$dbg $sudo $rpmi $rc --root $root -Uv $justdb `cat $type-$dist-$arch`
	;;
    -U*|-F*)
	[ -f $type-$dist-$arch ] || {
	    echo $type-$dist-$arch not found
	    exit 1
	}
	$dbg $sudo $rpmi $rc --root $root $cmd $* `cat $type-$dist-$arch`
	set ""
	;;
    -[Vqei]*|--showrc)
	$dbg $sudo $rpm $rc --root $root $cmd
	;;
    chroot)
	$dbg $sudo touch $root/etc/mtab
	$dbg $sudo cp /etc/resolv.conf /etc/fstab $root/etc
	$dbg $sudo env HOME=/root chroot $root
	;;
    *)		type=$cmd	;;
    esac
done
echo "=======> finish	`date`"
