#!/bin/sh

if test "$1" = "-nosub" ; then
	sub=1
	shift
elif test "$1" == "--" ; then
	shift
fi

if test -z "$1" ; then
	echo "usage: mplay [-nosub|--] PATH [MPLAYER OPTIONS]"
	false
else
	for i in $1/* ; do
		if test -z "$mov" && echo "$i" | grep -i "\.\(avi\|mpg\|asf\|mov\)$" ; then
			mov="$i"
		elif test -z "$sub" && echo "$i" | grep -i "\.\(sub\|txt\|mps\)$" ; then
			sub="$i"
		fi
		test -n "$mov" -a -n "$sub" && break
	done
	shift
	if test -z "$mov" ; then
		echo "$0: $i: no movie found"
		false
	else
		if test "$sub" = "1" ; then
			mplayer $* -sub /dev/null "$mov"
		elif test -z "$sub" ; then
			mplayer $* "$mov"
		else
			mplayer $* -sub "$sub" "$mov"
		fi
	fi
fi
