#!/bin/sh
cd /
echo  
echo "Extracting filesystem: "

#
# fixing CRLF-stuff
#
cat /mnt/tmp/config |  tr -d '\015' > /etc/config

. /etc/config

if [ ${ONLY_8M} = 'y' ];
then 
	BZIP=-s
else
	BZIP=
fi

bzip2 ${BZIP} -d < /mnt/tmp/floppyfw/add.bz2 | tar -x
#
# Kernel modules (contributed by: dr. Jones) 
#
echo
echo -n "Extracting kernel modules: "
for i in /mnt/tmp/floppyfw/mod*.bz2; do
	if test -f $i
	then
		echo -n "$i "
		#/lib/modules/`uname -r`
		bzip2 ${BZIP} -d < $i | tar -x
	fi
done

echo
echo "Copying configuration."
#cp /tmp/config /etc/config
cat /mnt/tmp/hosts |  tr -d '\015' > /etc/hosts
cat /mnt/tmp/modules.lst |  tr -d '\015' > /etc/modules.lst

cat /mnt/tmp/network.ini |  tr -d '\015' > /etc/network.init
chmod 777 /etc/network.init

cat /mnt/tmp/firewall.ini |  tr -d '\015' > /etc/firewall.init
chmod 777 /etc/firewall.init

#cat /mnt/tmp/syslog.cfg |  tr -d '\015' > /etc/syslog.conf

cp /mnt/tmp/licenses/* /licenses


#
# Packages (contributed by: t'Sade) 
#
echo -n "Looking for extra packages: "
for i in /mnt/tmp/packages/*.bz2; do
	if test -f $i
	then
		echo -n "$i "
		bzip2 ${BZIP} -d < $i | tar -x
	fi
done
echo

echo "Looking for pre- scripts..."

for i in /mnt/tmp/packages/pre-*.ini; do
	if test -f $i
	then
		PRE=${i#/mnt/tmp/packages}
		cat $i | tr -d '\015' > /etc/$PRE
		echo $PRE
		chmod 755 /etc/$PRE
		/etc/$PRE
	fi
done
echo "...scripts done." 


echo "Adding modules: "
cp /mnt/tmp/modules/* /lib/modules 2> /dev/null

cat /etc/modules.lst | while read myline
 do
   echo ${myline}  #or somethine else...
   insmod /lib/modules/${myline}
 done

echo
echo "Initializing network: "
sh /etc/network.init
echo 

if [ ${USE_SYSLOG} = 'y' ]; 
then
       echo "Starting klogd and syslogd"
       echo -n "klogd "
       klogd
       echo "syslogd"
       syslogd ${SYSLOG_FLAGS}
fi

echo -n "Detecting external IP.. "
EXTERNAL="`gi ${OUTSIDE_DEV}`"
echo "IP Found, ${EXTERNAL}"

echo "Setting up firewall rules: "
sh /etc/firewall.init ${EXTERNAL}
echo 

echo "Looking for post- scripts..."
for i in /mnt/tmp/packages/post-*.ini; do
	if test -f $i
	then
		POST=${i#/mnt/tmp/packages}
		cat $i | tr -d '\015' > /etc/$POST
		echo $POST
		chmod 755 /etc/$POST
		/etc/$POST
	fi
done
echo "...scripts done." 

umount /mnt/tmp
rm -r /mnt/tmp

if [ ${OPEN_SHELL} = 'y' ];
then
	echo "Opening Virtual Consoles: "
	#
	open -c 1 -- ash 
	open -c 2 -- ash 
	open -c 3 -- ash 
	open -c 4 -- ash 
	open -c 5 -- ash 
	open -c 6 -- ash 
	open -c 7 -- ash 
else
	echo "No shell executed"
fi
exec sleep 10000000d
