#!/bin/bash

MPD_HOST=$HOME/.config/mpd/socket

# Only start mpd if it's not running
if [ -z $(pgrep -u $USER mpd) ]; then
	mpd
fi

# Execute and handle errors
~/alice-dj -h "$MPD_HOST"; if [ ! -z $? ]; then
	if [ -S "$MPD_HOST" ]; then
		# MPD died; wait for it to die then restart
		while [ ! -z $(pgrep -u $USER mpd) ]; do sleep 1; done
		echo "MPD died... restarting"
		exec $0
	else
		# Give up
		echo "Unrecoverable error"
	fi
fi;