#!/bin/bash

#--------------------------------------------------------------------------
# Adapter detection script
#   Return the list of adapters
#   $1 : 
#     - 'transcoding' -> Detect transcoding adaters (D100 or D500)
#     - 'management'  -> Detect managment adapters !(D100 or D500)
#     - 'all'  -> Detect all ethernet adapters
# Output can be:
#  in case an IP address is assigned:
#    type ifname mac ip mask
#  in case an IP address is NOT assigned:
#    type ifname mac
#--------------------------------------------------------------------------
if [ -z $1 ]; then
# Default is to detect transcoding
  mode='all'
else
  mode=$1
fi

case $mode in
  'transcoding'|'management') condition="-e $mode";;
  'all')         condition="-e transcoding -e management";;
  *)             echo "Bad argument [transcoding|management|all]";
                 exit -1;
esac

/sbin/ifconfig -a | grep -e "Link encap:Ethernet" -A 1| awk ' \
  BEGIN{i=0;} \
  function eth_type(mac) { if (mac ~ /02:19:23/) return "transcoding"; else \
  return "management";} \
  /eth|sngdsp/{lines[i] = eth_type($5) " " $1 " " $5;} \
  /inet/{ sub(/addr:/,"", $2); sub(/Mask:/,"",$4); lines[i] = lines[i] " " $2 " " $4; i++} \
  /MTU/{i++} \
  END {for(j=0;j<i;j++) print lines[j]}' \
  | grep $condition

# No interface is not an error
exit 0
