get_sessions()
{
    case $# in
    0) _response="$(erl_rpc 'eturnal_ctl' 'get_sessions' || :)" ;;
    1) _response="$(erl_rpc 'eturnal_ctl' 'get_sessions' "[\"$1\"]" || :)" ;;
    esac

    case $_response in
        '{ok,'*)
            echo "$_response" | sed -e 's/{ok, *"\(.*\)"}/\1/' -e 's/~n/\
/g'
            exit 0
            ;;
        '{error,'*)
            echo "$_response" | sed 's/{error, *"\(.*\)"}/\1/' >&2
            exit 1
            ;;
        *)
            echo "Unknown error: '$_response'" >&2
            exit 1
            ;;
    esac
}

if [ $# -ge 1 ] && [ "x$1" = 'xhelp' ]
then
    echo "Usage: $REL_NAME sessions [Username]"
    echo 'Print the list of currently active TURN sessions. If a Username is'
    echo 'specified, print only the sessions belonging to the given Username.'
    echo 'If TURN credentials of the form "$timestamp:$suffix" are used, just'
    echo 'the $suffix following the colon may be specified as Username, in'
    echo 'which case all sessions with that $suffix are listed, regardless of'
    echo 'their expiry $timestamp.'
    exit 0
elif [ $# -le 1 ]
then
    ping_or_exit
    get_sessions "$@"
else
    echo "Usage: $REL_NAME sessions [Username]" >&2
    exit 2
fi
