#!/bin/bash

launcher_options () {
	launcher=totem
	music_widget=yes
	mpd_music_directory=/var/lib/mpd/music
	mpd_port=6600
	mpd_host=127.0.0.1
}

launcher () {
	#Music Players Support
	music_launcher () {
		if [[ $1 = --add ]]; then
			case $launcher in
				amarok | audacious )	option=-e	;;
				mocp )					option=-a	;;
				mpd | sonata | pygmy | gmpc | ncmpc | mpc )
										option=add	;;
				totem )					option=--enqueue
			esac
			shift
		else
			case $launcher in
				amarok )				option=-lp	;;
				mocp )					option=-cap	;;
				#exaile )				option=--play	;;
				listen )				option=-q
			esac
		fi

		case $launcher in
			mpd | sonata | pygmy | gmpc | ncmpc | mpc )
				[[ $option ]] && mpc clear
				mpc ls $(echo "$1" | sed -e "s|$mpd_music_directory/||" -e "s| |_|g") | mpc add
				[[ $option ]] && mpc play
			;;
			muine | exaile )
				find "$1" -maxdepth 1 -type f -iname '*.mp3' -or -iname '*.aac' -or -iname '*.m4a' -or -iname '*.ogg' -or -iname '*.flac' -or -iname '*.wma' | sort > $HOME/.avatar-factory/playing.m3u
				$launcher $option $HOME/.avatar-factory/playing.m3u
			;;
			quodlibet )
				ps -C $(which quodlibet) >& /dev/null
			  #not running:
				if [[ $? != 0 ]]; then
					$launcher &
					sleep 5
				fi

				quodlibet --play-file="$1"
			;;
			listen )
				ps -C listen >& /dev/null
			  #not running:
				if [[ $? != 0 ]]; then
					$launcher &
					sleep 5
				fi

				$launcher $option "$1"
			;;
			banshee | audacious | mocp | * )
				[[ $launcher = banshee ]] && option="--play --enqueue"
				local IFS=$'\n'
				files=($(find "$1" -maxdepth 1 -type f -iname '*.mp3' -or -iname '*.aac' -or -iname '*.m4a' -or -iname '*.ogg' -or -iname '*.flac' -or -iname '*.wma' | sort))
				$launcher $option "${files[@]}"
		esac
	}

	[[ ${launcher##*-} = add ]] && add=--add && launcher=${launcher%-add}

	#############Music Widget

	if [ "$music_widget" = "yes" ]; then
		icon_PATH_old=$(find -L $HOME/.avatar-factory/icons -maxdepth 1 -name 'music_widget*' | tail -n 1)
		number=$(echo $icon_PATH_old | sed -e 's/.*music_widget//' -e 's/\..*//')

		if [[ -z $number || $number = 99 ]]; then
			number=1
		else
			number=$((number+1))
		fi

		icon_PATH=$HOME/.avatar-factory/icons/music_widget$number.png
	fi

	music_w () {
		if [[ -d $HOME/Desktop ]]; then
			Desktop=Desktop
		else
			Desktop=$(cat ~/.config/user-dirs.dirs | grep XDG_DESKTOP_DIR | sed -e 's|.*/||' -e 's/"//')
		fi

		convert  \
			-size 333x156 null: \
			\( "$icon" -background  none \) -gravity west -composite \
			"$icon_PATH"

		cat > "$HOME/$Desktop/music_widget.desktop" << message
[Desktop Entry]
Encoding=UTF-8
Name=$widget_name
X-MultipleArgs=false
Type=Application
Exec=avatar-factory -al music mw --URL "$URL" --name "$avatar_title" --icon "$icon_PATH" --DND
Icon=$icon_PATH
URL=file://$URL
widget_icon=$icon_PATH

Title=$avatar_title
message

		rm $icon_PATH_old
	}

	pile_w () {
		if [[ -d $HOME/Desktop ]]; then
			Desktop=Desktop
		else
			Desktop=$(cat ~/.config/user-dirs.dirs | grep XDG_DESKTOP_DIR | sed -e 's|.*/||' -e 's/"//')
		fi

		if [[ ${DND##*.} = desktop ]]; then
			icon_DND=$(grep '^Icon=' "$DND" | sed 's/^Icon=//')
			URL_DND=$(grep '^URL=' "$DND" | sed 's|^URL=file://||')
			if [[ $(echo "$icon_DND" | cut -d/ -f6) != music ]]; then
				zenity --error --text "Not a music avatar" &
				exit
			fi
		else
			zenity --error --text "Not a music avatar" &
			exit
		fi



		case ${X[1]} in
			mw )
				pile_count=2
				album_rotation=30
	#			widget_width=208
				widget_width=333
				album_offset="-geometry +41+0"
			;;
			2 )
				pile_count=3
				album_rotation=-15
	#			widget_width=256
				widget_width=333
				album_offset="-geometry +103+0"
			;;
			3 )
				pile_count=4
				album_rotation=-30
				widget_width=333
				album_offset="-gravity east"
			;;
			4 )
				pile_count=5
				album_rotation=20
				widget_width=333
				album_offset="-geometry +20+0"
			;;
			5 )
				pile_count=6
				album_rotation=-45
				widget_width=333
				album_offset="-geometry +133+0"
			;;
			6 )
				pile_count=7
				album_rotation=0
				widget_width=333
				album_offset="-gravity west"
			;;
			7 )
				pile_count=8
				album_rotation=30
				widget_width=333
				album_offset="-geometry +41+0"
			;;
			8 )
				pile_count=3
				album_rotation=-15
				widget_width=333
				album_offset="-geometry +103+0"
		esac

		convert  \
			-size "$widget_width"x156 null: \
			\( "$icon" -background  none \) -gravity west -composite \
			\( "$icon_DND" -background  none -rotate $album_rotation \) $album_offset -composite \
			"$icon_PATH"

		URL_add="$(grep '^URL_add=' $HOME/$Desktop/music_widget.desktop)"

		cat > "$HOME/$Desktop/music_widget.desktop" << message
[Desktop Entry]
Encoding=UTF-8
Name=$widget_name
X-MultipleArgs=false
Type=Application
Exec=avatar-factory -al music $pile_count --URL "$URL" --name "$avatar_title" --icon "$icon_PATH" --DND
Icon=$icon_PATH
URL=file://$URL
widget_icon=$icon_PATH

Title=$avatar_title

$URL_add
URL_add=$URL_DND
message

		rm $icon_PATH_old
	}


	#############Flow

	if [ -n "${X[1]}" ]; then
		if [[ -n $DND ]]; then
			pile_w
			music_launcher --add "$URL_DND" &
		else
			music_launcher $add "$URL" &
			if [[ ${X[1]} != mw ]]; then
				URL_add="$(grep '^URL_add=' $HOME/Desktop/music_widget.desktop | sed 's|^URL_add=||')"
				echo "$URL_add" | while read -r URL ; do
							music_launcher --add "$URL"
						done
			fi
		fi
	elif [ "$music_widget" = "yes" ]; then
		music_launcher $add "$URL" &
		music_w
	else
		music_launcher $add "$URL" &
	fi
}
