/*****************************************************************************
*	    Change Log
*  Date	    | Change
*-----------+-----------------------------------------------------------------
* 12-Mar-86 | Created
*****************************************************************************/
/*   Record: scan command line, call phase1, trans2 */

#include "cext.h"
#include "stdio.h"
#include "adagio.h"
#include "phase1.h"
#include "phase2.h"
#include "atxt.h"
#include "userio.h"
#include "cmdline.h"

char score_na[name_length];	/* this will be available to phase1 and 2 */
char outfile[name_length];	/* output file name used by trans2 */

/* Variables set by command line switches */

extern int musictrace;
extern int miditrace;
extern int IsAT;
extern int debug_intr;
extern int debug_rec;
boolean poll = false;		/* true if poll, false if interrupt */
boolean no_mpu = false;		/* true if simulated */

#define nswitches 15
private char *switches[nswitches] = 
    { "-debug", "-d", "-help", "-miditrace", "-m", "-trace", "-t", "-at",
      "-metronome", "-met", "-simulated", "-s", "-xt", "-print", "-block" };

#define noptions 5
private char *options[noptions] = { "-c", "-control", "-out", "-o", "-tune" };

#define nmsw 2
private char *msw[nmsw] = { "-met", "-metronome" };

#define noopt 2
private char *oopt[noopt] = { "-o", "-out" };

#define nssw 2
private char *ssw[nssw] = { "-simulated", "-s" };

/****************************************************************************
*	Routines local to this module
****************************************************************************/
private void cmdline_help();

/****************************************************************************
*				 cmdline_help
* Effect: 
*	Prints out command line help
****************************************************************************/

private void cmdline_help()
{
    fprintf(stderr,"adagio [options] filename [options]\n");
    fprintf(stderr,"	 Options are below.  Those with * are for wizrds:\n");
    fprintf(stderr,"*	   -at		     PC/AT (secondary interrupt channel)\n");
    fprintf(stderr,"	   -block	     disable MIDI thru\n");
    fprintf(stderr,"	   -debug (-d)	     enable verbose debug mode\n");
    fprintf(stderr,"	   -help	     this message\n");
    fprintf(stderr,"	   -metronome (-met) turn on MPU-401 metronome\n");
    fprintf(stderr,"	   -miditrace (-m)   turn on MIDI command trace\n");
    fprintf(stderr,"	   -print	     print note data\n");
    fprintf(stderr,"	   -tune file	     use tuning from file\n");
    fprintf(stderr,"	   -simulated (-s)   run with simulated MPU-401\n");
    fprintf(stderr,"	   -trace (-t)	     trace music\n");
    fprintf(stderr,"*	   -xt		     PC/XT (no secondary interrupt channel)\n");
}

/****************************************************************************
*				     main
* Inputs:
*	int argc: Count of arguments
*	char * argv[]: Vector of argument pointers
* Effect: 
*	Runs record program.
****************************************************************************/

void main(argc,argv)
   int argc;
   char * argv[];
{
    FILE *fp = NULL;
    char *s;	/* temp pointer to input and output file names */

    cl_init(switches, nswitches, options, noptions, argv, argc);

    outfile[0] = NULL;	/* initialize file names to null strings */
    score_na[0] = NULL;

    if (cl_switch("-help")) {
	cmdline_help(); 
	return;
    }

    IsAT = (ATXT() == ISAT);
    if (cl_switch("-at")) {
	if (!IsAT) { /* lie! */
	    fprintf(stderr,"-at but not running on PC/AT!\n");
	    return;
	}
    }

    if (cl_nswitch(msw, nmsw) != NULL)
	 metronome(true);

    s = cl_noption(oopt, noopt);
    if (s != NULL) strcpy(outfile, s);

    if (cl_switch("-help")) {
	cmdline_help(); 
	return;
    }

    if (cl_nswitch(ssw, nssw) != NULL) no_mpu = true;

    if (cl_switch("-xt")) {
	if (ISAT) { /* lie! */
	    fprintf(stderr,"-xt but running on PC/AT!\n");
	    return;
	}
    }

    if ((s = cl_arg(1)) != NULL) strcpy(score_na, s);

    fp = fileopen(score_na, "gio", "r", "Name of Adagio score file");
    phase2(phase1(fp));
    return;
}
