#!/bin/bash

usage="
        Usage: $(basename "$0") <device_id> [macaddr] [-h]
      Example: $(basename "$0") wlan0 00:16:ea:12:34:56
Functionality: It replaces the MAC address of the network interface <device_id> to [macaddr]. 
               If [macaddr] is missing, the default [macaddr] is 00:16:ea:12:34:56.

      Options:
	-h		Show usage information
"

#if [[ $UID == 0 ]]; then
#	(>&2 echo "DO NOT run with sudo.")
#	echo "$usage"
#	exit 1
#fi

if [[ $# < 1 ]]; then
	(>&2 echo "device_id is mandatory.")
	echo "$usage"
	exit 1
fi

deviceId=$1
macAddr="00:16:ea:12:34:56"

if [[ $# == 2 ]]; then
	macAddr=$2
fi

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

scriptDir="$(cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd )"
isIntel5300=$(${scriptDir}/IsIntel5300NIC $deviceId)

if [[ $isIntel5300 == "1" && $# == 1 ]]; then
	echo "Skip setting the mac address ($macAddr) for Intel 5300 NIC ($deviceId)..."
	sudo ifconfig $deviceId down
	sudo ifconfig $deviceId up
	sudo ifconfig $deviceId down
fi

if [[ ($isIntel5300 == "1" && $# == 2) || $isIntel5300 == "0" ]]; then
	sudo ifconfig $deviceId down
	sudo ifconfig $deviceId hw ether $macAddr
	sudo ifconfig $deviceId up
fi

