#!/bin/sh
KERNEL_SOURCE=$PWD/../linux-2.4.18
RAMFSDIR=$PWD/rootfs
UCLIBCDIR=/usr/local/gcc333/lexra-nnop-v5/mips-linux-uclibc/lib
#UCLIBCDIR=uClibc-0.9.26/lib
MOUNTDIR=/mnt
MKSQUASHFS=$PWD/tools/mksquashfs
MKSQUASHFS_LZMA=$PWD/tools/mksquashfs-lzma
CVIMG=$PWD/tools/cvimg
MINI_UPNP=1
UPNPLIB_USE_141=1

if [ "$1" = ""  ]; then
	echo "mkimg {AP=1 | GW=1 | GW_8M=1 | GW_VPN=1}"
	exit 1
fi

eval $1
if [ "$2" != ""  ]; then
	eval $2
fi

if [ "$3" != ""  ]; then
	eval $3
fi

if [ "$4" != ""  ]; then
	eval $4
fi
if [ "$GW" = 1 ]; then
	COMPRESS_RAMDISK=1
fi

if [ "$AP" = 1 ]; then
	RAMDISK_SIZE=2000
elif [ "$GW" = 1 ]; then
	RAMDISK_SIZE=3200
elif [ "$GW_VPN" = 1 ]; then
	RAMDISK_SIZE=5600
elif [ "$GW_8M" = 1 ] ; then
	RO_FS=1
fi


rm -rf $RAMFSDIR $RAMDISK_TMP

#create root file system dir

ROOTDIRS="dev bin sbin usr etc var proc tmp"
for dir in $ROOTDIRS ; do
	mkdir -p $RAMFSDIR/$dir
done

if [ "$RO_FS" = 1  ];then
	rm -rf $RAMFSDIR/tmp
	ln -s /var/tmp $RAMFSDIR/tmp
fi

DEVICES="ttyS0,c,4,64	console,c,4,64  null,c,1,3 	mtd,b,31,0
	wl_chr0,c,13,0  wl_chr1,c,13,1"

if [ "$GW" = 1 -o  "$GW_8M" = 1 -o "$GW_VPN" = 1 ];then
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"
fi
if [ "$RO_FS" = 1  ];then
DEVICES="$DEVICES  mtdblock1,b,31,1"
fi

if [ "$GW_VPN" = 1  ];then
DEVICES="$DEVICES  random,c,1,8"
fi

if [ "$RO_FS" = 1 ] ;then
	ln -s /tmp/log $RAMFSDIR/dev/log  	
fi

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
done

