#!/bin/sh
#
# $Id: install-additional-tools 1770 2007-11-06 10:01:53Z 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
# Should be the same as in mindi => conf param
MAX_DISKS=99
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 "$MAX_DISKS" ] ; do
		LogIt "PLEASE WAIT. Untarring disk #$diskno" 2
		untar $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

LogIt "Recombining large files..." 1
biggienumber=0
while [ -e "$mountdir/slice-$biggienumber.name" ] ; do
    stub=$mountdir/slice-$biggienumber
    biggiefname=`cat $stub.name`
	if [ -f $biggiefname ]; then
		LogIt "$biggiefname already exists - skipped" 1
    	biggienumber=$(($biggienumber+1))
		continue
	fi
    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

    LogIt "$biggiefname ($biggiefsize KB) restored. $sliceno slices." 1

    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))
done
rm -f /slice*

LogIt "Populating / with tar file content..." 1

cd $mountdir
if [ -f etc/fstab ]; then
	mv etc/fstab /tmp
fi
if [ -f etc/raidtab ]; then
	mv etc/raidtab /tmp
fi

liste=`ls`
if [ "$liste" ]; then
	tar cf - $liste | (cd / ; tar xf -)
	rm -fr $liste
fi
cd /

sync;sync;sync

LogIt "Done." 1

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

