#!/bin/sh

LIBS="libc libext2fs.so libuuid libnss_files libe2p libtermcap libcom_err
      libnsl libnss_files libnss_dns libproc libdl libcrypt libdb"
USRLIBS="libz"
BINS="bzip2 ls mke2fs ash open fdisk badblocks rm chmod cat lsmod
      mkdir mknod mount rm umount ln cpio rpm.shared vi tar cp ifconfig route
      e2fsck grep sed gzip dd cpio mt mv ping traceroute sync chroot restore
      head tail swapon rpm ps tac pico vim df prtconf doexec uname basename"
ZBINS="rpm vim e2fsck pico"
DEVS="tty1 tty2 console null systty zero openprom"
DEVSALL="hda hdb hdc hdd sda sdb sdc sde sdf sdg scd ram"
USRLIB="rpm/rpmrc rpm/rpmpopt"
ETC="services protocols"

usage () {
    echo "updtftprescue takes no parameters" 2>&1
    exit 1
}

if [ -n "$1" ]; then
    usage
fi

# Do not delete tftprescue/modules
rm -rf tftprescue/[a-ln-z]* tftprescue/mnt
mkdir -p tftprescue/lib 	tftprescue/usr 		tftprescue/bin 	\
	 tftprescue/etc 	tftprescue/usr/lib 	tftprescue/mnt 	\
	 tftprescue/mnt/image	tftprescue/dev 		tftprescue/tmp 	\
	 tftprescue/mnt/floppy	tftprescue/proc		tftprescue/modules
ln -s bin tftprescue/sbin
ln -s ../bin tftprescue/usr/bin
ln -s ../bin tftprescue/usr/sbin
ln -s gzip tftprescue/bin/gunzip
ln -s swapon tftprescue/bin/swapoff
# bash is too large on SPARC
ln -s ash tftprescue/bin/sh

cp -a /lib/ld-2*.so tftprescue/lib
cp -a /lib/ld-linux.so.2* tftprescue/lib

cp -a /sbin/insmod64 tftprescue/sbin/insmod64
cp -a /sbin/insmod tftprescue/sbin/insmod32
cp -a /sbin/rmmod tftprescue/sbin/rmmod

cp -a ../install/pci-probing/test-pciprobe tftprescue/sbin/pciprobe
cp -a ../install/sbus-probing/test-sbusprobe tftprescue/sbin/sbusprobe
strip tftprescue/sbin/pciprobe
strip tftprescue/sbin/sbusprobe

for n in $ETC; do
	cp -a /etc/$n tftprescue/etc
done

echo "Copying libraries"

for n in $LIBS; do
	# this preserves hard links
	(cd /lib; tar cSpf - ${n}[-.]*) | (cd tftprescue/lib; tar xSpf -)
done

for n in $USRLIBS; do
	# this preserves hard links
	(cd /usr/lib; tar cSpf - ${n}[-.]*) | (cd tftprescue/lib; tar xSpf -)
done

rm -f tftprescue/lib/libdl*.so.1* tftprescue/lib/libz.{a,so}

echo "Stripping libraries"
(cd tftprescue/lib; file * | grep 'shared object' | cut -d: -f1 | xargs strip)

echo "Copying binaries"

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

echo "Copying devices"

for n in $DEVSALL; do
	for d in /dev/${n}*; do
	    cp -af $d tftprescue/dev
	done
done

for n in $DEVS; do
	for d in /dev/${n}; do
	    cp -af $d tftprescue/dev
	done
done

mv -f tftprescue/bin/rpm.shared tftprescue/bin/rpm
strip tftprescue/bin/rpm

mkdir -p tftprescue/usr/lib/rpm
for n in $USRLIB; do
	cp -af /usr/lib/$n tftprescue/usr/lib/rpm/
done

for n in $ZBINS; do
	bzip2 -c tftprescue/bin/$n > tftprescue/bin/$n.bz2
	cat > tftprescue/bin/$n <<-EOF
		#!/bin/sh
		bzip2 -dc /usr/bin/$n.bz2 > /tmp/$n
		chmod 755 /tmp/$n
		/tmp/$n \$*
		rm -f /tmp/$n
	EOF
	chmod 755 tftprescue/bin/$n
done

cat >> tftprescue/etc/fstab <<EOF
/dev/ram	/		ext2	defaults
/proc		/proc		proc	defaults
/dev/fd0	/mnt/floppy	ext2	defaults
EOF

touch tftprescue/etc/mtab

echo 'root::0:0:The Mighty:/:/bin/sh' > tftprescue/etc/passwd

cat >> tftprescue/sbin/init <<EOF
#!/bin/sh

mount -n -o remount,rw /
mount /proc
cp /proc/mounts /etc/mtab

open -l /bin/sh

echo 'Probing for PCI and SBUS adapters in the system.'
echo ''
insmod openprom
[ -f /proc/pci ] && pciprobe
sbusprobe
[ -f /proc/pci ] && MODS=\`pciprobe | sed -n -e '1,/^Probing for VIDEO/s/^.*-> |\(.*\)|/\1/p'\`
MODS="\$MODS \`sbusprobe | sed -n -e '1,/^Probing for VIDEO/s/^.*-> |\(.*\)|/\1/p'\`"
if echo \$MODS | grep [a-z] > /dev/null 2>&1; then
    echo 'Autodetect has found it could be desirable to insmod these modules:'
    echo "\$MODS"
    echo 'Please use insmod for these modules if you need to, and choose'
    echo 'your order of module loads.'
else
    echo 'Autodetect has not found any devices for which it would be desirable'
    echo 'to load their modules.'
fi

echo ''
echo 'This is the UltraPenguin network tftprescue set. Most of the basic system commands are'
echo 'in /bin.'
echo ''
echo 'Type exit to halt the system.'
echo ''

/bin/sh

echo "Unmounting filesystems..."
tac /proc/mounts | while read line ; do
    set \$line
    if [ \$2 != "/" ]; then
	umount \$1
    fi
done
echo "Halting system.. All fixed? :-)"
EOF

chmod +x tftprescue/sbin/init

cat >> tftprescue/sbin/insmod <<EOF
#!/bin/sh

ARCH=\`uname -m\`
if [ "\$ARCH" = "sparc" ]; then
    MODULES=/modules/modules.cbz2
    INSMOD=insmod32
else
    MODULES=/modules/modules64.cbz2
    INSMOD=insmod64
fi

MOD=\`basename \$1 .o\`.o

cd /tmp
cat \$MODULES | bzip2 -d | cpio -i --quiet \$MOD
if [ -f \$MOD ]; then
    doexec \$INSMOD insmod /tmp/\$MOD
    rm -f /tmp/\$MOD
else
    echo "Module \$MOD not found."
fi

EOF

chmod +x tftprescue/sbin/insmod

mkdir -p tftprescue/usr/share/terminfo/l
cp /usr/share/terminfo/l/linux tftprescue/usr/share/terminfo/l

infocmp linux > foo
infotocap -C foo > tftprescue/etc/termcap
rm -f foo
