#!/usr/bin/env awk
#
# Return true if one of the destination MAC address(es) is equal to 'mac'.
# If 'mac' is omitted, return the destination MAC address(es).
#
# Parameters:
#   - [m] : the MAC address to filter
#
# Dependencies:
#   - macRecorder
#
# See also:
#   - dether : alias for dmac
#   - ether
#   - mac
#   - sether
#   - smac
#
# Examples:
#   - tawk 'dmac("00:11:22:33:44:55")' file.txt
#   - tawk '{ print dmac() }' file.txt

@include "hdr"
@include "splitc"
@include "_validate_col"

function dmac(m,        _c, _i, _idx, _l, _macs, _reps, _str) {
    _c = _validate_col("srcMac_dstMac_numP;dstMac", _my_dstMac)
    if (_c != srcMac_dstMac_numP) {
        _idx = 1
    } else {
        _idx = 2
    }
    _l = split($_c, _reps, ";");
    if (__PRIHDR__ && hdr()) return splitc($_l, _idx)
    for (_i = 1; _i <= _l; _i++) {
        split(_reps[_i], _macs, "_");
        if (length(m) == 0) {
            if (_i == 1) _str = _macs[_idx]
            else if (_str !~ _macs[_idx]) _str = _str ";" _macs[_idx] # ignore reps
        } else if (_macs[_idx] ~ m) return 1
    }
    if (length(m) == 0) return _str
    return 0
}
