#!/bin/bash
#############################################################################
# Test MPRIS v1.0 dbus communication with vlc
#############################################################################
# Copyright (C) 2009, M2X BV
# Author: Jean-Paul Saman <jean-paul.saman at m2x.nl>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
#############################################################################

verbose="--print-reply=3"
medialist=$*
if test -z "$medialist"; then
    echo "Provide a MRL for testing, eg: mpris_v1.sh file://myvideo.mpeg file://myvideo2.mpeg"
    echo "Make sure vlc is started before running this test script, eg: vlc --extraintf=dbus"
    exit 1
fi

mpris_methodcall()
{
    local object=$1
    local method=$2
    shift
    shift
    local args=$*

    echo -n "Object $object method org.freedesktop.MediaPlayer.$method $args...["
    dbus-send $verbose --type=method_call --dest=org.mpris.vlc $object org.freedesktop.MediaPlayer.$method $args
    if test "$?" -eq 0; then
        echo "]... pass"
    else
        echo "]... fail"
    fi
    sleep 3
}

# ROOT object
mpris_methodcall "/" "MprisVersion"
mpris_methodcall "/" "Identity"

mpris_methodcall "/Player" "GetCaps"

mpris_methodcall "/TrackList" "GetLength"
mpris_methodcall "/TrackList" "SetLoop" "boolean:true"

mpris_methodcall "/Player" "GetStatus"

count=0
for media in $medialist; do
    if test $count -eq 0; then
        mpris_methodcall "/TrackList" "AddTrack" "string:$media" "boolean:true"
    else
        mpris_methodcall "/TrackList" "AddTrack" "string:$media" "boolean:false"
    fi
    mpris_methodcall "/TrackList" "GetLength"
    mpris_methodcall "/TrackList" "GetCurrentTrack"
    mpris_methodcall "/TrackList" "GetMetadata" int32:$count
    count=$(($count+1))
    sleep 10
done

mpris_methodcall "/Player" "Next"
mpris_methodcall "/Player" "GetMetadata"
mpris_methodcall "/Player" "Prev"

mpris_methodcall "/Player" "Pause"
mpris_methodcall "/Player" "GetStatus"
mpris_methodcall "/Player" "Pause"

mpris_methodcall "/TrackList" "SetRandom" "boolean:true"

mpris_methodcall "/Player" "Next"
mpris_methodcall "/Player" "Repeat" "boolean:true"
mpris_methodcall "/Player" "GetStatus"

mpris_methodcall "/TrackList" "SetRandom" "boolean:false"
mpris_methodcall "/TrackList" "SetLoop"   "boolean:false"

mpris_methodcall "/Player" "Stop"
mpris_methodcall "/Player" "GetStatus"
mpris_methodcall "/Player" "Play"

for vol in 0 10 20 30 40 50 60 70 80 90 100; do
    mpris_methodcall "/Player" "VolumeSet" int32:$vol
done
mpris_methodcall "/Player" "VolumeSet" int32:50
mpris_methodcall "/Player" "VolumeGet"

mpris_methodcall "/Player" "PositionSet" int32:100
mpris_methodcall "/Player" "PositionGet"
mpris_methodcall "/Player" "Stop"

sleep 5
count=0
for media in $medialist; do
    mpris_methodcall "/TrackList" "DelTrack" int32:$count
    mpris_methodcall "/TrackList" "GetLength"
    count=$(($count+1))
done
mpris_methodcall "/TrackList" "GetLength"

mpris_methodcall "/" "Quit"

exit 0