#!/usr/bin/env bash
#set -x

WHICH=/usr/bin/which
WGET=$($WHICH wget)
GREP=$($WHICH grep)
AWK=$($WHICH awk)
SED=$($WHICH sed)
WC=$($WHICH wc)
RM=$($WHICH rm)
MV=$($WHICH mv)
TEMPORARY_FILE="${HOME}/get-iab-$(echo $RANDOM).tmp"
DEFAULT_FETCH_URL="http://standards.ieee.org/regauth/oui/iab.txt"
DEFAULT_OUTPUT_FILE="ieee-iab.txt"
VERBOSE=FALSE

#########
# USAGE #
#########
usage()
{
	echo "Usage: get-iab [options]"
	echo "Fetch the Ethernet IAB file from the IEEE website, and save it in the format"
	echo "used by arp-scan."
	echo ""
	echo "'options' is one or more of:"
	echo "        -h Display this usage message."
	echo "        -f FILE Specify the output IAB file. Default=ieee-iab.txt"
	echo "        -u URL Specify the URL to fetch the IAB data from."
	echo "           Default=http://standards.ieee.org/regauth/oui/iab.txt"
	echo "        -v Give verbose progress messages."
	echo ""
}

#######################
# IAB File generation #
#######################
generateiab()
{
	echo "# ieee-iab.txt -- Ethernet vendor IAB file for arp-scan"  > "${DEFAULT_OUTPUT_FILE}"
	echo "#" >> "${DEFAULT_OUTPUT_FILE}"
	echo "# This file contains the Ethernet vendor IABs for arp-scan.  These are used" >> "${DEFAULT_OUTPUT_FILE}"
	echo "# to determine the vendor for a give Ethernet interface given the MAC address." >> "${DEFAULT_OUTPUT_FILE}"
	echo "#" >> "${DEFAULT_OUTPUT_FILE}"
	echo "# Each line of this file contains an IAB-vendor mapping in the form:" >> "${DEFAULT_OUTPUT_FILE}"
	echo "#" >> "${DEFAULT_OUTPUT_FILE}"
	echo "# <IAB><TAB><Vendor>" >> "${DEFAULT_OUTPUT_FILE}"
	echo "#" >> "${DEFAULT_OUTPUT_FILE}"
	echo "# Where <IAB> is the first 36 bits of the MAC address in hex, and <Vendor>" >> "${DEFAULT_OUTPUT_FILE}"
	echo "# is the name of the vendor." >> "${DEFAULT_OUTPUT_FILE}"
	echo "#" >> "${DEFAULT_OUTPUT_FILE}"
	echo "# Blank lines and lines beginning with \"#\" are ignored." >> "${DEFAULT_OUTPUT_FILE}"
	echo "#" >> "${DEFAULT_OUTPUT_FILE}"
	echo "# This file was automatically generated by get-iab at $(date "+%Y-%m-%d %H:%M:%S")" >> "${DEFAULT_OUTPUT_FILE}"
	echo "# using data from http://standards.ieee.org/regauth/oui/iab.txt" >> "${DEFAULT_OUTPUT_FILE}"
	echo "#" >> "${DEFAULT_OUTPUT_FILE}"
	echo "# Do not edit this file.  If you want to add additional MAC-Vendor mappings," >> "${DEFAULT_OUTPUT_FILE}"
	echo "# edit the file mac-vendor.txt instead." >> "${DEFAULT_OUTPUT_FILE}"
	echo "#" >> "${DEFAULT_OUTPUT_FILE}"
	$SED 's|^  ||g' "${TEMPORARY_FILE}" | $AWK 'BEGIN{FS="\t";OFS="|"} {$1=$1;print}' | $GREP '(hex)\|(base 16)' | $SED 's|(hex)||g' | $SED 's|(base 16)||g' | $AWK -F"|" '{print}; NR%2==0 {print ""}' | $AWK -F'|' 'BEGIN {RS=""}; {OFS="|"}; {$1=$1;print $1,$5,$4}' | $AWK -F'|' '{ gsub("-","",$1); print$1 substr($2,8,3)"\t"$3}' >> "${DEFAULT_OUTPUT_FILE}"
	$RM "${TEMPORARY_FILE}"
}

while getopts hvf:u: name
do
	case $name in
		h)
			hflag=1
			usage
			exit 2
			;;
		v)
			vflag=1
			VERBOSE=TRUE
			;;
		f)
			fflag=1
			DEFAULT_OUTPUT_FILE="$OPTARG"
			;;
		u)
			uflag=1
			DEFAULT_FETCH_URL="$OPTARG"
			;;
		*)
			usage
			exit 2
			;;
	esac
done

if [ $VERBOSE == "TRUE" ] ; then
	if [[ -f "${DEFAULT_OUTPUT_FILE}" ]] ; then
		echo "Renaming ${DEFAULT_OUTPUT_FILE} to ${DEFAULT_OUTPUT_FILE}.bak"
		$MV "${DEFAULT_OUTPUT_FILE}" "${DEFAULT_OUTPUT_FILE}.bak"
	fi
	echo "Fetching IAB data from ${DEFAULT_FETCH_URL}"
	FETCHED=$(LANG=C $WGET -O "${TEMPORARY_FILE}" "${DEFAULT_FETCH_URL}" 2>&1 | $GREP Length | $AWK '{print $2}' | $SED 's|,||g')
	echo "Fetched ${FETCHED} bytes"
	echo "Opening output file ${DEFAULT_OUTPUT_FILE}"
	generateiab
	echo "$($GREP -v ^# ${DEFAULT_OUTPUT_FILE} | $WC -l) IAB entries written to file ${DEFAULT_OUTPUT_FILE}"
else
	if [[ -f "${DEFAULT_OUTPUT_FILE}" ]] ; then
		$MV "${DEFAULT_OUTPUT_FILE}" "${DEFAULT_OUTPUT_FILE}.bak"
	fi
	$WGET -O "${TEMPORARY_FILE}" "${DEFAULT_FETCH_URL}" 2>&1 | $GREP Length | $AWK '{print $2}' | $SED 's|,||g'
	generateiab
fi
