#!/bin/sh
# @(#) $Id: update-ethercodes.sh.in 1529 2021-12-15 00:47:54Z leres $ (LBL)
#
#  Generate ethercodes.dat
#

prog="`basename $0`"

PATH=/usr/local/bin:${PATH}
export PATH

t1=/tmp/${prog}.1.$$

trap 'rm -f ${t1}; exit 1' 1 2 3 15 EXIT

if [ ! -f arp.dat ]; then
	cd /usr/local/arpwatch || exit 1
fi

fn=ethercodes.dat
url=http://standards-oui.ieee.org/oui/oui.csv

if [ -x /usr/bin/fetch ]; then
	cmd1="fetch -q -o - ${url}"
else
	cmd1="wget -q --output-document=- ${url}"
fi

fn2=massagevendor.py
if [ -x ${fn2} ]; then
	# XXX
	## cmd2="/usr/local/bin/python3.9 ${fn2}"
	cmd2="/usr/local/bin/python ${fn2}"
else
	cmd2=./massagevendor
fi

# Emit exit status of first command in pipe
# https://unix.stackexchange.com/a/70675
((((${cmd1}; echo $? >&3) | ${cmd2} >&4) 3>&1) | (read xs; exit ${xs})) 4> ${t1}
if [ $? -ne 0 ]; then
	rm -f ${t1}
fi
mv ${t1} ${fn}
