#!/bin/sh

dbus-send --system --type=method_call --dest=org.ofono / org.ofono.Manager.GetProperties

MODEMS=$(list-modems)

[ -z "$MODEMS" ] || {
	for modem in $MODEMS ; do
		[ "x$1" == "x" ] && {
			NEWSTATE=$(dbus-send --system --print-reply=powered --type=method_call --dest=org.ofono $modem org.ofono.Modem.GetProperties | awk 'BEGIN { FS = "[ \t\n]+" } $0 ~ /[pP]owered/ && $5 ~ /false/ { print "true" } $0 ~ /[pP]owered/ && $5 ~ /true/ { print "false" }')
		} || {
			NEWSTATE=$1
		}
		echo "Setting power on $modem to: $NEWSTATE"
		dbus-send --system --type=method_call --dest=org.ofono $modem org.ofono.Modem.SetProperty string:Powered variant:boolean:$NEWSTATE
	done
}
