#!/bin/sh
if [ -z "$1" ]; then
echo "Usage: uci_merge <board>"
exit
fi
board=$1

directory=/etc/router/uci
target_file=/etc/router/protoconfig.xml

cp -f /etc/router/boards/${board}/uci/*.xml ${directory} 2>/dev/null
cp -f /etc/router/boards/common/uci/*.xml ${directory} 2>/dev/null

uci req_auth_gen

echo "<?xml version=\"1.0\"?>" > ${target_file}
echo "<config_xml VERSION=\"1.0.0\">" >> ${target_file}
for filename in `ls ${directory}`
do
        if [ ! -d ${directory}'/'${filename} ] ; then            
                cat ${directory}'/'${filename} >> ${target_file}
                echo >> ${target_file}
        fi
done
echo "</config_xml>" >> ${target_file}

# yzhou add for sdk dashboard config
directory=/etc/router/dashboard/
target_name=sdk_dashboard.xml
target_file=/etc/router/dashboard/${target_name}
if [ ! -e ${target_file} ] ; then
    touch ${target_file}
fi 
echo "<!-- this file is automatic generated from ${directory}, DO NOT modify -->" > ${target_file}
echo "<sdk_dashboard_root menu=\"SDKDashBoard\" menu_weight=\"10\" VERSION=\"1.0.0\">" >> ${target_file}
for filename in `ls ${directory}`
do
        # don't process these files
        if [ ${filename} = 'dashboard.xml' -o ${filename} = ${target_name} ] ; then
                continue 
        fi

        if [ -f ${directory}'/'${filename} ] ; then            
                cat ${directory}'/'${filename} >> ${target_file}
                #echo >> ${target_file}
        fi
done
echo "</sdk_dashboard_root>" >> ${target_file}
# end

directory=/etc/router/usi
target_file=/etc/router/protostatus.xml

cp -f /etc/router/boards/${board}/usi/*.xml ${directory} 2>/dev/null
cp -f /etc/router/boards/common/usi/*.xml ${directory} 2>/dev/null

echo "<?xml version=\"1.0\"?>" > ${target_file}
echo "<status_xml VERSION=\"1.0.0\">" >> ${target_file}
for filename in `ls ${directory}`
do
        if [ ! -d ${directory}'/'${filename} ] ; then            
                cat ${directory}'/'${filename} >> ${target_file}
                echo >> ${target_file}
        fi
done
echo "</status_xml>" >> ${target_file}

directory=/etc/router/graph
target_file=/etc/router/protograph.xml

cp -f /etc/router/boards/${board}/graph/*.xml ${directory} 2>/dev/null
cp -f /etc/router/boards/common/graph/*.xml ${directory} 2>/dev/null

echo "<?xml version=\"1.0\"?>" > ${target_file}
echo "<graph_xml VERSION=\"1.0.0\">" >> ${target_file}
for filename in `ls ${directory}`
do
        if [ ! -d ${directory}'/'${filename} ] ; then            
                cat ${directory}'/'${filename} >> ${target_file}
                echo >> ${target_file}
        fi
done
echo "</graph_xml>" >> ${target_file}
