#!/bin/sh

IPTODEV=ip-to-devinfo
SMAP=smap
OUIDIR=/var/cache/mac-to-devinfo

XIP_SCANS=FALSE

usage() {
    echo "Usage: $0 IP-Range [-r ratelimit] [-i interface] [-x] [port,port,...]"
}
if [ "$1" == "-h" ] || [ "$2" == "-h" ] || [ "$3" == "-h" ] || [ -z "$1" ]; then
    usage
    exit 1
fi

IPRANGE="$1"
shift

if [ "$1" == "-r" ]; then
    RATELIMIT="-r $2"
    if [ -z "$RATELIMIT" ]; then
	echo "-r without a ratelimit"
	usage
        exit 1
    fi
    shift
    shift
fi

if [ "$1" == "-i" ]; then
    INTERFACE="-i $2"
    if [ -z "$INTERFACE" ]; then
	echo "-i without interface"
	usage
	exit 1
    fi
    shift
    shift
fi

if [ "$1" == "-x" ]; then
    XIP_SCANS=TRUE
    shift
fi

if [ -n "$1" ]; then
    PORTLIST="$1"
    
    if [ -z "$(echo $PORTLIST | grep ',')" ]; then
	PORTS="$PORTLIST"
    else
	FIELD=1
	oldcurport=""
	curport="$(echo $PORTLIST | cut -f$FIELD -d, )"
	while [ "$curport" != "$oldcurport" ]; do
	    PORTS="$curport $PORTS"
	    FIELD=$(($FIELD + 1))
	    oldcurport="$(echo $PORTLIST | cut -f$FIELD -d, )"
	    curport="$(echo $PORTLIST | cut -f$FIELD -d, )"
	done
    fi
fi

prep-devinfo

if [ -z "$PORTS" ]; then
    PORTS=5060
fi

for port in $PORTS; do

    if [ "$XIP_SCANS" == "TRUE" ]; then
	XIP_PORT="-x $port"
    fi
    for sip_device in $($SMAP $RATELIMIT -p $port $IPRANGE | grep -E -v 'host.? scanned' | grep 'SIP enabled' | cut -f1 -d:  ); do
	if [ -x "$(which httping)" ]; then
	    if httping -G -q -o 401,200 -c 2 -h $sip_device; then
		echo "$sip_device"" | ""$($IPTODEV $INTERFACE $XIP_PORT $sip_device)"" | ""http://$sip_device/"
		continue
	    fi
	fi
	echo "$sip_device"" | ""$(IPTODEV $XIP_PORT $sip_device)"" | "" - "
    done
done