#!/bin/zsh
if [[ -z "$DISPLAY" ]]; then
	echo "no DISPLAY set" >/dev/stderr
	exit 1
fi

if [[ -n "$XDG_DATA_HOME" ]]; then
    tmux_sesssion_dir=$XDG_DATA_HOME/tmux_session
else
    tmux_sesssion_dir=$HOME/.local/share/tmux_session
fi

typeset -A sessions

wmctrl -l | while read winid workspace host title; do
	case $title in
		(*tmux::*:*)
			session=${${title##*tmux::}%%:*}
			sessions[${session}]=$winid
			;;
	esac
done

while IFS=: read session workspace; do
	if ((${+sessions[${session}]})); then
#		wmctrl -s $workspace
#		wmctrl -R ${sessions[${session}]}
		wmctrl -r ${sessions[${session}]} -t $workspace
	fi
done <$tmux_sesssion_dir/$DISPLAY