#!/bin/sh
# Script which select title and album from Amarok
# and paste it to file ~/.fidoip/NowPlaying.txt to GoldED-NSF tagline. Install instuction:
# tar -xpf nowplay.tar -C ~/
# If not installed then install Amarok, kdebase-bin from repository 
# add to crontab -e this script */1 * * * * /home/your_login/.fidoip/nowplay
# add TAGLINE      @~/.fidoip/NowPlaying.txt ; to config of GoldED-NSF
# Manually set qdbus path for your distro:
QDBUSPATH="/usr/bin/qdbus"
# Place here in SIGNITURE your TAGLINE
SIGNITURE="     "

# Here we check that you are not root user:
USERNAME=`whoami`
T1="root"

if [ "$T1" = "$USERNAME" ]; then
echo  'Please do not run this script as root' ; exit
fi

# Checking is Amarok running
SERVICE='amarok'

if ps ax | sed "/nowplay/d" | sed "/start/d" | grep -v grep | grep $SERVICE  > /dev/null
then
    echo "$SERVICE running"
else
    echo "$SERVICE is not running"
# My standart signiture
    echo "Adding my standart signiture"
    echo $SIGNITURE > ~/.fidoip/NowPlaying.txt
    exit
fi


# Checking is Amarock is playing something.
AMAROKSTATUS=`export DISPLAY=:0.0 && $QDBUSPATH --literal org.kde.amarok /Player org.freedesktop.MediaPlayer.GetStatus`
echo $AMAROKSTATUS


if [ "$AMAROKSTATUS" = "[Argument: (iiii) 0, 0, 0, 0]"  ]; then
echo "Amarok is playing."
echo 
else
echo "Amarok is not playing. Adding standart signiture"
echo $SIGNITURE > ~/.fidoip/NowPlaying.txt
exit
fi


#Getting Track number from Amarok
TRACKNUMBER=`export DISPLAY=:0.0 && $QDBUSPATH org.kde.amarok /Player org.freedesktop.MediaPlayer.GetMetadata | grep tracknumber: | sed "s|tracknumber:||"`
#Getting duration of track from Amarok
DURATION_OF_TRACK=`export DISPLAY=:0.0 && $QDBUSPATH org.kde.amarok /Player org.freedesktop.MediaPlayer.GetMetadata | grep mtime: | sed "s|mtime:||"`
YEAR_OF_ALBUM=`export DISPLAY=:0.0 && $QDBUSPATH org.kde.amarok /Player org.freedesktop.MediaPlayer.GetMetadata | grep year: | sed "s|year:||"`

# Get info about what is Amarok playing now and ganre
TITLE=`export DISPLAY=:0.0 && $QDBUSPATH org.kde.amarok /Player org.freedesktop.MediaPlayer.GetMetadata | grep title: | sed "s|title: ||"`
GENRE=`export DISPLAY=:0.0 && export LANG=ru_RU.KOI8-R && $QDBUSPATH org.kde.amarok /Player org.freedesktop.MediaPlayer.GetMetadata | grep genre: | sed "s|genre:||"`
ALBUM=`export DISPLAY=:0.0 && $QDBUSPATH org.kde.amarok /Player org.freedesktop.MediaPlayer.GetMetadata | grep album: | sed "s|album:||"`
ARTIST=`export DISPLAY=:0.0 && $QDBUSPATH org.kde.amarok /Player org.freedesktop.MediaPlayer.GetMetadata | grep artist: | sed "s|artist:||"`


PRINTENV=":"

if [ "$ARTIST" = " " ]
then
echo
else
PRINTENV=""$PRINTENV""$ARTIST", "
fi

if [ "$TITLE" = " " ]
then
echo
else
PRINTENV=""$PRINTENV". \""$TITLE"\" "
fi

#if [ "$ALBUM" = " " ]
#then
#echo
#else
#PRINTENV=""$PRINTENV" ."$ALBUM" "
#fi

PRINTENV=`echo $PRINTENV | sed "s| ,||g"`
PRINTENV=`echo $PRINTENV | sed "s|()\,\ \.\ \.\ \.\ \, \.\ \.\ \, \.\ \"\ ( \,\ )\"\ \.\ (\ \,\ )||g"`
PRINTENV=`echo $PRINTENV | sed "s|()\,\ \.\ \"\ \"\ \.||g"`
PRINTENV=`echo $PRINTENV | sed "s|()\,||g"`
PRINTENV=`echo $PRINTENV | sed "s|\.\ \.||g"`
PRINTENV=`echo $PRINTENV | sed "s|\.\ \,||g"`
PRINTENV=`echo $PRINTENV | sed "s|\.\ \.||g"`
PRINTENV=`echo $PRINTENV | sed "s|\.\ \.||g"`
PRINTENV=`echo $PRINTENV | sed "s|\ \,\ ||g"`
PRINTENV=`echo $PRINTENV | sed "s|\.\ \"\ (\ \,\ )\"||g"`
PRINTENV=`echo $PRINTENV | sed "s|\ \"\ ||g"`
PRINTENV=`echo $PRINTENV | sed "s|\,\ \-\ \"||g"`
PRINTENV=`echo $PRINTENV | sed "s|\"\,\ \"|\"|g"`
PRINTENV=`echo $PRINTENV | sed "s|\"\,\ \-|\"|g"`

PRINTENV=`echo $TITLE | sed "s/\(.\{75\}\).\+$/\1/"`
echo $PRINTENV > ~/.fidoip/NowPlaying.txt
echo $PRINTENV
