#!/bin/bash

# mmp,a mplayer wrapper to play the music.
# http://www.vinoca.org
# 2011/09/24
#
# Licensed under GPL version 3
# 

#configfile parsing. {{{
[[ -r ~/.mmprc ]]  || echo "DIR=" >~/.mmprc && source ~/.mmprc
[[ ! -d "${DIR}" && ! -r "${DIR}" ]] &&\
		echo "default playback directory does not exist,"&&\
		echo "please set the \"DIR\" variable at ~/.mmprc ."&& exit
[[ "$DIR" =~ " " ]] && echo "There are spaces in the \"DIR\" variable."&& exit
#}}}

help() { #{{{
	cat <<EOF
mmp,a mplayer wrapper to play the music in the background.
Useage: mmp [ -s | -l | -c | -h | filename | keywords ]

  c, -c, --create      create a playlist
  l, -l, --list        list all matched tracks
  r, -r, --repeat      repeat
  s, -s, --shuffle     shuffle
  q, -q, --quit        stop mplayer

  h, -h, --help        show this help

set "DIR=dir" in ~/.mmprc to specify the default playback directory.

EOF
	exit
} #}}}

c_pl() { # create a playlist {{{
	local dir_list pl0

	if [[ -d "$1" ]]; then	# $1 is a directory
		[[ $(basename $1) = $1 ]] && pl0=$PWD/$1 || pl0=$1
		pl=(${pl[@]} $(find "$pl0"))

	else 			# $1 is a keyword
		[[ $1 =~ '/' ]] && echo 'keyword can not contain "/".'&& exit
		pl0=($(find "${DIR}" -iname "*$1*"))
		# keyword is a directory
		for ((i=0; i<${#pl0[$i]}; i++)); do
			[[ -d "${pl0[$i]}" ]] && dir_list=(${dir_list[@]} ${pl0[$i]})
		done
		for i in ${dir_list[@]}; do pl=(${pl[@]} $(find "$i")); done
		pl=(${pl[@]} ${pl0[@]})
	fi
	# delete all the directory in the "$pl" array
	pl0=${#pl[@]}
	for ((i=0; i<$pl0; i++)); do [[ -d ${pl[$i]} ]] &&unset pl[$i]; done
	return 0
} #}}}

# get opts. {{{
argv=($@)
for ((i=0; i<$#; i++)); do
	str=${argv[$i]}
	[[ $str = s || $str = -s || $str = --shuffle ]] &&\
		unset argv[$i] && arg="$arg -shuffle"
	[[ $str = r || $str = -r || $str = --repeat ]] &&\
		unset argv[$i] && arg="$arg -loop 0"
	[[ $str = q || $str = -q || $str = --quit ]] &&\
		{ pkill mplayer; exit; }
	[[ $str = c || $str = -c || $str = --create ]] &&\
		unset argv[$i] && arg=pl_out
	[[ $str = l || $str = -l || $str = --list ]] &&\
		unset argv[$i] && arg=list
done
((${#argv[@]} == 0)) && [[ ! -f "${DIR}" ]] && c_pl "${DIR}" || pl=${DIR}
if ((${#argv[@]} == 1)); then
	str=${argv[@]}
	[[ $str = h || $str = -h || $str = --help ]] && help
	[[ -f $str ]] && pl=$str || c_pl $str
else
	for i in ${argv[@]}; do c_pl $i; done
fi

((${#pl[@]} == 0)) && echo -e "\e[1;34mFind nothing. \e[1;31m@_@\e[0m\n"&& exit

# pl element is a playlist file
pl=(${pl[@]})
num=$((${#pl[@]} > 10 ?10:${#pl[@]}))
for ((i=0; i<$num; i++)); do
	[[ ! -r "${pl[$i]}" ]] && continue
	if [[ $(file "${pl[$i]}") =~ text ]]; then
		 pl=(${pl[@]} $(< ${pl[$i]}))
		 unset pl[$i]
	fi
done
pl=(${pl[@]})

# --list and --create opts.
if [[ $arg =~ list ]]; then 
	for ((i=0; i<${#pl[@]}; i++)); do pl[$i]=${pl[$i]##*/}; done
	echo "${pl[@]}" | tr -t " " '\n' | more&& exit
elif [[ $arg =~ pl_out ]]; then
	echo "${pl[@]}" | tr -t " " '\n'&& exit
fi

# show a coloured playlist
num=$((${#pl[@]} > 10 ?10:${#pl[@]}))
echo -ne "\t\t\e[1;33m+--- "
((${#pl[@]} > 10 )) &&\
	echo -ne "\e[1;34mlist top \e[1;35m$num\e[1;34m,"
echo -e "\e[1;34mtotal \e[1;35m${#pl[@]} \e[1;34mtracks \e[1;33m---\e[0m"
for ((i=1; i<=$num; i++)); do
	echo -e "  \t\t\e[1;32m$i\e[1;31m. \e[0m${pl[$i-1]##*/}"
done

# run and play
mplayer -really-quiet $arg "${pl[@]}" &
exit
#}}}
# vim: set ts=2 sw=2 fdm=marker noet:
