/*
 * midiopts.c--parse cmd-line options for mplay. 
 *		see mplay.c for comments
 */
#include "midiwrite.h"
#include "midiopts.h"

extern MidiParameters params; 
extern MidiVariables vars; 
extern struct option *opts;
extern int fdin;
extern char fname[];
extern char *device;
extern int play, print, verbose, silent, usage, compact;
extern long sTime, eTime;

midiopts(argc, argv) int argc; char *argv[]; {
	register struct option *o = opts;

	argv++;
	while (*argv) {
		if ((o = Option(Options, &argc, argv))) {
			switch (o->index) {
								/*********stp additions-5/92*********/
			  case ARG_startTick:			/* -s = start */
				sTime = atol(o->args[0]);
				if (verbose) fprintf(stderr, "start at: %d\n", sTime);
				break;
			  case ARG_usage:				/* -u = print msg and die */
				printUsage();
				exit(0);
			  case ARG_verbose:				/* -v = verbose */
				verbose = 1;
				break;
			  case ARG_silent:				/* -z = silent */
				silent = 1;
				break;
			  case ARG_compact:				/* -c = compact input format */
				compact = 1;
				break;
			  case ARG_durTick:				/* -d = duration */
				eTime = sTime + atol(o->args[0]);
				if (verbose) fprintf(stderr, "end at: %d\n", eTime);
				break;
			  case ARG_endTick:				/* -e = end */
				eTime = atol(o->args[0]);
				if (verbose) fprintf(stderr, "end at: %d\n", eTime);
				break;
			  case ARG_playOrPrint:			/* -p = print events */
				play = 0;
				if (verbose) fprintf(stderr, "print events\n");
				break;
			  case ARG_printStatus:			/* -x = print status */
				print = 1;
				if (verbose) fprintf(stderr, "print status\n");
				break;
			  case ARG_tickRate: {
				  char *p;
				  params.midi.clockRate.tv_sec = 0;
				  params.midi.clockRate.tv_usec =  atol(o->args[0]);
			  }
				break;
								/***************standard options*************/
			  case ARG_input:
				if ((fdin = open(o->args[0], O_RDONLY)) < 0)
					perror(o->args[0]);
				strcpy(fname, o->args[0]);
				break;
			  case ARG_device:
				device = o->args[0];
				break;
			  case ARGP_mpmclockRate: {
				  char *p;
				  params.midi.clockRate.tv_sec = atoi(o->args[0]);
				  params.midi.clockRate.tv_usec = 0;
				  if ((p = (char *) index(o->args[0], '+')))
					params.midi.clockRate.tv_usec = atoi(p+1);
			  }
				break;

			  case ARGP_mpmclockMod:
				params.midi.clockModulus = atoi(o->args[0]);
				break;
			  case ARGP_mpmointc:
				params.midi.option.internalClock = 1;
				break;
			  case ARGM_mpmointc:
				params.midi.option.internalClock = 0;
				break;
			  case ARGP_mpmoautoStart:
				params.midi.option.autoStart = 1;
				break;
			  case ARGM_mpmoautoStart:
				params.midi.option.autoStart = 0;
				break;
			  case ARGP_mpmoautoStop:
				params.midi.option.autoStop = 1;
				break;
			  case ARGM_mpmoautoStop:
				params.midi.option.autoStop = 0;
				break;
			  case ARGP_mpmoinhibitClock:
				params.midi.option.inhibitClock = 1;
				break;
			  case ARGM_mpmoinhibitClock:
				params.midi.option.inhibitClock = 0;
				break;
			  case ARGP_mpmoinhibitMTC:
				params.midi.option.inhibitMTC = 1;
				break;
			  case ARGM_mpmoinhibitMTC:
				params.midi.option.inhibitMTC = 0;
				break;
			  case ARGP_mpmoinhSongPointer:
				params.midi.option.inhibitSongPointer = 1;
				break;
			  case ARGM_mpmoinhSongPointer:
				params.midi.option.inhibitSongPointer = 0;
				break;
			  case ARGP_mpmid:
				params.midi.id = atoi(o->args[0]);
				break;
			  case ARGP_mpmmtcChannel:
				params.midi.mtcChannel = atoi(o->args[0]);
				break;
			  case ARGP_mpirs:
				params.input.option.runningStatus = 1;
				break;
			  case ARGM_mpirs:
				params.input.option.runningStatus = 0;
				break;
			  case ARGP_mpiloop:
				params.input.option.loopBack = 1;
				break;
			  case ARGM_mpiloop:
				params.input.option.loopBack = 0;
				break;
			  case ARGP_mpors:
				params.output.option.runningStatus = 1;
				break;
			  case ARGM_mpors:
				params.output.option.runningStatus = 0;
				break;
			  case ARGP_mpoloop:
				params.output.option.loopBack = 1;
				break;
			  case ARGM_mpoloop:
				params.output.option.loopBack = 0;
				break;
			  case ARGP_mpstclockRate: {
				  char *p;
				  
				  params.smpte.clockRate.tv_sec = atoi(o->args[0]);
				  params.smpte.clockRate.tv_usec = 0;
				  if ((p = (char *) index(o->args[0], '+')))
					params.smpte.clockRate.tv_usec = atoi(p+1);
			  }
				break;
			  case ARGP_mpstf24:
				params.smpte.type = smpte_f24;
				break;
			  case ARGP_mpstf25:
				params.smpte.type = smpte_f25;
				break;
			  case ARGP_mpstf30:
				params.smpte.type = smpte_f30;
				break;
			  case ARGP_mpstf30drop:
				params.smpte.type = smpte_f30drop;
				break;
			  case ARGP_mpsoint:
				params.smpte.option.internalClock = 1;
				break;
			  case ARGM_mpsoint:
				params.smpte.option.internalClock = 0;
				break;
			  case ARGP_mpsoautoStart:
				params.smpte.option.autoStart = 1;
				break;
			  case ARGM_mpsoautoStart:
				params.smpte.option.autoStart = 0;
				break;
			  case ARGP_mpsoautoStop:
				params.smpte.option.autoStop = 1;
				break;
			  case ARGM_mpsoautoStop:
				params.smpte.option.autoStop = 0;
				break;
			  case ARGP_mpsodropFrame:
				params.smpte.option.dropFrame = 1;
				break;
			  case ARGM_mpsodropFrame:
				params.smpte.option.dropFrame = 0;
				break;
			  case ARGP_mpsoreverse:
				params.smpte.option.reverse = 1;
				break;
			  case ARGM_mpsoreverse:
				params.smpte.option.reverse = 0;
				break;
			  case ARGP_mpdebug:
				params.debugFlags = atoi(o->args[0]);
				break;
			  case ARGM_mvmclockTick:
				vars.midi.clockTick = atoi(o->args[0]);
				break;
			  case ARGM_mvmsongPointer:
				vars.midi.songPointer = atoi(o->args[0]);
				break;
			  case ARGM_mvmfrunning:
				vars.midi.flag.running = 0;
				break;
			  case ARGP_mvmfrunning:
				vars.midi.flag.running = 1;
				break;
			  case ARGP_mvrclock:
				vars.realtime.midiClock = 1;
				break;
			  case ARGP_mvrstart:
				vars.realtime.midiStart = 1;
				break;
			  case ARGM_mvrstop:
				vars.realtime.midiStop = 1;
				break;
			  case ARGP_mvrcontinue:
				vars.realtime.midiContinue = 1;
				break;
			  case ARGP_mvractive:
				vars.realtime.midiActive = 1;
				break;
			  case ARGP_mvrreset:
				vars.realtime.midiReset = 1;
				break;
			  case ARGP_mvrmtc:
				vars.realtime.midiReset = 3;
				break;
			  case ARGP_mvrsongPointer:
				vars.realtime.midiSongPointer = 3;
				break;
			  case ARGP_mvstime: {
				  char *p = o->args[0];
				  
				  if (p) {
					  vars.smpte.hours = atoi(p);
					  if ((p = (char *) index(p, ':'))) {
						  vars.smpte.minutes = atoi(++p);
						  if ((p = (char *) index(p, ':'))) {
							  vars.smpte.seconds = atoi(++p);
							  if ((p = (char *) index(p, ':'))) {
								  vars.smpte.frames = atoi(++p);
								  if ((p = (char *) index(p, ':'))) {
									  vars.smpte.fraction = atoi(++p);
			 	}}}}}}
				break;
			  case ARGM_mvsfrunning:
				vars.smpte.flag.running = 0;
				break;
			  case ARGP_mvsfrunning:
				vars.smpte.flag.running = 1;
				break;
			  case ARGP_mvsfqframe:
				vars.smpte.flag.qframe = atoi(o->args[0]);
				break;
			  default:
				fprintf(stderr, "unsupported option %s\n", *argv);
			}
		} else { 
							/****** new--default arg = in_file_name stp *******/
			if ((fdin = open(*argv, O_RDONLY)) < 0)
				perror(*argv);
			strcpy(fname, *argv);
			if (verbose) 
				fprintf(stderr, "Open file %s.\n", fname);
			argv++;
		}
	}
}
