#
# Common shell routines for testing Avahi service discovery
# Copyright (c) 2013-2014 Red Hat.
#

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

avahi_notrun_checks()
{
    _get_libpcp_config
    $service_discovery || _notrun "No support for service discovery"

    # Is avahi-browse installed?
    which avahi-browse >/dev/null 2>&1 || _notrun avahi-browse is not installed

    # Is the avahi-daemon installed and running?  (maybe /sbin or /usr/sbin)
    avahi-daemon --check 2>/dev/null || _notrun avahi-daemon is not available
}

avahi_cleanup()
{
    cd $here
    $sudo $signal -a "$service" >/dev/null 2>&1
    rm -f $tmp.*
}

avahi_service()
{
    $sudo "$PCP_RC_DIR/$service" restart >>$here/$seq.full 2>&1
}

avahi_verify()
{
    msg="$@"

    [ "X$msg" = "X" ] && msg="Verify $service avahi service advertising"
    echo $msg | tee -a $here/$seq.full

    # strip any domain part ... e.g. if hostname is vm08.localdomain,
    # avahi reports this as vm08.local
    # look for a line like this ...
    # = eth0 IPv4 PCP pmwebd on vm04.localdomain _pmwebd._tcp local
    # + eth0 IPv6 PCP pmwebd on vm30 _pmwebd._tcp local
    #
    hostname=`hostname | sed -e 's/\..*//'`
    echo "hostname=$hostname" >>$here/$seq.full
    avahi-browse -rt -d local "_$service._tcp" 2>&1 | \
	tee -a $here/$seq.full | \
	sed -n \
	    -e '/ '"$hostname"'[ .]/{
s/  */ /g
s/'"$hostname"'[^ ]*/HOSTNAME/
s/[=+] [^ ]* /. <interface> /
s/ IPv[46] / <protocol> /
p
q
}'

}
