#!/bin/sh

. kuro_lib

TMP_BOOT_MPT=/mnt/boot
TMP_ROOT_MPT=/mnt/rootfs

HDD_ROOTFS=${MTD_MPT}/hddrootfs.tar.gz
UIMAGE=${MTD_MPT}/uImage

# check material
if [ -e ${HDD_ROOTFS} -a -e ${UIMAGE} ] ; then
	# I believe sda is already fdisked and format is finished.
	# That's a KUROBOX quality....www
	#

	mkdir -p ${TMP_BOOT_MPT}
	mkdir -p ${TMP_ROOT_MPT}

	mount -t ext3 ${DISK1_DEV}1 ${TMP_BOOT_MPT}
	[ $? -ne 0 ] && exit 1
	mount -t ext3 ${DISK1_DEV}2 ${TMP_ROOT_MPT}
	[ $? -ne 0 ] && exit 1

	cp -afp ${UIMAGE} ${TMP_BOOT_MPT}/uImage.buffalo
	./tar zxf ${HDD_ROOTFS} -C ${TMP_ROOT_MPT}/
	cp -f /usr/local/bin/kuro_lib ${TMP_ROOT_MPT}/usr/local/bin/

	# OK! prepare of hdd is finished.

	# change u-boot environment
	nvram -c set default_kernel_addr 0x00100000
	nvram -c set bootargs_base 'console=ttyS0,115200'
	nvram -c set bootargs_root 'root=/dev/sda2 rw panic=5'
	nvram -c set bootargs 'console=ttyS0,115200 root=/dev/sda2 rw panic=5 BOOTVER=1.09'
	nvram -c set bootcmd 'ide reset; ext2load ide 0:1 $(default_kernel_addr) /$(kernel); bootm $(default_kernel_addr)'
	nvram -c set nand_boot no

	# OK! u-boot environment is changed.

else
	exit 1
fi
