#!/bin/sh
for i in $*; do
	echo $i
        eval $i
done

if [ -f $USERS_DIR/.config ]; then
. $USERS_DIR/.config
fi

if [ -f $LINUX_DIR/.config ]; then
. $LINUX_DIR/.config
fi

#if RAMFS no need to generate rootfs
if [ "$CONFIG_BLK_DEV_INITRD" = "y" ]; then
	exit 0
fi

RAMFSDIR=$PWD/romfs
MKSQUASHFS=$USERS_DIR/squashfs4.0/squashfs-tools/mksquashfs
#MKSQUASHFS_LZMA=$USERS_DIR/squashfs4.0/squashfs-tools/mksquashfs-lzma
CVIMG=$USERS_DIR/goahead-2.1.1/LINUX/cvimg
STRIPTOOL=$TOOLCHAIN_DIR/bin/rsdk-linux-strip
SSTRIP=$TOOLCHAIN_DIR/bin/rsdk-linux-sstrip

if [ "$CONFIG_RTL_FLASH_MAPPING_ENABLE" = "y" ]; then
if [ -n $CONFIG_RTL_ROOT_IMAGE_OFFSET ]; then
	ROOTFS_OFFSET=$CONFIG_RTL_ROOT_IMAGE_OFFSET
	FLASH_SIZE=$CONFIG_RTL_FLASH_SIZE
	MAX_ROOTFS_SIZE=$(($(($CONFIG_RTL_JFFS2_IMAGE_OFFSET))-$(($ROOTFS_OFFSET))))
else
	ROOTFS_OFFSET=0xF0000	
fi
else
	ROOTFS_OFFSET=0xF0000	
fi


#build symbol link for tmp
#rm -rf $RAMFSDIR/tmp
#ln -s /var/tmp $RAMFSDIR/tmp

#DEVICES="ttyS0,c,4,64	console,c,4,64  null,c,1,3	mtdblock0,b,31,0"
# filename type mode uid gid major minor
echo "/dev/ttyS0     c 666 root root   4 64" > squashfs-pf-list.txt
echo "/dev/console   c 666 root root   4 64" >> squashfs-pf-list.txt
echo "/dev/null      c 666 root root   1  3" >> squashfs-pf-list.txt
echo "/dev/mtdblock0 b 666 root root  31  0" >> squashfs-pf-list.txt


#DEVICES="$DEVICES  ttyS1,c,4,65   ppp,c,108,0 	ptyp0,c,2,0
#	ptyp1,c,2,1	ttyp0,c,3,0	ttyp1,c,3,1	urandom,c,1,9"
echo "/dev/ttyS1     c 666 root root   4 65" >> squashfs-pf-list.txt
echo "/dev/ppp       c 666 root root 108  0" >> squashfs-pf-list.txt
echo "/dev/ptyp0     c 666 root root   2  0" >> squashfs-pf-list.txt
echo "/dev/ptyp1     c 666 root root   2  1" >> squashfs-pf-list.txt
echo "/dev/ttyp0     c 666 root root   3  0" >> squashfs-pf-list.txt
echo "/dev/ttyp1     c 666 root root   3  1" >> squashfs-pf-list.txt
echo "/dev/urandom   c 666 root root   1  9" >> squashfs-pf-list.txt

