#!/bin/sh
#
# Add-ons for user-defined installation
#

printf "\nExecute bspinst_addons.sh\n\n"
#source ./bspinst_api.sh

nand_erase ()
{
	local name=$1
	local mtd=

	mtd=`get_mtd "$name"`

	if [ -z "$mtd" ]; then
		echo "No MTD partition called $name."
		return 0
	fi

	if [ -e "$mtd" ]; then
		echo "flash eraseall $mtd ... "
		Quiet flash_eraseall $mtd
		echo "flash eraseall done"
	fi
}

android_data_init ()
{
	msg "copy data to data partition..."
	mtdblock_data=`get_mtdblock "android-data"`
	data_mnt="/mnt/mtd_data"

	/bin/mkdir -p ${data_mnt}
	/bin/mount -t yaffs2 -o rw ${mtdblock_data}  ${data_mnt}
#   extract patch packages to Android data partition
	echo "[WMT] update data package to data partition..."
	/bin/tar xf $src_dir/packages/fs_patch/root_patch/.restore/data.tar -C  ${data_mnt} >/dev/null 
	ratio 85
	/bin/tar xf $src_dir/packages/fs_patch/root_patch/.restore/data_app.tar -C  ${data_mnt} >/dev/null 
#   copy patch packages to Android data partition
	/bin/cp -aR $src_dir/packages/fs_patch/data_patch/. ${data_mnt}
	/bin/chmod -R 777  ${data_mnt}/app
	ratio 90

#   no need to chmod/chown if the patch is dumped from Anroid runtime system
#   normally, you can't modify this dumped patch
#   you must use sudo to modify and re-compress the pactch if want to modify it
#	/bin/chmod 0660  ${data_mnt}/data/com.android.providers.settings/databases/*
#   1000 is uid:guid of settings.apk's
#	/bin/chown -hR 1000:1000  ${data_mnt}/data/com.android.providers.settings
	sync
	/bin/umount ${data_mnt}
}

create_udc ()
{
	tar zxf $src_dir/tools/mk_utility*.tgz -C / >/dev/null 

	mtd_max=2147483648
	mtd_len=`get_mtd_len "LocalDisk"`
	if [ "$(($mtd_max))" -lt "$(($mtd_len))" ]; then
		mtd_len=$(($mtd_max))
	fi
	mtd_lendec=`echo $(($mtd_len))`
	mtd_full_reserve=$(( $mtd_lendec + 1073741824 ))
	mtd_full_reserve=$(( $mtd_full_reserve * 5 ))
	mtd_full_reserve=$(( $mtd_full_reserve / 100 ))
	mtd_small_reserve=$(( $mtd_lendec * 5 ))
	mtd_small_reserve=$(( $mtd_small_reserve / 100 ))
	mtd_reserve=$(( $mtd_lendec - $mtd_full_reserve ))	
	mtd_min=10485760
	if [ "$(($mtd_reserve))" -le "$(($mtd_min))" ]; then
		mtd_lendec=$(( $mtd_lendec - $mtd_small_reserve ))
	else
		mtd_lendec=$(( $mtd_lendec - $mtd_full_reserve ))
	fi
	    
	echo "[WMT] Creating UDC udc_file...\n"
	dd bs=1 seek=$mtd_lendec count=1 if=/dev/zero of=$1
	mkdosfs $1	
	sync
}

localdisk_init ()
{
	udc_dir=/mnt/localdisk
	udc_file=${udc_dir}/backfile.vfat
	udc_mount_point=/LocalDisk
	udc_loop_dev=/dev/loop/3

	mtdblock=`get_mtdblock "LocalDisk"`
	/bin/mkdir -p ${udc_dir}

	if [ $INIT_LOCAL_DISK == 0 ]; then
		echo "[WMT] Just remove '~wmtthumb' in LocalDisk"
		/bin/mount -t yaffs2 -o rw ${mtdblock} ${udc_dir}
		/bin/mkdir -p ${udc_mount_point}
		if [ -f  ${udc_file} ]; then
			/sbin/losetup ${udc_loop_dev} ${udc_file}
			/bin/mount -t vfat ${udc_loop_dev} ${udc_mount_point}
			if [ -d  ${udc_mount_point}/~wmtthumb ]; then
				echo "[WMT] removed wmtthumb.db in LocalDisk"
				/bin/rm -rf ${udc_mount_point}/~wmtthumb
				sync
			fi
			/bin/umount ${udc_mount_point}
			/sbin/losetup -d ${udc_loop_dev}
			sync
		else
			echo "[WMT] not found udc file in LocalDisk"
			create_udc ${udc_file}
		fi

		/bin/umount ${udc_dir}
		return 0
	fi

	nand_erase "LocalDisk"
	/bin/mount -t yaffs2 -o rw ${mtdblock} ${udc_dir}
	create_udc ${udc_file}
#   copy pre-files to LocalDisk partition
	msg "copy pre-files to LocalDisk..."
	/bin/mkdir -p ${udc_mount_point}
	/sbin/losetup ${udc_loop_dev} ${udc_file}
	/bin/mount -t vfat -o utf8 ${udc_loop_dev} ${udc_mount_point}
	/bin/cp -aR $src_dir/packages/fs_patch/localdisk/. ${udc_mount_point}
	sync
	/bin/umount ${udc_mount_point}
	/sbin/losetup -d ${udc_loop_dev}
	sync
	/bin/umount ${udc_dir}
	echo "[WMT] copy pre-files to LocalDisk, done"
}

update_logo ()
{
	local name=$1
	local mtd=
	
	mtd=`get_mtd "$name"`

	if [ -z "$mtd" ]; then
		echo "No MTD partition called $name."
		return 0
	fi

	file="$src_dir/$1.data"

	if [ -e "$mtd" ]; then
		echo "flash eraseall $mtd ... "
		Quiet flash_eraseall $mtd
		if [ "u-boot-logo" == "$name" ]; then
			uboot_logo_ver="$src_dir/$1-ver.data"
			echo "[WMT] update uboot-logo `basename $uboot_logo_ver`"
			Quiet nandwrite -p $mtd $uboot_logo_ver
			echo "[WMT] update uboot-logo2"
			Quiet nandwrite -p -s $UBOOT_LOG2_OFFSET  $mtd $file
		else
			echo "[WMT] update kernel-logo"
			Quiet nandwrite -p $mtd $file
		fi
		echo "done"
	fi
}

#msg "Upgrading U-Boot LOGO to NAND"
#update_logo "u-boot-logo"
#ratio 76
#
#msg "Upgrading Kernel LOGO to NAND"
#update_logo "kernel-logo"
#ratio 78
#
#nand_erase "android-data"
#nand_erase "android-cache"
#
#android_data_init

#localdisk_init

printf "\nExecute bspinst_addons.sh done. \n\n"

