#!/bin/sh
# Script which select title and album from xmms
# 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/xmms-nowplay
# add TAGLINE      @~/.fidoip/NowPlaying.txt ; to config of GoldED-NSF
# announce the song currently playing in xmms, in the format:
# Place here in SIGNITURE your TAGLINE
SIGNITURE="Xmms  "
# Change also path to xmmsctrl if it in different catalog
XMMSCTRLPATH="/usr/bin/xmmsctrl"
# And if your teg is not in CP1251 this codepage
CODEPAGE="WINDOWS-1251" 

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

SERVICE='xmms'

if ps ax | sed "/xmms-nowplay/d" | sed "/xmms-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 && $XMMSCTRLPATH print %T | iconv -f $CODEPAGE -t KOI8R`
POSITION=(`export DISPLAY=:0.0 && $XMMSCTRLPATH print %s`)
LENGHT=(`export DISPLAY=:0.0 && $XMMSCTRLPATH print %S`)
LENGHTMINUTES=(`export DISPLAY=:0.0 && $XMMSCTRLPATH print %M`)
PERDECA=`expr 10 \* "${POSITION}"`
PERDECA=`expr "${PERDECA}" \/ "${LENGHT}"`

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}"]"${LENGHTMINUTES}"" | sed "s|\ -\ \[|-\[|g"`
echo "Adding signuture:"
echo $SIGNITURE
echo "to file ~/.fidoip/NowPlaying.txt"
echo $SIGNITURE > ~/.fidoip/NowPlaying.txt
