#!/bin/bash
### Original script

## Author: WiseLord
## License: Public Domain
## Version: 0.2

###Modified script:

##Made by kTorpi << ktorpi@gmail.com >>
##and
##Made by Marcus << marcus@elitemail.hu >>

## Last edited, Moviuro, 2012.04.11 << moviuro@gmail.com >>

##Depens on sh, amarok2
#SERVER=$1;
#TARGET=$2;
message=""
nowPlaying="$(qdbus org.kde.amarok /Player org.freedesktop.MediaPlayer.GetMetadata 2>/dev/null)"

if [ -n "${nowPlaying}" ]
then
        version="$(qdbus org.kde.amarok / org.freedesktop.MediaPlayer.Identity)"
        nlength="$(qdbus org.kde.amarok /Player org.freedesktop.MediaPlayer.PositionGet)"
        nlength=$((nlength/1000))
                                                                                                                                                                                                                                                                               
#         title="$(echo "${nowPlaying}" | sed -ne 's/^title: \(.*\)$/\1/p')"                                                                                                                                                                                                     
#         artist="$(echo "${nowPlaying}" | sed -ne 's/^artist: \(.*\)$/\1/p')"                                                                                                                                                                                                   
#         bitrate="$(echo "${nowPlaying}" | sed -ne 's/^audio\-bitrate: \(.*\)$/\1/p')"                                                                                                                                                                                          
#         album="$(echo "${nowPlaying}" | sed -ne 's/^album: \(.*\)$/\1/p')"                                                                                                                                                                                                     
        length="$(echo "${nowPlaying}" | sed -ne 's/^time: \(.*\)$/\1/p')"                                                                                                                                                                                                     
#         year="$(echo "${nowPlaying}" | sed -ne 's/^year: \(.*\)$/\1/p')"                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                               
         #The length sec part                                                                                                                                                                                                                                                  
        secv=$(($length%60))                                                                                                                                                                                                                                                   
        if [ ${secv} -lt 10 ]                                                                                                                                                                                                                                                  
        then                                                                                                                                                                                                                                                                   
                sec="0${secv}"                                                                                                                                                                                                                                                 
        else                                                                                                                                                                                                                                                                   
                sec="${secv}"                                                                                                                                                                                                                                                  
        fi                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                               
        #The lengt min part                                                                                                                                                                                                                                                    
        minv=$(($length/60))                                                                                                                                                                                                                                                   
        if [ ${minv} -lt 10 ]                                                                                                                                                                                                                                                  
        then                                                                                                                                                                                                                                                                   
                min="0${minv}"                                                                                                                                                                                                                                                 
        else                                                                                                                                                                                                                                                                   
                min="${minv}"                                                                                                                                                                                                                                                  
        fi                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                               
        #elapsed time sec part                                                                                                                                                                                                                                                 
        secn=$(($nlength%60))                                                                                                                                                                                                                                                  
        if [ ${secn} -lt 10 ]                                                                                                                                                                                                                                                  
        then                                                                                                                                                                                                                                                                   
                nsec="0${secn}"                                                                                                                                                                                                                                                
        else                                                                                                                                                                                                                                                                   
                nsec="${secn}"                                                                                                                                                                                                                                                 
        fi                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                               
        #elapsed time min part
        minn=$(($nlength/60))
        if [ ${minn} -lt 10 ]
        then
                nmin="0${minn}"
        else
                nmin="${minn}"
        fi

#         if [ ${length} == 0 ]
#         then
#                 progressbar=""
#         else        
#                 # % of the elapsed time
#                 rate=$(((nlength*100)/length))
#         
#                 # progressbar
#                 progressbar="["
#                 nrate=$((rate/10))
#                 for ((i=1;i<=${nrate};i++))
#                 do
#                         progressbar="${progressbar}-"
#                 done
#                 progressbar="${progressbar}|"
#                 for((i=1;i<(10-nrate);i++))
#                 do
#                         progressbar="${progressbar}-"
#                 done
#                 progressbar="${progressbar}] "
#         fi
        
#         message="$(whoami) is playing: ${artist} - ${title}"
#         if [ -n "${album}" ]
#         then
#                 message="${message} (${album})"
#         fi
#         if [ ${year} -gt  0 ]
#         then
#                 message="${message} from ${year}"
#         fi

        message="[ ${nmin}:${nsec}/${min}:${sec} ]" #This line was added by Moviuro for the conky config file "Monochrome_simple"
        #The message in DC: [13:44:20] * [OP]Marcus is playing: Cascada - Evacuate The Dancefloor (Evacuate The Dancefloor) from 2009 [00:06/03:27] [|---------] [1078 kbps] :: Amarok 2.4.0

##This part is from the original script.
                                                                                                                                                                                                                        
## Also you can send song magnet to chat:
# [17:45:03] * WiseLord is listening now: Therion - Midgård (02. Midgård.mp3) (7.0 МиБ)
# You can use <magnet show=NAME_TO_SHOW>PATH_TO_FILE</magnet> or just <magnet>PATH_TO_FILE</magnet>                                                                                                                                          
# If you want to do this, uncomment 2 lines below:                                                                                                                                                                                           
                                                                                                                                                                                                                                             
#       location="$(echo "${nowPlaying}" | sed -ne 's/^location: file:\/\/\(.*\)$/\1/p' | sed -e s/\'/\\\\\'/g -e 's/%\([0-9A-Fa-f][0-9A-Fa-f]\)/\\\\\x\1/g' | xargs echo -e )"                                                              
#       message="/me is listening now: ${artist} - ${title} ( <magnet>${location}</magnet> )"                                                                                                                                                
fi                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                             
echo "${message}" 

#echo ${message} |  while read line; do qdbus org.kde.konversation /irc say $SERVER "$TARGET" "$line"; done