#!/bin/sh
PATH=/bin/:/sbin/:/usr/bin/:/usr/sbin:/usr/local/sbin:/usr/local/bin
export PATH

DEFAULT_MODEL="D11--A-01-12A-99999"
DEFAULT_DEVCAP_FILE=`echo ${DEFAULT_MODEL} | cut -d- -f1`.conf

EEPROM_CONFIG=/etc/default/eeprom.conf
DEVCAP_POOL_DIR=/etc/default/devcap_pool
DEVCAP_DIR=/etc/default
OEM_CONF_ROOT=/etc/config/oem

# get the MAC address form uboot environment partition
ETHADDR=`fw_printenv ethaddr |cut -d= -f2`
# get the serial number from uboot environment partition
MODEL=`fw_printenv serial |cut -d= -f2`

if [ -z "$MODEL" ]; then
	acti_logger "devcap: invalid serial no found. use ${DEFAULT_DEVCAP_FILE}"
	MODEL=$DEFAULT_MODEL
fi
# the devcap file use the first field seperated by - in the first 5 characters
# in the serial number and 
serial=`echo ${MODEL} | cut -c 1-7`
devcap_file=${DEVCAP_POOL_DIR}/${serial}.conf
if [ ! -s ${devcap_file} ]; then
	# not the skew camera model, use the regular expression to get devcap file
	serial=`echo ${MODEL} | cut -c 1-5 | cut -d- -f1`
	devcap_file=${DEVCAP_POOL_DIR}/${serial}.conf
	if [ ! -s ${devcap_file} ]; then 
		acti_logger "devcap: ${devcap_file} no found. use ${DEFAULT_DEVCAP_FILE}"
		acti_logger "devcap: change model to ${DEFAULT_MODEL}"
		devcap_file=${DEVCAP_POOL_DIR}/${DEFAULT_DEVCAP_FILE}
		MODEL=$DEFAULT_MODEL
	fi
fi

. ${devcap_file}
# load devcap file to get the DEVCAP_VERSION if the firmware is AC
if [ ! -d $OEM_CONF_ROOT ]; then
	acti_logger "Devcap Version ${DEVCAP_VERSION}"
fi

echo "EEPROM_MAC_ADDR='$ETHADDR'" > $EEPROM_CONFIG
echo "EEPROM_SERIAL_ID='${MODEL}'" >> $EEPROM_CONFIG

# copy the selected devcap file to /etc/default
cp -f ${devcap_file} ${DEVCAP_DIR}/devcap

exit 0
