#!/bin/sh
#
# mpl123 - Small frontend to mpg123 using Ncurses dialog boxes.
#
# Copyright (C) 2014 Christophe Lincoln - BSD License
#
config="$HOME/.config/mpl123"
[ -f "/etc/mpl123.conf" ] && . /etc/mpl123.conf
[ -f "$config/mpl123.conf" ] && . ${config}/mpl123.conf
[ -f "./mpl123.conf" ] && . ./mpl123.conf

# Internal variables
name="$(basename $0)"
title="{ $name }"
tmpdir="/tmp/$name"
tmpfile="$tmpdir/$$"
height="20"
width="72"

# Last playlist nd radiolist
playlist="$HOME/.config/$name/playlist.m3u"
radiolist="/usr/share/$name/radiolist.txt"
[ -f "$config/radiolist.txt" ] && radiolist="$config/radiolist.txt"

# Initialize
mkdir -p ${tmpdir} ${config}
[ -f "$config/$name.conf" ] || cp /etc/${name}.conf ${config}
touch ${playlist}

#
# Functions
#

usage() {
	cat << EOT

$(boldify "Usage:") $(basename $0) [command|playlist.m3u]

$(boldify "Commands:")
  usage    $(gettext "Display this short usage")
  radio    $(gettext "Turn on/off web radio")

$(boldify "Examples:")
  $name Music/playlists/rock.m3u
  $name radio

EOT
}

# Usage: boldify "Message..."
boldify() { 
	echo -e "\033[1m$@\033[0m"
}

header() {
	clear
	echo "$(boldify 'mpl123 - Audio Player') (Ctrl+C to quit)"
	echo "================================================================================"
}

exit_player() {
	#[ "$(pidof mpg123)" ] && killall mpg123
	rm -rf ${tmpdir} && exit 0
}

read_continue() {
	echo ""
	echo -n "Press ENTER to continue..."; read
}

# Usage: edit_box "path/to/file"
edit_box() {
	local file="$1"
	dialog \
		--clear --title "{ mpl123 Editor }" \
		--ok-label "$(gettext 'Save')" \
		--editbox "$file" ${height} ${width} 2> ${tmpfile}
	case "$?" in
		1|255) continue ;;
		0) cat ${tmpfile} > ${file} ;;
	esac
}

# Usage: play_track "track.mp3"
play_track() {
	local track="$1"
	echo "Playing: $(basename "$track")"
	mpg123 -q "$track"
}

# Usage: play_list "list.m3u"
play_list() {
	local list="$1"
	count=$(cat $list | wc -l)
	header
	if [ "$count" == 0 ]; then
		gettext "Playlist is empty:"; echo " $list"
		read_continue && return 0
	fi
	sed -i "/^$/"d ${list}
	cat $list | while read track
	do
		play_track "$track"
	done
}

play_all() {
	header && cd ${MUSIC}
	find . -name "*.mp3" | mpg123 -Z --list - 2>&1 | grep "^Playing"
}

radio_switch() {
	[ "$1" == "reload" ] && killall mpg123 2>/dev/null && sleep 1
	if [ "$(pidof mpg123)" ]; then
		killall mpg123
	else
		clear && gettext "Connecting to:"; boldify " $RADIO"
		mpg123 -q http://"$RADIO" &
		gettext "Press enter to continue..."; read -t 4
	fi
}

radio_list() {
	IFS="|"
	cat $radiolist | while read radio info
	do
		[ "$info" ] || info="N/A"
		echo -e "${radio#http://}" "$info\n"
	done && unset IFS info
}

radio_list_box() {
		dialog \
		--clear --title "$title Radio" \
		--cancel-label "Menu" \
		--menu "\nList: $radiolist" ${height} ${width} 14 \
$(radio_list) 2> ${tmpfile}
	
	# Handle options
	case "$?" in
		1|255) continue ;;
		0)
			radio="$(cat ${tmpfile})" 
			sed -i s"#RADIO=.*#RADIO=\"$radio\"#" ${config}/mpl123.conf
			. ${config}/mpl123.conf
			radio_switch reload ;;
	esac
}

# Main Dialog menu
main_box() {
	# Note portable
	#volume=$(amixer get Master | egrep -o "[0-9]+%")
	dialog \
		--clear --title "$title" \
		--cancel-label "Quit" \
		--menu "\nMusic: $MUSIC" ${height} ${width} 14 \
"playall"	  "$(gettext 'Random play of all tracks')" \
"playlist"	  "$(gettext 'Listen to last playlist')" \
"radio"		  "$(gettext 'Turn on/off web radio')" \
"radiolist"   "$(gettext 'Select the radio station')" \
"mixer"		  "$(gettext 'Audio mixer')" \
"editlist"	  "$(gettext 'Edit default playlist')" \
"settings"	  "$(gettext 'Edit mpl123 settings')" 2> ${tmpfile}
	
	# Handle options
	case "$?" in
		1|255) exit_player ;;
	esac
	
	# Handle actions
	action=$(cat $tmpfile)
	case "$action" in
		playlist) play_list ${playlist} ;;
		playall) play_all ;;
		radio) radio_switch ;;
		radiolist) radio_list_box ;;
		mixer) ${MIXER} ;;
		editlist) edit_box ${playlist} ;;
		settings) edit_box ${config}/mpl123.conf ;;
	esac
}

#
# Handle commands
#

case "$1" in
		
	*.m3u)
		[ -f "$1" ] || exit 1
		play_list "$1" ;;

	radio)
		radio_switch ;;

	*usage)
		usage ;;
	
	*)
		# Main mpl123 loop
		while true; do
			main_box
		done ;;
esac

exit 0
