#!/bin/sh

LIBS="ld-linux.so libc.so libcom_err.so libdl.so libe2p.so libext2fs.so libuuid"
#LIBS="ld-linux.so libc.so libcom_err.so libdl.so libe2p.so libext2fs.so libm.so libtermcap.so"
USRLIBS="libnewt.so rpmrc"
BINS="gzip ls mke2fs ash cat rm open fdisk badblocks df"
BOOT="first.b second.b"

usage () {
    echo "updboots: <imagetree> <kernel-version>" 2>&1
    exit 1
}

if [ -z "$1" -o -z "$2" ]; then
    usage
fi

TREE=$1

if [ ! -d "$TREE/misc/src/install" ]; then
    echo "$TREE is not a valid directory" 2>&1
    usage
fi

if [ ! -f "/boot/vmlinux-$2.gz" ]; then
    echo "/boot/vmlinux-$2.gz does not exist"
    usage
fi

PATH=/usr/sbin:/usr/bin:/bin:/sbin

rm -f boot2/lib/*
for n in $LIBS; do
	# this preserves hard links
	(cd /lib; tar cSpf - ${n}*) | (cd boot2/lib; tar xSpf -)
done

for n in $USRLIBS; do
	rm boot2/usr/lib/${n}*
done
for n in $USRLIBS; do
	cp -a /usr/lib/${n}* boot2/usr/lib
done

for n in boot2/lib/* boot2/usr/lib/*; do
	[ ! -L $n ] && strip $n
done

for n in $BINS; do
	rm -f boot2/usr/bin/$n
	cp -af `which $n` boot2/usr/bin
	strip boot2/usr/bin/$n
done

rm -f boot1/boot/*
for n in $BOOT; do
	cp -a /boot/$n boot1/boot
done


strip $TREE/misc/src/install/install
strip $TREE/misc/src/install/install2
strip $TREE/misc/src/init/init
cp -a $TREE/misc/src/init/init boot2/sbin/init
cp -a $TREE/misc/src/install/install boot2/bin/install
cp -a $TREE/misc/src/install/install2 boot2/usr/bin/install2

ln -sf /proc/mounts boot2/etc/mtab

cp -f "/boot/vmlinux-$2.gz" boot1/vmlinux.gz
cp -f "/boot/System.map-$2" tftp/System.map
