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

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

SERVICE='audacious2'

if ps ax | sed "/audacious-nowplay/d" | sed "/audacious-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 && $AUDIOTOOLPATH current-song`
DATA=(`export DISPLAY=:0.0 && export LANG=ru_RU.KOI8-R && $AUDIOTOOLPATH \
playback-status \
current-song-bitrate-kbps \
current-song-output-length \
current-song-length \
current-song-output-length-seconds \
current-song-length-seconds`)
PERDECA=`expr 10 \* "${DATA[4]}"`
PERDECA=`expr "${PERDECA}" \/ "${DATA[5]}"`

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