#!/bin/bash

set -e

test_mode=0
if [ $# -gt 0 ]; then
	test_mode=1
fi

skip_list="{{ skip_list | join(" ") }}"

ifacelist=""
for iface in $(find /sys/class/net/ ! -type d -printf "%f\n"); do
	iface_type=`grep DEVTYPE "/sys/class/net/$iface/uevent"|cut -d= -f2`
	if [ "$iface_type" != "wlan" ]; then
		continue
	fi

	if [ "x$skip_list" != "x" ]; then
		if echo "$skip_list" | grep -qw "$iface"; then
			continue
		fi
	fi

	if [ $test_mode -eq 0 ]; then
		wicked ifdown $iface
		if [ -f "/etc/sysconfig/network/ifcfg-$iface" ]; then
			rm -f "/etc/sysconfig/network/ifcfg-$iface"
		fi
		if [ -f "/etc/wicked/ifconfig/$iface.xml" ]; then
			rm -f "/etc/sysconfig/network/ifcfg-$iface"
		fi
	fi
	if [ "x$ifacelist" == "x" ]; then
		ifacelist="\"$iface\""
	else
		ifacelist="$ifacelist,\"$iface\""
	fi
done
if [ "x$ifacelist" == "x" ]; then
	echo "{\"changed\": false}"
else
	echo "{\"changed\": true, \"interfaces\": [$ifacelist]}"
fi




