#!/bin/bash
#
CMD=$(basename $0)
CMDVER="1.4"
CMDSTR="$CMD v$CMDVER (2023-08-27)"

workdir="/usr/share/arp-scan"
defaultfile="mac-vendor.txt"
customfile="custom-mac-vendors.txt"
updatefile="klabs-mac-vendors.txt"
updateurl="http://www.kubit.ch/arp-scan/$updatefile"


usage()
{
	echo "
usage: $CMD [options] ...

options:
  -f|--force		force update (download and rebuild)
  -r|--rebuild		do rebuild only (no download)
  -u|--url URL		get updates from this url
  			(default: $updateurl)

  -n|--dry-run		dry run (don't exec, only show actions)
  -v|--verbose		be verbose
  -q|--quiet		be quiet
  -h|--help		show help
  -D[n]|--debug[=n]	set debug, optionally with level
  --			stop processing options
" >&2
	[ $# != 0 ] && echo -e "\n$@\n" >&2
	exit 127
}


cleanup()
{
	:
}

show_help()
{
	echo -e "
== $CMDSTR ==

Updates custom vendor list for 'arp-scan' utility, fetching from
$updateurl

Data will be merged into $customfile, then you need to call
arp-scan with parm --macfile=$customfile, or use the command
'ku-netdiscover' (our simple front-end for arp-scan).
"
	return 0
}


wgetfile()
{
	local url=$1
	local out=${2:-""}
	local stat=0
	local cmdout=

	out=${out:-$(echo "$url" | sed -e 's#.*/##')}

	$VERBOSE && echo -en "  getting $out from $url ... "

	cmdout=$(wget -N -O "$out" "$url" 2>&1) || {
		stat=$?
		echo -e "\nerror $? getting file\n\n$cmdout\n" >&2
		rm -f "$out"
		return $stat
	}
	$DEBUG && echo -e "\nD> wget output\n\n$cmdout\n" >&2
	$VERBOSE && echo "ok"
}





# (MAIN)

trap 'echo -e "\n*INTR*\n"; cleanup; exit 255' 1 2 3
trap 'echo -e "\nunexpected error $? at $LINENO\n"' ERR
trap 'cleanup' EXIT
set -e -u

VERBOSE=${VERBOSE:-true}
DEBUG=${DEBUG:-false}
DBGLEVEL=${DBVLEVEL:-0}
f_exec=true
f_force=false
f_download=true

while [ $# != 0 ]
do
  case $1 in
    -u|--url)		shift
    			[ $# != 0 ] && usage '--url requires an argument'
			case $url in
			  http://*|ftp://*|file:///*) ;;
			  *) usage 'wrong url type, allowed: http:// ftp:// file:///'
			esac
			updateurl=$1
			;;
    -h|--help)		show_help; usage ;;
    -n|--dry-run)	f_exec=false ;;
    -f|--force)		f_force=true ;;
    -r|--rebuild)	f_download=false ;;
    -v|--verbose)	VERBOSE=true ;;
    -q|--quiet)		VERBOSE=false ;;
    -D|--debug)		DEBUG=true ;;
    -D[0-9]|--debug=[0-9])
    			DEBUG=true; DBGLEVEL=$(echo "X$1" | sed -e 's/^X-D//' -e 's/^X--debug=//')
			;;
    --)			break ;;
    -*|"")		usage "unknown parm: '$1'" ;;
  esac
  shift
done

[ "$(id -u)" != '0' ] && {
	echo "error, you need to be root to run this command" >&2
	exit 1
}


cd $workdir

$f_download && {
	$f_force && rm -f $updatefile $updatefile.new

	[ -f $updatefile ] && {
		cp -a $updatefile $updatefile.new
	}


	wgetfile $updateurl $updatefile.new || exit $?

	[ -f $updatefile ] && {
		cmp $updatefile.new $updatefile >/dev/null && {
			$VERBOSE && echo "  $updatefile is up to date"
			rm -f $updatefile.new
			exit 0
		}
	}
	rm -f $updatefile
	mv $updatefile.new $updatefile
}

$VERBOSE && echo -n "  rebuilding $customfile ... "
echo "# $customfile
# updated on $(date) by $CMDSTR
#" >$customfile

for file in $defaultfile $updatefile
do
	[ -f $file ] || continue

	echo -n "($file) ... "

	echo "# ===== $file (start) ====" >>$customfile
	echo "#" >>$customfile
	cat $file >>$customfile
	echo "#" >>$customfile
	echo "# ===== $file (end) ====" >>$customfile
done

chmod 666 $customfile
echo "ok"

exit 0
