#! /bin/bash
# This file is autogenerated
# update-cluster-maccollect script, coded by Junichi Uekawa
# 10 Sep 2002
# Copyright 2001 Junichi Uekawa
# Licensed under GPL version 2 or later.
# $Id: update-cluster-maccollect.in,v 1.6 2002/09/20 16:55:52 dancer Exp $

TEXTDOMAIN=update-cluster
TEXTDOMAINDIR=/usr/share/locale

CLUSTERXML=/etc//update-cluster/cluster.xml
IPPREFIX=192.168.0.
PREFIX=node
IPSTART=2
HOSTSTART=2
DOMAINSUFFIX=.cluster
INTERFACE=

UPDATE_CLUSTER_ADD="/usr/sbin/update-cluster-add"
UPDATE_CLUSTER_REMOVE="/usr/bin/update-cluster-remove"
TCPDUMP="tcpdump"

CLUSTERIP=
CLUSTERHOST=
CLUSTERMAC=
CLUSTERNOTES=
CLUSTERNFSROOT=

function showhelp() {
    echo $"\
update-cluster-maccollect [--options] [item] ...
  collects mac address using tcpdump, and creates necessary hostnames 
  and IP address
  for the MAC address.

"
}


if test -z "$1"; then
    showhelp
    exit 1	
fi  

while test -n "$1"; do
    case "$1" in
	--prefix) 
	    PREFIX="$2"
	    shift; shift;
	    ;;
	--ipprefix)
	    IPPREFIX="$2"
	    shift; shift;
	    ;;
	--ipstart)
	    IPSTART="$2"
	    shift; shift;
	    ;;
	--hoststart)
	    HOSTSTART="$2"
	    shift; shift;
	    ;;
	--domainsuffix)
	    DOMAINSUFFIX="$2"
	    shift; shift;
	    ;;
	--interface)
	    INTERFACE="-i $2"
	    shift;shift;
	    ;;
	*)
	    showhelp
	    exit 1 
	    ;; 
    esac;  
done


echo $"Starting scan of network for new MAC address"

${TCPDUMP} -e ${INTERFACE} |awk '$2 ~ /.+:.+:.+:.+:.+:/ {print $2 ; fflush() }' | while read MAC; do
    # I have the MAC address, I will try and hack it into usable something.
    if ! ${UPDATE_CLUSTER_REMOVE} --not --failfound --mac "$MAC" > /dev/null < ${CLUSTERXML} ; then
	# there was a match
	printf $"%s already exists\n" "$MAC"
    else
	# there was no match, this is a new thing.
	printf $"%s is new!\n" "$MAC"
	echo "output will be IP:$IPPREFIX$IPSTART HOSTNAME:$PREFIX$HOSTSTART$DOMAINSUFFIX MAC:$MAC"
	${UPDATE_CLUSTER_ADD} --ip "IP:$IPPREFIX$IPSTART" --hostname "$PREFIX$HOSTSTART$DOMAINSUFFIX" --mac "$MAC"
	IPSTART=$((IPSTART + 1))
	HOSTSTART=$((HOSTSTART + 1))
    fi
done
