#!/usr/local/bin/tclsh8.6

#
# Take a "buildgraph" compatible input file, add Wifi sensors according
# to our Osiris policy, and send to stdout a "buildgraph" compatible
# file.
#
# Syntax:
#	ssidprobes		(no argument)
#
# History :
#   2008/07/29 : pda/boggia : conception
#


#
# Main procedure
#
# History
#   2008/07/29 : pda/boggia : design
#


proc usage {argv0} {
    puts stderr "usage: $argv0"
    exit 1
}

proc main {argv0 argv} {

    #
    # Check argument
    #

    if {[llength $argv] != 0} then {
	usage $argv0
	return 1
    }

    #
    # Read informations on stdin
    #

    while {[gets stdin line] >= 0} {
	#
	# Output line first
	#

	puts stdout $line

	#
	# Keep only "node ... type L1" lines which have "radio" attributes
	#

	if {[regexp {^node (\S+) type L1 eq (\S+) .*radio [0-9]+ [0-9]+\s+(.*)} \
				$line \
				bidon \
				node eq ssids]} then {

	    #
	    # Loop through ssids
	    #

	    foreach {ssid name mode} $ssids {
		if {$ssid eq "ssid"} then {
		    switch $mode {
			open {
			    set id "M$eq.assocwifi.$name"
			    puts stdout "ssidprobe $id eq $eq iface $node ssidname $name mode assoc"

			    set id "M$eq.authwifi.$name"
			    puts stdout "ssidprobe $id eq $eq iface $node ssidname $name mode auth"
			}
			auth {
			    set id "M$eq.authwifi.$name"
			    puts stdout "ssidprobe $id eq $eq iface $node ssidname $name mode auth"
			}
			default {
			    puts stderr "Inconsistency: invalid ssid mode '$mode' in '$line'"
			}
		    }
		} else {
		    puts stderr "Inconsistency: bad ssid in '$line'"
		}
	    }
	}
    }

    return 0
}

exit [main $argv0 $argv]
