#!/bin/bash

icon_file="/tmp/mpd_cover.jpg"
backup_file="/home/dqnid/.config/custom_icons/music.png"
final_icon_route="$icon_file"
id=5657 # HACK: dirty but works

notify() {
	mpc readpicture "$(mpc current -f %file%)" > $icon_file
	song_title=$(mpc current -f %Title%)
	song_artist=$(mpc current -f %albumartist%)
	song_duration=$(mpc current -f %time%)

	# check image
	is_image=$(cat $icon_file | grep volume | grep repeat)
	if [[ ${#is_image} > 0 ]]; then
		final_icon_route="$backup_file"
	else
		final_icon_route="$icon_file"
	fi

	case $1 in
		"playing")
			notify-send -i $final_icon_route -t 6000 -r $id "'$song_artist' is playing" " $song_title ($song_duration)"
		;;
		"paused")
			notify-send -i $final_icon_route -t 6000 -r $id "'$song_artist' is paused" " $song_title ($song_duration)"
		;;
	esac
}


while true; do
	status=$(mpc idle player &>/dev/null && mpc status)
	echo "$status" | grep "\[playing\]" &>/dev/null && notify && notify "playing"
	echo "$status" | grep "\[paused\]" &>/dev/null && notify && notify "paused"
done
