#!/bin/sh

ORIG=/mnt/maragda
MODULES_ORIG=/mnt/maragda/lib/modules
MODULES_VERSION=2.2.18pre21 
DEST=tmp_root
FINAL_DEST=mnt_root
#SIZE=18 (it fits into 18 Mb but you must allow some extra space for running processes)
SIZE=25

#------------------------------------------------
umount $FINAL_DEST 2> /dev/null

echo "* creating ROOT.FS"
./Make_fs $SIZE Mb

mv ${SIZE}Mb.fs ROOT.FS

echo "* mounting it"
mount ROOT.FS $FINAL_DEST -o loop
rm -rf $FINAL_DEST/*

echo "* cleaning temporal root directory"
rm -rf $DEST/*

#------------------------------------------------

# bin,boot,dev,etc,home,lib,mnt,opt,proc,root,sbin,tmp,usr,var
echo "* copying directories"

tar --create  --directory $ORIG --exclude var/lib --exclude lib/modules --file - bin sbin boot dev etc home lib mnt proc root tmp var | tar --extract --directory $DEST --same-permissions --file - 

echo "* creating symbolink links for the _big_ directories (opt usr var/lib)"
for i in  opt usr var/lib
do
	(cd $DEST ; ln -s mnt/whole_fs/$i $i)
done

echo "* making initrd directory, where the first boot root file system will be available"
(cd $DEST ; mkdir initrd)

cp -a /dev/initrd $ORIG/dev

echo "* creating mnt sub-directories: may be completed"
mkdir $DEST/mnt/source
mkdir $DEST/mnt/whole_fs
mkdir $DEST/mnt/floppy
mkdir $DEST//floppy
mkdir $DEST/mnt/cdrom
mkdir $DEST/cdrom
mkdir $DEST/mnt/paride
mkdir $DEST/mnt/mnt1
mkdir $DEST/mnt/mnt2
mkdir $DEST/mnt/mnt3
mkdir $DEST/mnt/mnt4

echo "* unstrip-ing"
./unstrip-lib $DEST/lib
./unstrip-bin $DEST/bin
./unstrip-bin $DEST/sbin

echo "* copying /lib/modules (for this kernel version ${MODULES_VERSION})"
mkdir $DEST/lib/modules
cp -a ${MODULES_ORIG}/${MODULES_VERSION} $DEST/lib/modules/$MODULES_VERSION

echo "* copying configuration files (files under config)"
cp -f -a config/* $DEST

echo "*cleaning some log dirs (var/log)"
for i in `find $DEST/var/log -type f`
do
	> $i
done

echo "BEFORE FINAL COPY (pres enter to continue)"
read avant

#------------------------------------------------

echo "* final copy "
cp -a $DEST/* $FINAL_DEST
echo "(if it does not fit, try to clean config/home/jordi)"

echo "* cleaning temporal root directory"
rm -rf $DEST/*

echo "DONE: umounting"
umount $FINAL_DEST

exit 0

#  ==========================================================
# cal arreglar  
	/bin
	/lib/modules
	/mnt
	/etc/fstab

