#!/bin/sh
# Script which select title and album from rhythmbox
# and paste it to file ~/.fidoip/NowPlaying.txt to GoldED-NSF tagline. Install instuction:
# tar -xpf nowplay.tar -C ~/
# If not installed then install xmms from repository and also xmmsctrl 
# add to crontab -e this script */1 * * * * /home/your_login/.fidoip/rhythmbox-nowplay
# add TAGLINE      @~/.fidoip/NowPlaying.txt ; to config of GoldED-NSF
# announce the song currently playing in rhythmbox, in the format:
# Place here in SIGNITURE your TAGLINE
SIGNITURE="Rhythmbox  "
# Change also path to rhythmbox-client if it in different catalog
RYTMBOXCLIENTPATH="/usr/bin/rhythmbox-client"
# And if your teg is not in CP1251 this codepage
CODEPAGE="CP1251" 

# 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 Rhythmbox running

# Checking is Rhythmbox running
SERVICE='rhythmbox'

if ps ax | sed "/rhythmbox-nowplay/d" | sed "/rhythmbox-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


# bash splits the text into an array at any whitespace
TITLE=`export DISPLAY=:0.0 && export LANG=ru_RU.KOI8-R && $RYTMBOXCLIENTPATH --no-start --print-playing-format "%ta, %tt" | iconv -f $CODEPAGE -t KOI8R`

POSITION=(`export DISPLAY=:0.0 && $RYTMBOXCLIENTPATH --no-start --print-playing-format "%te"`)
LENGHT=(`export DISPLAY=:0.0 && $RYTMBOXCLIENTPATH --no-start --print-playing-format "%td"`)

POSITIONMIN=`echo $POSITION | sed 's|:.*||g'`
POSITIONSEC=`echo $POSITION | sed 's|.*:||g'`
POSITIONMIN2SEC=$(expr $POSITIONMIN \* 60)
DUCATIONSEC=$(expr $POSITIONMIN2SEC \+ $POSITIONSEC)
LENGHTMIN=`echo $LENGHT | sed 's|:.*||g'`
LENGHTSEC=`echo $LENGHT| sed 's|.*:||g'`
LENGHTMIN2SEC=$(expr $LENGHTMIN \* 60)
LENGHTSECALL=$(expr $LENGHTMIN2SEC \+ $LENGHTSEC)

PERDECA=`expr 10 \* "${DUCATIONSEC}"`
PERDECA=`expr "${PERDECA}" \/ "${LENGHTSECALL}"`


SLIDER="|---------"

case "$PERDECA" in
	0  ) SLIDER=">----------" ;;
	1  ) SLIDER="=>---------" ;;
	2  ) SLIDER="==>--------" ;;
	3  ) SLIDER="===>-------" ;;
	4  ) SLIDER="====>------" ;;
	5  ) SLIDER="=====>-----" ;;
	6  ) SLIDER="======>----" ;;
	7  ) SLIDER="=======>---" ;;
	8  ) SLIDER="========>--" ;;
	9  ) SLIDER="=========>-" ;;
	10 ) SLIDER="==========>" ;;
esac

TITLE=`echo $TITLE | sed "s/\(.\{47\}\).\+$/\1/"`
SIGNITURE=`echo ": "$TITLE"["${SLIDER}"]"${LENGHT}"" | sed "s|\ -\ \[|-\[|g"`
echo "Adding signuture:"
echo $SIGNITURE
echo "to file ~/.fidoip/NowPlaying.txt"
echo $SIGNITURE > ~/.fidoip/NowPlaying.txt
