#!/bin/bash

PORT=$1
SERVER=$2
TARGET=$3

if [ ! `dcop amarok` ];
then
  dcop $PORT Konversation error "Amarok not running."
  exit;
fi

if [ ! $TARGET ]
then
  dcop $PORT Konversation error "Can't write into status view."
else
  STATUS=`dcop amarok player isPlaying`
  
  if [ $STATUS == "false" ]
  then
    dcop $PORT Konversation error "Nothing's playing in Amarok."
  else  
    TEXT=`dcop amarok player nowPlaying`

    TOTAL=`dcop amarok player trackTotalTime`
    let TMIN=$TOTAL/60
    let TSEC=$TOTAL%60
    if [ $TSEC -lt 10 ]; then
      TSEC="0$TSEC"
    fi

    CURR=`dcop amarok player trackCurrentTime`
    let CMIN=$CURR/60
    let CSEC=$CURR%60
    if [ $CSEC -lt 10 ]; then
      CSEC="0$CSEC"
    fi

    dcop $PORT Konversation say $SERVER "$TARGET" "/me plays $TEXT [$CMIN:$CSEC/$TMIN:$TSEC]"
  fi
fi
