#!/bin/sh


select_right() {
    tag=`wmiir read /tag/sel/ctl | head -n1`
    found="0"
    first=""
    wmiir ls /tag | (while read current
    do
        if [ $current = "sel/" ];then
            continue;
        fi
        if [ "$first" = "" ];then
            first=$current;
        fi
        if [ "$found" = "1" ];then
            wmiir xwrite /ctl view $current;
            return;
        fi
        if [ "$tag/" = "$current" ];then
            echo "cur=$current";
            found="1";
        fi
    done
    wmiir xwrite /ctl view $first;)
}

select_left() {
    tag=`wmiir read /tag/sel/ctl | head -n1`
    echo tag=$tag
    prev=""
    first=""
    wmiir ls /tag | (while read current
    do 
        if [ $current = "sel/" ];then
            continue;
        fi
        if [ "$first" = "" ];then
            first=$current;
        fi
        echo "prev=$prev"
        echo "current=$current"
        if [ "$tag/" = "$current" ];then
            if [ "$current" = "$first" ];then
                echo > /dev/null
            else
                wmiir xwrite /ctl view $prev;
                return;
            fi;
        fi
        prev=$current;
    done
    echo wmiir xwrite /ctl view $prev;
    wmiir xwrite /ctl view $prev;
    )
}

echo $1
case "$1" in
    left)
        select_left ;;
    right)
        select_right ;;
esac