#!/usr/bin/env sh

actions="move
fetch-client
fetch-channel
away
not away
back"
action=$(echo "$actions" | $DMENU)


ts_control_move_self() {
	channel=$(teamspeak-query-lib channels | $DMENU)
	if [ -z "$channel" ]; then
		return 1
	fi
	teamspeak-query-lib move --strict-channel "$channel"
	return 0
}

ts_control_fetch() {
		target=$(teamspeak-query-lib "$1s" | $DMENU)
		if [ -z "$target" ]; then
			exit 1
		fi
		teamspeak-query-lib fetch "--strict-$1" "--$1" "$target"
}

case $action in
	"move")
		ts_control_move_self
		;;
	"fetch-client")
	  ts_control_fetch client
		;;
	"fetch-channel")
	  ts_control_fetch channel
		;;
	"not away")
		teamspeak-query-lib move "Not Away From Keyboard"
		teamspeak-query-lib update --microphone=false --speakers=false
		;;
	"away")
		message=$(printf "\n" | $DMENU -p "message")
		if [ -z "$message" ]; then
			exit 1
		fi
		teamspeak-query-lib move "Away From Keyboard"
		teamspeak-query-lib update --away "$message"
		teamspeak-query-lib update --microphone=false --speakers=false --away "$message"
		;;
	"back")
		teamspeak-query-lib update --back
		ts_control_move_self
		teamspeak-query-lib update --microphone=true --speakers=true
		;;
esac