# busybox
cp -R -p ./busybox-1.00-pre8/_install/* $RAMFSDIR/

cp ./bridge-utils/brctl/brctl $RAMFSDIR/bin

if [ "$GW" = 1 -o "$GW_8M" = 1 -o "$GW_VPN" = 1 ];then
	cp ./iptables-1.2.6a/iptables $RAMFSDIR/bin
fi


# web sever
if [ "$GW" = 1 ];then
	cp ./goahead-2.1.1/LINUX/flash-gw $RAMFSDIR/bin/flash
	cp ./goahead-2.1.1/LINUX/webs-gw $RAMFSDIR/bin/webs
elif [ "$GW_VPN" = 1 ];then
	cp ./goahead-2.1.1/LINUX/flash-vpn $RAMFSDIR/bin/flash
	cp ./goahead-2.1.1/LINUX/webs-vpn $RAMFSDIR/bin/webs
elif [ "$GW_8M" = 1 ];then
	cp ./goahead-2.1.1/LINUX/flash-gw $RAMFSDIR/bin/flash
	cp ./goahead-2.1.1/LINUX/webs-gw $RAMFSDIR/bin/webs
elif [ "$AP" = 1 ];then
	cp ./goahead-2.1.1/LINUX/flash-ap $RAMFSDIR/bin/flash
	cp ./goahead-2.1.1/LINUX/webs-ap $RAMFSDIR/bin/webs
fi
if [ "$RO_FS" = 1 ];then
	ln -s /var/web $RAMFSDIR/web
fi
# script files

if [ "$GW" = 1  -o "$GW_8M" = 1 -o "$GW_VPN" = 1  ];then
	cp ./script/*.sh $RAMFSDIR/bin
	if [ "$GW_VPN" != 1 ];then
		rm  $RAMFSDIR/bin/vpn.sh
	fi
elif [ "$AP" = 1  ];then
	cp ./script/init.sh $RAMFSDIR/bin
	cp ./script/bridge.sh $RAMFSDIR/bin
	cp ./script/fixedip.sh $RAMFSDIR/bin
	cp ./script/dhcpc.sh $RAMFSDIR/bin
	cp ./script/dhcpd.sh $RAMFSDIR/bin
	cp ./script/wlan.sh $RAMFSDIR/bin
	cp ./script/wlanapp.sh $RAMFSDIR/bin
	cp ./script/killsh.sh $RAMFSDIR/bin
fi

# Copy debugging scripts
DBG_SCRPT="ib iw idd icck iofdm irf ob ow od occk oofdm orf rssi tssi bias acltd"
for scrpt in $DBG_SCRPT ; do
	cp ./script/script_dbg/$scrpt $RAMFSDIR/bin
done

# udhcp DHCP client

#cp ./udhcp-0.9.9-pre/udhcpc $RAMFSDIR/bin
mkdir $RAMFSDIR/usr/share
mkdir $RAMFSDIR/usr/share/udhcpc
cp ./udhcp-0.9.9-pre/mips-scripts/* $RAMFSDIR/usr/share/udhcpc
if [ "$RO_FS" = 1  ]; then
	ln -s /var/udhcpc $RAMFSDIR/etc/
	rm $RAMFSDIR/usr/share/udhcpc/*.deconfig
	ln -s /var/udhcpc/br0.deconfig $RAMFSDIR/usr/share/udhcpc/br0.deconfig
	ln -s /var/udhcpc/eth0.deconfig $RAMFSDIR/usr/share/udhcpc/eth0.deconfig
	ln -s /var/udhcpc/eth1.deconfig $RAMFSDIR/usr/share/udhcpc/eth1.deconfig
	ln -s /var/udhcpc/wlan0.deconfig $RAMFSDIR/usr/share/udhcpc/wlan0.deconfig
else
	mkdir $RAMFSDIR/etc/udhcpc
fi

# udhcpd DHCP server
cp ./udhcp-0.9.9-pre/udhcpd $RAMFSDIR/bin
ln -s udhcpd $RAMFSDIR/bin/udhcpc
if [ "$RO_FS" = 1  ]; then
	ln -s /var/udhcpd $RAMFSDIR/etc
else
	mkdir -p $RAMFSDIR/var/lib/misc
fi

# for uclibc shared library
mkdir $RAMFSDIR/lib
#cp $UCLIBCDIR/libuClibc-0.9.26.so $RAMFSDIR/lib/libc.so.0
#if [ "$GW" = 1  -o "$GW_8M" = 1  -o "$GW_VPN" = 1 ];then
#	cp $UCLIBCDIR/libcrypt-0.9.26.so $RAMFSDIR/lib/libcrypt.so.0
#fi
#if [ "$GW_VPN" = 1 ];then
#	cp $UCLIBCDIR/libm-0.9.26.so $RAMFSDIR/lib/libm.so.0
#fi

# add DNS relay (dnrd)
if [ "$GW" = 1  -o "$GW_VPN" = 1 -o "$GW_8M" = 1 ];then
    if [ "$RO_FS" = 1 ] ;then
        cp ./dnrd-2.12.1/src/dnrd $RAMFSDIR/bin
	ln -s /var/dnrd $RAMFSDIR/etc/
    else
	cp ./dnrd-2.12.1/src/dnrd $RAMFSDIR/bin
	mkdir $RAMFSDIR/etc/dnrd
    fi
fi

# wireless LAN configure tool
cp ./wireless_tools.25/iwpriv $RAMFSDIR/bin

# for 802.1x
cp ./auth/src/auth $RAMFSDIR/bin
cp ./auth/src/dlisten/iwcontrol $RAMFSDIR/bin

# for IAPP
cp ./IAPP/iapp $RAMFSDIR/bin
# for DDNS 
if [ "$GW" = 1 -o "$GW_VPN" = 1 -o "$GW_8M" = 1 ];then
	cp ./updatedd-2.5/src/updatedd $RAMFSDIR/bin
fi

if [ "$MINI_UPNP" = 0 ] && [ "$GW" = 1 -o "$GW_VPN" = 1 -o "$GW_8M" = 1 ];then
	if [ "$RO_FS" = 1 ] ; then
		cp ./upnpd/pseudoicsd/upnpd $RAMFSDIR/bin/upnpd
		ln -s /var/linuxigd $RAMFSDIR/etc/
		mkdir -p $RAMFSDIR/etc/tmp
		cp ./script/pics* 	$RAMFSDIR/etc/tmp
	else
		mkdir $RAMFSDIR/etc/linuxigd
		cp ./upnpd/pseudoicsd/upnpd $RAMFSDIR/bin/upnpd
		cp ./script/pics* $RAMFSDIR/etc/linuxigd
	fi
	if [ "$UPNPLIB_USE_141" = 1 ] ; then
		cp ./upnpd/libupnp-1.4.1/upnp/.libs/libupnp.so.2 $RAMFSDIR/lib/libupnp.so.2
		cp ./upnpd/libupnp-1.4.1/ixml/.libs/libixml.so.2 $RAMFSDIR/lib/libixml.so.2
		cp ./upnpd/libupnp-1.4.1/threadutil/.libs/libthreadutil.so.2 $RAMFSDIR/lib/libthreadutil.so.2
		
	else
		cp ./upnpd/libupnp-1.2.1/upnp/bin/mips-uclibc/libixml.so $RAMFSDIR/lib/libixml.so
		cp ./upnpd/libupnp-1.2.1/upnp/bin/mips-uclibc/libthreadutil.so $RAMFSDIR/lib/libthreadutil.so
		cp ./upnpd/libupnp-1.2.1/upnp/bin/mips-uclibc/libupnp.so $RAMFSDIR/lib/libupnp.so	
	fi
fi
# PPPoE
if [ "$GW" = 1  -o "$GW_VPN" = 1 -o "$GW_8M" = 1 ];then
	if [ "$RO_FS" = 1 ] ; then
		cp ./ppp-2.4.2/pppd/pppd $RAMFSDIR/bin
		ln -s /var/ppp $RAMFSDIR/etc/ 
	else
		cp ./ppp-2.4.2/pppd/pppd $RAMFSDIR/bin
		mkdir $RAMFSDIR/etc/ppp
		touch $RAMFSDIR/etc/ppp/pap-secrets
		chmod 600 $RAMFSDIR/etc/ppp/pap-secrets
		touch $RAMFSDIR/etc/ppp/chap-secrets
		chmod 600 $RAMFSDIR/etc/ppp/chap-secrets
	fi
fi

#PPTP
if [ "$GW" = 1 -o "$GW_VPN" = 1 -o "$GW_8M" = 1 ];then
	if [ "$RO_FS" = 1 ] ; then
		cp ./pptp-linux-1.3.1/pptp $RAMFSDIR/bin
		cp ./pptp-linux-1.3.1/pptp /mnt/bin
	else
		mkdir $RAMFSDIR/etc/ppp/peers
		touch $RAMFSDIR/etc/ppp/peers/rpptp
		cp ./pptp-linux-1.3.1/pptp $RAMFSDIR/bin
	fi
fi

# auto-discovery daemon
cp ./discover/disc_server $RAMFSDIR/bin

# reload daemon
cp ./rc/reload $RAMFSDIR/bin

# etc and var dir 
cp -R -p ./etc/* $RAMFSDIR/etc
cp -R -p ./var/var/* $RAMFSDIR/var
if [ "$GW" = 1 ];then
	RM_FILES="profile.ap profile.gw-vpn profile.gw-8m group"
	for i in $RM_FILES ; do
		rm -rf $RAMFSDIR/etc/$i
	done
elif [ "$GW_8M" = 1  ];then
	RM_FILES="profile.ap profile.gw-vpn group "
	for i in $RM_FILES ; do
		rm -rf $RAMFSDIR/etc/$i
	done
	mv $RAMFSDIR/etc/profile.gw-8m $RAMFSDIR/etc/profile

elif [ "$GW_VPN" = 1  ];then
	RM_FILES="profile.gw-8m profile.ap"
	for i in $RM_FILES ; do
		rm -rf $RAMFSDIR/etc/$i
	done
	mv $RAMFSDIR/etc/profile.gw-vpn $RAMFSDIR/etc/profile
elif [ "$AP" = 1  ];then
	RM_FILES="profile.gw-8m profile.gw-vpn group ppp"
	for i in $RM_FILES ; do
		rm -rf $RAMFSDIR/etc/$i
	done
	mv $RAMFSDIR/etc/profile.ap $RAMFSDIR/etc/profile
fi

if [ "$RO_FS" = 1 ];then
	rm -rf $RAMFSDIR/var/*
fi

if [ "$RO_FS" = 1 ];then
        rm -rf $RAMFSDIR/var/*
        ln -s /var/resolv.conf $RAMFSDIR/etc/resolv.conf
fi

#ntp
if [ "$GW" = 1  -o "$GW_8M" = 1 -o "$GW_VPN" = 1 ];then
	cp  ntpclient/ntpclient $RAMFSDIR/bin
	if [ "$RO_FS" = 1 ];then
		ln -s /var/TZ $RAMFSDIR/etc/TZ
	fi
fi

if [ "$APMIB_SHARED" = 1 ]; then
	cp ./goahead-2.1.1/LINUX/apmib.so $RAMFSDIR/lib
fi

# SNMP
if [ "$CONFIG_SNMP" = 1 ]; then
	cp ./net-snmp-5.4.1/agent/snmpd $RAMFSDIR/bin
	if [ "$RO_FS" = 1 ]; then
		ln -s /var/net-snmp $RAMFSDIR/etc/
	else
		mkdir $RAMFSDIR/etc/net-snmp
	fi
fi



#ipsec
if [ "$GW_VPN" = 1 ];then
	FREESWAN=./openswan-1.0.1/_install
	cp mawk-1.3.3/mawk $RAMFSDIR/bin/awk
	mkdir -p $RAMFSDIR/usr/local/lib/ipsec
	rm -f ${FREESWAN}/usr/local/lib/ipsec/*.old
	cp -p ${FREESWAN}/usr/local/lib/ipsec/* $RAMFSDIR/usr/local/lib/ipsec
	cp ${FREESWAN}/usr/local/sbin/ipsec $RAMFSDIR/sbin
	mkdir -p $RAMFSDIR/etc/rc.d/init.d
	cp ${FREESWAN}/etc/rc.d/init.d/ipsec $RAMFSDIR/etc/rc.d/init.d
fi

# for WPS
cp ./wsc/src/wscd $RAMFSDIR/bin
cp ./wsc/src/wscd.conf $RAMFSDIR/etc/wscd.conf
if [ "$RO_FS" = 1 ];then
	cp ./script/simplecfg* $RAMFSDIR/etc
	ln -s /var/wps $RAMFSDIR/etc/simplecfg	
else
	mkdir $RAMFSDIR/var/wps
	cp ./script/simplecfg* $RAMFSDIR/var/wps
fi
cp ./mini_upnp/mini_upnp.so $RAMFSDIR/lib
cp ./mini_upnp/mini_upnpd $RAMFSDIR/bin

if [ "$AP" != 1  ];then
	if [ "$MINI_UPNP" = 1 ]; then
		# for mini-IGD		
		cp ./miniigd/miniigd $RAMFSDIR/bin		
		
		if [ "$RO_FS" = 1 ] ; then
			ln -s /var/linuxigd $RAMFSDIR/etc/
			mkdir -p $RAMFSDIR/etc/tmp
			cp ./miniigd/pics* $RAMFSDIR/etc/tmp
		else
			mkdir $RAMFSDIR/etc/linuxigd
			cp ./miniigd/pics* $RAMFSDIR/etc/linuxigd
		fi		
		rm $RAMFSDIR/bin/upnp.sh
		mv $RAMFSDIR/bin/miniigd.sh $RAMFSDIR/bin/upnp.sh
	fi		

	# for LLTD
	#cp ./lltdd/native-linux/lld2d $RAMFSDIR/bin
	#cp ./lltdd/src/wrt54g.large.ico $RAMFSDIR/etc/icon.ico
	#for igmpproxy
	cp ./igmpproxy/igmpproxy $RAMFSDIR/bin
fi	

#library  strip
OBJ_FILES=`find $RAMFSDIR \( -type d -o -type f -o -type b -o -type c -o -type l \) -print | file -f - | grep ELF |  grep "shared" | cut -d':' -f1` 

#echo $OBJ_FILES
cd libstrip
./libstrip $OBJ_FILES
if [ "$GW_VPN" = 1 ];then
./libstrip-pthread $OBJ_FILES
fi
cd ..

cp libstrip/libuClibc-0.9.26.so $RAMFSDIR/lib/libc.so.0
cp $UCLIBCDIR/ld-uClibc-0.9.26.so $RAMFSDIR/lib/ld-uClibc.so.0
if [ "$GW" = 1  -o "$GW_8M" = 1 ];then
	cp libstrip/libcrypt-0.9.26.so $RAMFSDIR/lib/libcrypt.so.0
	cp $UCLIBCDIR/libresolv-0.9.26.so $RAMFSDIR/lib/libresolv.so.0
	cp $UCLIBCDIR/libdl-0.9.26.so $RAMFSDIR/lib/libdl.so.0
        cp $UCLIBCDIR/libnsl-0.9.26.so $RAMFSDIR/lib/libnsl.so.0
fi

if [ "$GW_VPN" = 1 ];then
	#cp libstrip/libm-0.9.26.so $RAMFSDIR/lib/libm.so.0
	cp $UCLIBCDIR/libcrypt-0.9.26.so $RAMFSDIR/lib/libcrypt.so.0
	cp $UCLIBCDIR/libdl-0.9.26.so $RAMFSDIR/lib/libdl.so.0
	cp libstrip/libpthread-0.9.26.so $RAMFSDIR/lib/libpthread.so.0 
	cp $UCLIBCDIR/libresolv-0.9.26.so $RAMFSDIR/lib/libresolv.so.0
	cp $UCLIBCDIR/ld-uClibc-0.9.26.so $RAMFSDIR/lib/ld-uClibc.so.0
	cp  libstrip/libgmp.so.3 $RAMFSDIR/lib/libgmp.so.3
        cp $UCLIBCDIR/libnsl-0.9.26.so $RAMFSDIR/lib/libnsl.so.0

fi

#delete CVS related file
find $RAMFSDIR/. -name CVS | xargs -r rm -rf

sync;sync;sync;
if [ "$RO_FS" != 1  ];then
	rm -f ./ramdisk.gz ./ramdisk
	mkdir -p $RAMFSDIR $RAMDISK_TMP
	echo "============Create Ramdisk====================="
	echo "==>Making ramdisk size $RAMDISK_SIZE KBytes"

	# strip rootfs file , but not strip shared library
	NON_STRIPS=`find $RAMFSDIR -type f -print -exec file {} \; | grep "not stripped" | grep -v "\.so" | cut -d":" -f1`
	if [ "$NON_STRIPS" != "" ]; then
		mips-uclibc-strip -S --strip-unneeded $NON_STRIPS
	fi

	ROOTSIZE=`du -s $RAMFSDIR | cut -f1`
	if [ $ROOTSIZE -gt $RAMDISK_SIZE ];then
		echo "Error! :Your rootfs size $ROOTSIZE KBytes over ramdisk size"
		exit 1
	fi
	dd if=/dev/zero of=/dev/ram bs=1k count=$RAMDISK_SIZE
	mke2fs -c /dev/ram  $RAMDISK_SIZE || exit
	mount -t ext2 /dev/ram $MOUNTDIR || exit
	
	# copy to ramdisk mount
	cd $RAMFSDIR ; find * -print | tar cTf  - - |  (cd $MOUNTDIR && tar -xf -) || exit
	cd ..
	umount /dev/ram
	dd if=/dev/ram bs=1k count=$RAMDISK_SIZE of=./ramdisk

	if [ "$COMPRESS_RAMDISK" = "1" ]; then
		if [ "$BZIP2" = "1" ]; then
			bzip2 -9v -f ramdisk
			cp -f ramdisk.bz2 $KERNEL_SOURCE/arch/mips/ramdisk/ramdisk.img
		else
			gzip -9v -f ramdisk
			cp -f ramdisk.gz $KERNEL_SOURCE/arch/mips/ramdisk/ramdisk.img
		fi
	else
		cp -f ramdisk $KERNEL_SOURCE/arch/mips/ramdisk/ramdisk.img
	fi
	# show ramdisk size
	AVAIL_SIZE=`expr $RAMDISK_SIZE - $ROOTSIZE`
	echo "==============================================="
	echo "Summary:"
	echo "==>Total size     = $RAMDISK_SIZE	KBytes"
	echo "==>Ramdisk size   = $ROOTSIZE	KBytes"
	echo "==>Available size = $AVAIL_SIZE		KBytes"
else
if [ "$LZ" = "1" ]; then
    	echo "============Create Squashfs-Lzma====================="
    	rm squashfs-lzma.o
    	$MKSQUASHFS_LZMA $RAMFSDIR squashfs-lzma.o -be
    
    	$CVIMG root squashfs-lzma.o root-lzma.bin 100000 100000
    	ROOTSIZE=`du -s $RAMFSDIR | cut -f1`
    	IMGSIZE=`du -s squashfs-lzma.o |  cut -f1`
    	echo "=============================================="
    	echo "Summary:"
    	echo "==>Squashfs-Lzma disk size  = $ROOTSIZE	KBytes"
    	echo "==>Squashfs-Lzma image size = $IMGSIZE	KBytes"
else
	echo "============Create Squashfs====================="
	 rm squashfs.o
	 #$MKCRAMFS $RAMFSDIR cramfs.o -r
	 $MKSQUASHFS $RAMFSDIR squashfs.o -be
	 $CVIMG root squashfs.o root.bin 100000 100000
	ROOTSIZE=`du -s $RAMFSDIR | cut -f1`
	IMGSIZE=`du -s squashfs.o |  cut -f1`
	echo "=============================================="
	echo "Summary:"
	echo "==>Squashfs disk size  = $ROOTSIZE	KBytes"
	echo "==>Squashfs image size = $IMGSIZE	KBytes"
fi	
fi
