#!/bin/sh
#
#	Prepare the RootFS for later use. Depending on
#	the chosen target device type, do some of the following:
#	* Create initial set of device nodes.
#	* Recursively change owner to root.
#	* Copy kernel to flash.
#

MYTARGET=@@TARGET_FS@@

ME="`basename $0`"
ID=`id -u`

makenode() { # (name,type,major,minor)
	if [ -c $1 ]; then
		echo "skipping existing file \`$1'"
	elif ! mknod $1 $2 $3 $4; then
		echo "error creating file \`$1', aborting!"
		exit 1
	fi
}
help_quit() { # ()
	echo "Usage:"
	echo "$ME"
	echo
	echo "Prepare the RootFS right after installation for later use."
	echo "If you want to know better, look at the source."
	exit 1
}

[ $# -gt 0 ] && { echo "Too many arguments!"; help_quit;}
[ "$ID" -ne 0 ] && { echo "You need to be root to run me!"; help_quit;}

# check for myself in current dir
if [ ! -f "${PWD}/$ME" ]; then
	cd `dirname $0`
fi

case $MYTARGET in

ext2-cf|nfs)
echo "creating device nodes"
makenode dev/null c 1 3
makenode dev/tty c 5 0
makenode dev/console c 5 1
makenode dev/cfa b 13 0
makenode dev/cfa1 b 13 1
makenode dev/cfa2 b 13 2
;;

yaffs2)
echo "copying over device nodes"
cp -dpR /dev/null /dev/tty /dev/console /dev/mtd* dev/
echo "copying over kernel"
if [ -b /dev/mtdblock0 ]; then
	mount -t yaffs2 /dev/mtdblock0 mnt/
elif [ -b /dev/mtdblock/0 ]; then
	mount -t yaffs2 /dev/mtdblock/0 mnt/
else
	echo "device node for first flash partition not found!"
	exit 1
fi
cp kernel mnt/kernel
umount mnt/
;;

*)
echo "something is really going wrong here!"
;;

esac

echo "recursively fixing ownership"
chown -R root:root *

exit 0