if [ "$USE_SAMBA" = 1 ]; then
#DEVICES="$DEVICES   zero,c,1,5     random,c,1,8       urandom,c,1,9
#        sda,b,8,0 sda1,b,8,1 sda2,b,8,2 sda3,b,8,3 sda4,b,8,4
#        sda5,b,8,5 sda6,b,8,6 sda7,b,8,7
#        sda8,b,8,8 sda9,b,8,9 sda10,b,8,10
#        sda11,b,8,11 sda12,b,8,12
#        sdb,b,8,16 sdb1,b,8,17 sdb2,b,8,18 sdb3,b,8,19 sdb4,b,8,20 
#        sdb5,b,8,21 sdb6,b,8,22 sdb7,b,8,23
#        sdb8,b,8,24 sdb9,b,8,25 sdb10,b,8,26
#        sdb11,b,8,27 sdb12,b,8,28 "
echo "/dev/zero      c 666 root root   1  5" >> squashfs-pf-list.txt
echo "/dev/random    c 666 root root   1  8" >> squashfs-pf-list.txt
echo "/dev/urandom   c 666 root root   1  9" >> squashfs-pf-list.txt
echo "/dev/sda       b 666 root root   8  0" >> squashfs-pf-list.txt
echo "/dev/sda1      b 666 root root   8  1" >> squashfs-pf-list.txt
echo "/dev/sda2      b 666 root root   8  2" >> squashfs-pf-list.txt
echo "/dev/sda3      b 666 root root   8  3" >> squashfs-pf-list.txt
echo "/dev/sda4      b 666 root root   8  4" >> squashfs-pf-list.txt
echo "/dev/sda5      b 666 root root   8  5" >> squashfs-pf-list.txt
echo "/dev/sda6      b 666 root root   8  6" >> squashfs-pf-list.txt
echo "/dev/sda7      b 666 root root   8  7" >> squashfs-pf-list.txt
echo "/dev/sda8      b 666 root root   8  8" >> squashfs-pf-list.txt
echo "/dev/sda9      b 666 root root   8  9" >> squashfs-pf-list.txt
echo "/dev/sda10     b 666 root root   8 10" >> squashfs-pf-list.txt
echo "/dev/sda11     b 666 root root   8 11" >> squashfs-pf-list.txt
echo "/dev/sda12     b 666 root root   8 12" >> squashfs-pf-list.txt
echo "/dev/sdb       b 666 root root   8 16" >> squashfs-pf-list.txt
echo "/dev/sdb1      b 666 root root   8 17" >> squashfs-pf-list.txt
echo "/dev/sdb2      b 666 root root   8 18" >> squashfs-pf-list.txt
echo "/dev/sdb3      b 666 root root   8 19" >> squashfs-pf-list.txt
echo "/dev/sdb4      b 666 root root   8 20" >> squashfs-pf-list.txt
echo "/dev/sdb5      b 666 root root   8 21" >> squashfs-pf-list.txt
echo "/dev/sdb6      b 666 root root   8 22" >> squashfs-pf-list.txt
echo "/dev/sdb7      b 666 root root   8 23" >> squashfs-pf-list.txt
echo "/dev/sdb8      b 666 root root   8 24" >> squashfs-pf-list.txt
echo "/dev/sdb9      b 666 root root   8 25" >> squashfs-pf-list.txt
echo "/dev/sdb10     b 666 root root   8 26" >> squashfs-pf-list.txt
echo "/dev/sdb11     b 666 root root   8 27" >> squashfs-pf-list.txt
echo "/dev/sdb12     b 666 root root   8 28" >> squashfs-pf-list.txt
fi

#DEVICES="$DEVICES  mtdblock1,b,31,1"
echo "/dev/mtdblock1 b 666 root root  31  1" >> squashfs-pf-list.txt
echo "/dev/mtdblock2 b 666 root root  31  2" >> squashfs-pf-list.txt
echo "/dev/mtdblock3 b 666 root root  31  3" >> squashfs-pf-list.txt

if [ "$GW_VPN" = 1  ];then
#DEVICES="$DEVICES  random,c,1,8"
echo "/dev/random    c 666 root root   1  8" >> squashfs-pf-list.txt
fi

if [ "$VOIP_SUPPORT" != "" ]; then
#DEVICES="$DEVICES  pcmctrl0,c,244,0"
echo "/dev/pcmctrl0  c 666 root root 244  0" >> squashfs-pf-list.txt
fi

ln -s /tmp/log $RAMFSDIR/dev/log

