#!/bin/bash
#
#  Copyright (c) 2018, The OpenThread Authors.
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#  3. Neither the name of the copyright holder nor the
#     names of its contributors may be used to endorse or promote products
#     derived from this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.
#

#
# This script includes common utils for testing mdns
#

set -euxo pipefail

readonly DNS_SD_RESULT=result

case "${OTBR_MDNS}" in
    mDNSResponder)
        sudo service avahi-daemon stop || true
        sudo killall mdnsd || true
        sudo mdnsd
        sleep 1
        ;;

    avahi)
        sudo killall mdnsd || true
        sudo service avahi-daemon restart
        sleep 1
        ;;

    *)
        echo >&2 "Not supported"
        exit 128
        ;;
esac

on_exit()
{
    readonly EXIT_CODE=$?

    kill "$PID"
    [[ ! -e ${DNS_SD_RESULT} ]] || rm "${DNS_SD_RESULT}" || true

    exit $EXIT_CODE
}

start_publisher()
{
    "${OTBR_TEST_MDNS}" "$1" &
    PID=$!
    trap on_exit EXIT
    sleep 2
}

#######################################
# Check if a service is regisered
#
# Arguments:
#   $1  Name
#   $2  Type
#   $3  Text record
#
# Returns:
#   0           Registered
#   otherwise   Not registered
#######################################
dns_sd_check()
{
    # dns-sd will not exit
    dns-sd -L "$1" "$2" local >"${DNS_SD_RESULT}" 2>&1 &
    DNS_SD_PID=$!
    sleep 1
    kill "${DNS_SD_PID}"

    cat "${DNS_SD_RESULT}"
    grep "$3" "${DNS_SD_RESULT}"
}

#######################################
# Check if a service is registered with
# a given type.
#
# Arguments:
#   $1  Name
#   $2  Type
#
# Returns:
#   0           Registered
#   otherwise   Not registered
#######################################
dns_sd_check_type()
{
    # dns-sd will not exit
    dns-sd -B "$2" local >"${DNS_SD_RESULT}" 2>&1 &
    DNS_SD_PID=$!
    sleep 1
    kill "${DNS_SD_PID}"

    cat "${DNS_SD_RESULT}"
    grep "$1" "${DNS_SD_RESULT}"
}

#######################################
# Check if a host is regisered
#
# Arguments:
#   $1  hostname
#   $2  address
#
# Returns:
#   0           Registered
#   otherwise   Not registered
#######################################
dns_sd_check_host()
{
    # dns-sd will not exit
    dns-sd -G v6 "$1" >"${DNS_SD_RESULT}" 2>&1 &
    DNS_SD_PID=$!
    sleep 1
    kill "${DNS_SD_PID}"

    cat "${DNS_SD_RESULT}"
    grep "$2" "${DNS_SD_RESULT}"
}

#######################################
# Check if a service is registered
#
# Arguments:
#   $1  Expected avahi query result string
#   $2  Service type. If omitted, all
#       services will be examined.
#
# Returns:
#   0           Registered
#   otherwise   Not registered
#######################################
avahi_check()
{
    local service_type
    (($# == 2)) && service_type="$2" || service_type="-a"

    avahi-browse -prt "$service_type" | tee | grep "$1"
}
