#!/bin/bash

usage="
        Usage: $(basename "$0") <deviceId, monId, PhyPath> [-h]
      Example: $(basename "$0") wlp2s0
Functionality: It resolves the phyId for the given deviceId, monId or PhyPath. For example, it may return \"phy0\" for \"wlan0\", \"mon0\", or a sample PhyPath \"311\".

      Options:
	-h		Show usage information
"

while getopts 'h' OPT; do
    case $OPT in
        h)
            echo "$usage"
			exit 1
    esac
done

if [[ $# == 0 ]]; then
	(>&2 echo "Argument deviceId or monId or PhyPath is mandatory.")
	echo "$usage"
	exit 1
fi

scriptDir="$(cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd )"
PhyPathMapping="$(${scriptDir}/GetPhyPathMapping)"

result=""
result+=$(${scriptDir}/PATH2PHY $1)
result+=$(echo "$PhyPathMapping" | grep -E "PHY:$1 " | grep -Eo "PHY:\S+" | sed -e "s:PHY\:::g" | sed -e "s: ::g")
result+=$(${scriptDir}/DEV2PHY  $1)

# DEV2PHY and MON2PHY return the same thing for DEV, so shall only one work.
if [[ $result == "" ]]; then
    result+=$(${scriptDir}/MON2PHY  $1)
fi

if [[ -n "$result" ]]; then
    echo "$result"
fi