#!/bin/sh /etc/rc.common
# Copyright (C) 2008 OpenWrt.org

START=02

. /lib/functions/userconfig.sh

USERCONFIG_PARTTTION_NAME="user-config"
mount_userconfig() {
	userconfig_mtd=`cat /proc/mtd | grep ${USERCONFIG_PARTTTION_NAME} | head -n 1 | awk -F : '{print $1}'`
	userconfig_mtd_num=${userconfig_mtd##mtd}
	local USERCONFIG_DEVICE=/dev/mtdblock${userconfig_mtd_num}
	local USERCONFIG_DIR=/tmp/userconfig
	mkdir -p ${USERCONFIG_DIR}
	/usr/sbin/ubiattach -m ${userconfig_mtd_num} -d 1 -b 5 /dev/ubi_ctrl
	if [ $? ==0 ]; then
		/bin/mount -t ubifs -o rw /dev/ubi1_0 /tmp/userconfig -o bulk_read -o sync
	else
		/usr/sbin/ubiformat -y -q /dev/mtd${userconfig_mtd_num}
		/usr/sbin/ubiattach -m ${userconfig_mtd_num} -d 1 -b 5 /dev/ubi_ctrl
		/usr/sbin/ubimkvol /dev/ubi1 -m -N user-config
		/bin/mount -t ubifs -o rw /dev/ubi1_0 /tmp/userconfig -o bulk_read -o sync
	fi
	rootfs_init_etc_ramfs
}

start() {
	mount_userconfig
	defFlag=`grep -o Default /tmp/userconfig/userconfig_default.tmp`
	agileFlag=`grep -o Default /tmp/agileconfig_default.tmp`
	if ([ -n "$defFlag" ] && [ $defFlag = 'Default' ]) || ([ -n "$agileFlag" ] && [ $agileFlag = 'Default' ]);
	then
		echo "set dftmac from tddp flash to network" > /dev/console
		/usr/bin/tddpd dftmac
		/lib/cfg_save/config.sh save
	else
	   echo "no" > /dev/console
	fi
}