#!/bin/sh

usage() {
	echo "usage: smfplay [-mxy] [-d device] [-i device] midifile"
	exit 2
}

extclock=0
sendrt=0
input=""
device=$MIDIDEV
metronome="off"
tempo=0
pos=0

while getopts mxyd:i:g: optname; do
	case "$optname" in
	m)
		metronome="on";;
	x)
		extclock=1;;
	y)
		sendrt=1;;
	d)
		device=$OPTARG;;
	i)
		input=$OPTARG;;
	g)
		pos=$OPTARG;;
	esac
done
shift $(($OPTIND - 1))
if [ "$#" != "1" -o -z "$1" ]; then
	usage;
fi

exec midish -b <<EOF
import "$1"
g $pos
if "$device" {
	if "$input" {
		dnew 0 "$device" wo
	} else {
		dnew 0 "$device" rw
	}
	if $extclock == 1 {
		dclkrx 0
	}
	if $sendrt {
		dclktx {0}
	}
}
if "$input" {
	dnew 1 "$input" ro
	fnew myfilt
	fmap {any {1 0..15}} {any {0 0..15}}
}
m $metronome
p
EOF