#for dev in $DEVICES; do
#	name=`echo $dev |cut -d"," -f1`
#	type=`echo $dev |cut -d"," -f2 `
#	major=`echo $dev |cut -d"," -f3 `
#	minor=`echo $dev |cut -d"," -f4 `
#	mknod -m666 $RAMFSDIR/dev/$name $type $major $minor
#    	chmod 777 $RAMFSDIR/dev/$name
#done

#ln -s /var/web $RAMFSDIR/web

################################################
# At First: we get the strip-file list 

# At Last: Copy debugging scripts	/opt/rtl819x/rtl819x_sdk/boards/dir-603
TDIR=`pwd`/../../users
DBG_SCRPT="ib iw idd irf ob ow od orf rssi acltd setmib getmib"
for scrpt in $DBG_SCRPT ; do
	cp $TDIR/script/script_dbg/$scrpt $RAMFSDIR/bin
done

# Friendly API
if [ "$CONFIG_RTL_COMAPI_CFGFILE" != "n" -a  "$CONFIG_RTL_COMAPI_CFGFILE" != "" ]; then
        mkdir $RAMFSDIR/etc/Wireless
        ln -s /var/RTL8192CD.dat $RAMFSDIR/etc/Wireless/RTL8192CD.dat
fi

#find $RAMFSDIR/. -name ".svn" | xargs -r rm -rf

sync;sync;sync;
echo "============Create Squashfs====================="
if [ "$CONFIG_SQUASHFS_LZMA" = "y" ]; then
	rm -f squashfs-lzma.o
        #$MKSQUASHFS_LZMA $RAMFSDIR squashfs-lzma.o -be -always-use-fragments 
        #$MKSQUASHFS $RAMFSDIR squashfs-lzma.o -comp lzma -always-use-fragments 
        $MKSQUASHFS $RAMFSDIR squashfs-lzma.o -comp lzma -always-use-fragments -pf squashfs-pf-list.txt
        if [ "$USE_SAMBA" = 1 -o "$USE_4M" = 1 ]; then
		$CVIMG root squashfs-lzma.o root.bin 2D0000 $ROOTFS_OFFSET
	else
		$CVIMG root squashfs-lzma.o root.bin F0000 $ROOTFS_OFFSET
	fi
		IMGSIZE=`du -s squashfs-lzma.o |  cut -f1`
else
	rm -f squashfs.o
	#$MKSQUASHFS $RAMFSDIR squashfs.o -always-use-fragments
	$MKSQUASHFS $RAMFSDIR squashfs.o -always-use-fragments -pf squashfs-pf-list.txt
	$CVIMG root squashfs.o root.bin F0000 $ROOTFS_OFFSET
	IMGSIZE=`du -s squashfs.o |  cut -f1`
fi
ROOTSIZE=`du -s $RAMFSDIR | cut -f1`
IMGBYTES=`du -b root.bin | cut -f1`
echo "=============================================="
echo "Summary:"
echo "==>Squashfs disk size  = $ROOTSIZE	KBytes"
echo "==>Squashfs image size = $IMGSIZE	KBytes"

if [ "$CONFIG_RTL_FLASH_MAPPING_ENABLE" = "y" ]; then
if [ $IMGBYTES -gt $MAX_ROOTFS_SIZE ]; then
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "Error:"
echo "====root is too big to burn into flash  ====="
echo "===>max root size      = $MAX_ROOTFS_SIZE  Bytes" 
echo "===>current root size  = $IMGBYTES Bytes" 
exit 1
fi
fi
AVAILABLE_ROOTFS_SIZE=$(($(($MAX_ROOTFS_SIZE))-$(($IMGBYTES))))
echo "====root flash size check ======"
echo "===>root flash start offset = $ROOTFS_OFFSET ====="
echo "===>root flash end  offset = $FLASH_SIZE ====="
echo "===>max root size      = $MAX_ROOTFS_SIZE  Bytes" 
echo "===>current root size  = $IMGBYTES Bytes" 
echo "===>available root size  = $AVAILABLE_ROOTFS_SIZE Bytes" 
