#!/bin/sh
#
# $Id: install-additional-tools 1315 2007-04-16 14:13:59Z bruno $
#
########################################################################


Die() {
    echo "Fatal error! $1" >> /dev/stderr
    exit 1
}



# ------------ main -----------

# if the file '/tmp/CDROM-LIVES-HERE' exists then we should use the CD

LogIt "Starting install-additional-tools"
mountdir=$GROOVY
mkdir -p $mountdir
diskno=1
res=0
if [ -e "/tmp/TAPEDEV-HAS-DATA-DISKS" ] ; then
    LogIt "I am assuming the data disks' contents are already in $mountdir."
else
    while [ "$diskno" -lt "20" ] ; do
		LogIt "PLEASE WAIT. Untarring and softlinking disk #$diskno" 2
		untar-and-softlink $diskno $mountdir /
		res=$(($res+$?))
		[ -e "/LAST-DISK" ] && diskno=999
		diskno=$(($diskno+1))
		[ -e "/tmp/CDROM-LIVES-HERE" ] && [ ! -f "/mnt/cdrom/images/$diskno.tar.gz" ] && break
    done
fi
rm -f /LAST-DISK

echo -e -n "Recombining large files..."
biggienumber=0
while [ -e "$mountdir/slice-$biggienumber.name" ] ; do
    stub=$mountdir/slice-$biggienumber
    biggiefname=`cat $stub.name`
    biggiefsize=`cat $stub.size`
    rm -f $stub.name $stub.size
    > /tmp/out.dat
    sliceno=0
    while [ "$sliceno" -le "999" ] ; do
		slicefile=$stub.
        [ "$sliceno" -lt "100" ] && slicefile="$slicefile"0
		[ "$sliceno" -lt "10" ]  && slicefile="$slicefile"0
		slicefile="$slicefile"$sliceno
		[ ! -e "$slicefile" ] && break
		cat $slicefile >> /tmp/out.dat
		rm -f $slicefile
		sliceno=$(($sliceno+1))
    	done

    echo "$biggiefname ($biggiefsize KB) restored. $sliceno slices." >> $LOGFILE

    mkdir -p $biggiefname
    rmdir $biggiefname 2> /dev/null
    chmod +x /tmp/out.dat
    cp -f /tmp/out.dat $biggiefname
    rm -f /tmp/out.dat
    biggienumber=$(($biggienumber+1))
    echo -e -n "."
done
echo -en "Done.                                                              \r"
rm -f /slice*

LogIt "Resolving softlinks"
echo -en "Finalizing softlinks..."

cd $mountdir
for path in `find | grep -v /etc/fstab` ; do
    if [ -d "$path" ] ; then
		mkdir /$path 2> /dev/null
    else
# Patch from Andrew Bulhak
        if [ -h "/$path" ] ; then
            # echo "removing symlink /$path"
            rm -f /$path
        fi
# end patch
		mv -f $path /$path
    fi
done
cp -f etc/fstab /tmp/fstab
tar cf - sbin bin usr/bin usr/sbin lib usr/lib lib64 usr/lib64 | (cd / ; tar xf -)

cd /

for i in modprobe lsmod depmod rmmod ; do
    [ -e "$mountdir/sbin/$i" ] && ln -sf $mountdir/sbin/$i /sbin/$i
done

sync;sync;sync

echo "Done."

for j in io.sys msdos.sys embleer.B.bz2 embleer.C.bz2 ; do
    [ -e "$mountdir/$j" ] && ln -sf $mountdir/$j /$j
done
[ -e "$mountdir/etc/fstab" ] && ln -sf $mountdir/etc/fstab /tmp/fstab
[ -e "$mountdir/etc/raidtab" ] && ln -sf $mountdir/etc/raidtab /tmp/raidtab

which gawk 2> /dev/null > /dev/null
[ "$?" -ne "0" ] && which awk 2> /dev/null > /dev/null && ln -sf `which awk` /usr/bin/gawk

LogIt "Exiting install-additional-tools"
exit 0

# exit $res

