#!/bin/sh

#mac="11:22:33:44:55:66"
input_mac=""

tmp_mac=$1
#mac 合法性检测
input_mac=`echo ${tmp_mac} | sed -n "/^\([0-9A-F][0-9A-F]\)\{5\}[0-9A-F][0-9A-F]$/p"`

if [ -z "$input_mac" ]; then 
	input_mac=`echo ${tmp_mac} | sed -n "/^\([0-9a-f][0-9a-f]\)\{5\}[0-9a-f][0-9a-f]$/p"`
	if [ -z "$input_mac" ]; then 
		echo "mac unvalid!" 
		exit;
	fi
fi
input_mac=${input_mac:0:2}":"${input_mac:2:2}":"${input_mac:4:2}":"${input_mac:6:2}":"${input_mac:8:2}":"${input_mac:10:2}
input_mac=$(/usr/sbin/maccalc add "$input_mac" -2)

echo -n "1"	   
#if [ -f /bin/art-partition.bin ]
#then 
#	rm /bin/art-partition.bin
#fi

fac=`cat /proc/mtd | grep "factory" | awk -F ":" '{print $1}' | awk -F "d" '{print $2}'`
art=`cat /proc/mtd | grep "ART" | awk -F ":" '{print $1}' | awk -F "d" '{print $2}'`
if [ $art ]
then
	num=$art
elif [ $fac ]
then
	num =$fac
else
	echo "error"
	exit;
fi

#echo num:$num

#cat /dev/mtdblock$num >/bin/art-partition.bin

chmod +x /bin/mac_set

/bin/mac_set	$input_mac 
#cat /bin/art-partition.bin >/dev/mtdblock$num

wifi_mac=$(dd if=/dev/mtd$num bs=1 skip=4102 count=6 | hexdump  -e '6/1 "%02x" "%02x"');
wifi_mac=${wifi_mac:0:2}":"${wifi_mac:2:2}":"${wifi_mac:4:2}":"${wifi_mac:6:2}":"${wifi_mac:8:2}":"${wifi_mac:10:2}

rai0=`ifconfig | grep "wifi1" | awk -F " " '{print $1}'`
if [ $rai0 ]
then
	wifi5_mac=$(dd if=/dev/mtd$num bs=1 skip=20486 count=6 | hexdump  -e '6/1 "%02x" "%02x"');
#	echo wifi5_mac:$wifi5_mac
	wifi5_mac=${wifi5_mac:0:2}":"${wifi5_mac:2:2}":"${wifi5_mac:4:2}":"${wifi5_mac:6:2}":"${wifi5_mac:8:2}":"${wifi5_mac:10:2}					
#	wifi5_mac=$(/usr/sbin/maccalc add "$wifi_mac" 1)
#	mac4=`$wifi5_mac | awk -F ":" '{print $4}'`
#	mac5=`$wifi5_mac | awk -F ":" '{print $5}'`
#	iwpriv rai0 6=${mac4}51
#	iwpriv rai0 8=${mac6}${mac5}
fi

lan_mac=$(/usr/sbin/maccalc add "$wifi_mac" 2)
wan_mac=$(/usr/sbin/maccalc add "$lan_mac" 1)

echo wifi_mac:$wifi_mac
echo wifi5_mac:$wifi5_mac
echo lan_mac:$lan_mac
echo wan_mac:$wan_mac

[ -n "$wifi_mac" ] && uci set wireless.wifi0.macaddr=$wifi_mac
[ -n "$wifi5_mac" ] && uci set wireless.wifi1.macaddr=$wifi5_mac

echo "write mac end"
#wifi
#/etc/init.d/network restart


