#!/bin/sh

export PATH="/bin:/sbin:/usr/bin:/usr/sbin"

deal_record_dump(){
    local name_a
    local name_txt
    local ip 
    local txt
    local txt_parse

    #deal a record format
    [ -f /tmp/mdns_a_record_format ] && rm -f /tmp/mdns_a_record_format
    cp -rf /tmp/mdns_a_record_tmp /tmp/mdns_a_record_format
    sed -i '/^$/d' /tmp/mdns_a_record_format

    #deal a record ipv6 format
    [ -f /tmp/mdns_a_record_v6_format ] && rm -f /tmp/mdns_a_record_v6_format
    cp -rf /tmp/mdns_a_record_v6_tmp /tmp/mdns_a_record_v6_format
    sed -i '/^$/d' /tmp/mdns_a_record_v6_format

    #deal txt record format
    [ -f /tmp/mdns_txt_record_format ] && rm -f /tmp/mdns_txt_record_format
    cp -rf /tmp/mdns_txt_record_tmp /tmp/mdns_txt_record_format
    sed -i '/^$/d' /tmp/mdns_txt_record_format

    #match the ip mac and device name
    [ -f /tmp/mdns_ipv4_result_tmp ] && rm -f /tmp/mdns_ipv4_result_tmp

	#match a record and txt record (use deal_txt_record to avoid escape issue)
	deal_txt_record
	[ -f /tmp/mdns_ipv4_result_format ] 
		cat /tmp/mdns_ipv4_result_format | sort -u > /tmp/mdns_ipv4_result_tmp
	
	[ -f /tmp/mdns_v6_result_format ] 
		cat /tmp/mdns_v6_result_format | sort -u > /tmp/mdns_ipv6_result_tmp

    #judge device is offline or nor through arping its ip
    /usr/sbin/mdns_result_maintain
    
}

usage(){
    echo -e "\
Usage:\n\
    -q do not need to query(reduce time of generating mdns table)\n\
    -u generate txt record of mywifiext service\n\
    -o generate extender table which runs mywifiext service\n\
    -h dump help\n "
}

do_query=1

while getopts :uqoh arg
do
    case $arg in
        u)
            update=1
            ;;
        o)
            dump=1
            ;;
        q)
            do_query=0
            ;;
        h)
            usage
            ;;
        ?)
            echo "invalid option"
            usage
            exit 0
            ;;
    esac
done

old_config=""
new_config=""
if [ "x$update" = "x1" ]; then
	hostname=`uci get system.@system[0].hostname`
	devname=$(echo "$hostname" | hexdump -n ${#hostname} -ve '1/1 "%02X"')
    ownmac=$(uci get wireless.2g.macAddr | awk '{print $NF}' | sed 's/://g' | tr 'a-z' 'A-Z')
    if [ "x$ownmac" = "x" ];then
        ownmac=$(ifconfig ra0 | grep HWaddr | awk '{print $NF}' | sed 's/://g' | tr 'a-z' 'A-Z')
    fi
    joined="1" #$(config get mdns_joined)
    modelname=$(echo `uci get system.@system[0].model` | awk '{printf "%s",$1}'|sed 's/ //g')
    if [ "x$modelname" = "x" ];then
        modelname=$( echo `envctl factory get model` | awk '{printf "%s",$1}'|sed 's/ //g')
    fi
    [ -z "$devname" -o -z "$ownmac" -o -z "$joined" ] && \
        echo "===updateservice: get wrong paramter devname=$devname, ownmac=$ownmac, joined=$joined , do not update, exit===" && exit
    [ -z ${modelname} ] && modelname=EAX17
    old_config="$(cat /tmp/mdns_mywifiext_config 2> /dev/null)"
    new_config="${devname}${ownmac}${joined}${modelname}"
    if [ "x$old_config" = "x" -o "x$old_config" != "x$new_config" ];then
        eval "sed -i -e '8s,<txt-record>.*</txt-record>,<txt-record>mac=${ownmac}</txt-record>,'  \\
                     -e '9s,<txt-record>.*</txt-record>,<txt-record>joined=${joined}</txt-record>,' \\
                     -e '10s,<txt-record value-format=\"binary-hex\">.*</txt-record>,<txt-record value-format=\"binary-hex\">devname=${devname}</txt-record>,' \\
                     -e '11s,<txt-record>.*</txt-record>,<txt-record>modelname=${modelname}</txt-record>,' \\
                        /tmp/avahi/services/mywifiext.service"
        [ "$(ps -ww | grep avahi-daemon | grep -o running)" = "running" ] && killall -SIGHUP avahi-daemon
        sleep 2
    fi
    echo "${devname}${ownmac}${joined}${modelname}" > /tmp/mdns_mywifiext_config
fi

if [ "x${do_query}" = "x1" ]; then
    querier
fi

if [ "x$dump" = "x1" ]; then
    [ "$(ps -ww | grep avahi-daemon | grep -o running)" = "running" ] && killall -SIGUSR1 avahi-daemon && sleep 2
    deal_record_dump
fi

exit 0
