#!/bin/sh

ulimit -S -c unlimited
dbg=	# echo

rpm=/bin/rpm
#rpm=/X/src/rpm/rpm
#rpmi=mrpm
#rpmi="strace -o xxx /bin/rpm"
rpmi="$rpm"
#rpmi=/usr/lib/rpm/rpmi
rpmb=/usr/bin/rpmbuild
rpmq=/usr/bin/rpmquery
rpmdb=/usr/bin/rpmdb
rpmk=/usr/bin/rpmsign

pgppubkey="`echo /usr/share/doc/rpm-4*/RPM-PGP-KEY`"
gpgpubkey="`echo /usr/share/doc/rpm-4*/RPM-GPG-KEY`"
betapubkey="`echo /usr/share/doc/rpm-4*/BETA-GPG-KEY`"

#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=8.0
type=min
arch=`$rpm --eval '%{_arch}' | sed -e 's/i686/i386/'`

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-
coreutils-
db1-1
db2-2
db3-3
db4-4
dev-
diffutils-
e2fsprogs-1
fileutils-
findutils-
gawk-
gdb-
gdbm-1
grep-
gzip-
info-
less-
libacl-2
libattr-2
mount-
ncurses-[45]
pcre-
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-
bdflush-
console-tools-
cyrus-sasl-[12]
cyrus-sasl-md5-
elfutils-libelf-
glib2-2
gmp-[234]
initscripts-
iputils-
iproute-
kbd-
kernel-2
krb5-libs-
libuser-
libelf-*
losetup-
lvm-
mkinitrd-
modutils-
net-tools-
openldap-2
openssl-0
pam-0
pamconfig-
redhat-release-
sh-utils-
sysklogd-
usermode-1
util-linux-
vixie-cron-
rpm-[34]
rpm-devel-[34]
sash-
SysVinit-
"

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

buildglob="
$develglob
autoconf-
automake-
bash2-
binutils-
cpp-
cvs-
db3-utils-
db4-utils-
egcs-1
egcs-c++-1
expat-1
gcc-[23]
gcc-c++-[23]
gettext-
krb5-configs-
libgcc-3
libgcj-3
libstdc++-
libtool-
make-
m4-
ncurses-devel-
patch-
perl-5
perl-CGI-
perl-Filter-
perl-Parse-RecDescent-
python-[12]
rpm-build-
file-3
tcsh-
zip-
"

mozillaglob="
$buildglob
compat-libstdc++-7
curl-
fontconfig-2
freetype-2
gtk+-1
indexhtml-
libao-0
libjpeg-
libpng-
libogg-
libvorbis-
mozilla-
ORBit-0
redhat-menus-
vorbis-tools-
XFree86-Mesa-libGL-
XFree86-libs-[34]
XFree86-libs-data-
"

classes="min0 min1 min2 base devel build mozilla"
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.*|8.*)
	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
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 mkdir -p $root/etc
	$dbg $sudo touch $root/etc/fstab
	$dbg $sudo touch $root/etc/mtab
	$dbg $sudo $rpmdb $rc --root $root --initdb
	if [ X"`$rpm --version`" = X"RPM version 4.2" ]; then
	    $dbg $sudo $rpmk $rc --root $root --import $pgppubkey
	    $dbg $sudo $rpmk $rc --root $root --import $gpgpubkey
	    $dbg $sudo $rpmk $rc --root $root --import $betapubkey
	fi
	;;
    --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`"
