#!/usr/local/bin/tclm -f
#
# Copyright (c) 1993 Michael B. Durian.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#	This product includes software developed by Michael B. Durian.
# 4. The name of the the Author may be used to endorse or promote 
#    products derived from this software without specific prior written 
#    permission.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

# mplay,v 1.7 1993/05/06 21:42:22 durian Exp

if {! [midiplayable]} {
	puts stderr [concat "Cannot play.  Tclm was not compiled with the " \
	    "play functionality turned on."]
	exit 1
}

set repeat 0
set tracks {}
set speed 1.0
set filenames {}

proc parse_arg {args} {
	global repeat
	global tracks
	global speed
	global filenames

	# strip away extra {}'s
	set args [lindex $args 0]
	set num_args [llength $args]
	if {$num_args > 1 && [string compare [lindex $args 0] "-f"] == 0} {
		set i 2
	} else {
		set i 0
	}
	for {} {$i < $num_args} {incr i} {

		set arg [lindex $args $i]
		case $arg in \
		-repeat {
			set repeat 1
		} -tracks {
			set tracks [lindex $args [incr i]]
		} -speed {
			set speed [lindex $args [incr i]]
		} default {
			lappend filenames $arg
		}
	}
}

parse_arg $argv

if {$repeat} {
	set repeat repeat
} else {
	set repeat ""
}

if {[llength $tracks] != 0} {
	set tracks "tracks \"$tracks\""
} else {
	set tracks ""
}

if {$speed != 1.0} {
	set speed "reltempo $speed"
} else {
	set speed ""
}

if {[llength $filenames] == 0} {
	set midi_file stdin
	set done 0
	while {1} {
		if {[catch {midiread $midi_file} mfile]} {
			if {[string compare "$mfile" "EOF"] == 0} {
				exit 0
			} else {
				puts stderr $mfile
				exit 1
			}
		}
		eval "midiplay $repeat $tracks $speed $mfile"
		midifree $mfile
	}
} else {
	foreach file $filenames {
		if {[catch {open $file} midi_file]} {
			puts stderr $midi_file
			exit 1
		}
		if {[catch {midiread $midi_file} mfile]} {
			puts stderr $mfile
			exit 1
		}
		eval "midiplay $repeat $tracks $speed $mfile"
		midifree $mfile
		close $midi_file
	}
}
exit 0
