+async = require 'async'
 parse = require './parse.coffee'
 proto = require './proto.coffee'
 command = require './command.coffee'

	* @param {function(?err)} callback
###
exports.ensure_if = (netname, ip, netmask, callback) ->
	exports.list (err, ifaces) ->
		return callback err if err
		
		iface = ifaces.narrow (previous, current) ->
			return previous if previous and previous.Name == netname
			return current if current and current.Name == netname

				
				exports.list callee
		else
			created_ifs = ifaces
				.map (iface) -> iface.Name
				.filter (iface) -> iface not in snapshoted_ifs and iface != netname
				
			if created_ifs.length > 0
				wrapper = (next) ->
					return async.each created_ifs, ((iface, callback) -> exports.remove_if iface, callback), next
			else
				wrapper = (next) ->
					return do next if next
					
			if iface.IP != ip or iface.NetworkMask != netmask
				wrapper (err) ->
					return callback err if err
					return exports.configure_if netname, ip, netmask, callback
			else
				wrapper (err) ->
					return callback err if err
					return do callback if callback
