#include "post_mus.h"

int     stave,
	staff,
	beamslope = 0,
	staff_now = 0,
	offset_now = 0,
	in_tie = 0,
	prev_key = 0,
	bar0start = 0;

struct staves	stavev[NSTAVES],
               *stavep;

main (argc, argv) int	argc;
char   *argv[];
{
    char   *p;
    argc--;
    argv++;
    while (argc > 0 && **argv == '-') {
	p = *argv;
	p++;
	switch (*p++) {
	    /* and atom_monitor get and set separately */
	    case 'v': 		/* verbosity */
		switch (*p) {
		    case 'p': 
			put_monitor = atoi (p + 1);
			break;
		    case 's': 
			set_monitor = atoi (p + 1);
			break;
		    case 'g': 
			get_monitor = atoi (p + 1);
			break;
		    default: 
			put_monitor = atoi (p);
			break;
		}
		break;
	    case 'm': 		/* atom_monitor specific atom */
		if (*p)
		    atom_monitor = atoi (p);
		break;
	    case 't':		/* testing */
		testing = 1;
		break;
	    default: 
		ioerror ("What's flag \"%c\"?\n", *--p, 0);
		abandon (1);
	}
	argc--;
	argv++;
    }

 /* global constants, not reset by execute */
    reduction = 2400 / height;	/* stave is 24 units high */
    page_width = (int) ((1.0 * width * reduction) / 100);
    clefs_now[0] = clef_now = TREBLE;
 /* default one stave, treble clef */
    n_staves = 1;
    staffv[0][0].s_clef = TREBLE;
 /* default part 0 = either, 1 = up, 2 = down, all stave 0 */
    n_parts = 3;
    partv[0].pt_end_stave = partv[1].pt_end_stave = partv[2].pt_end_stave = 0;
    partv[0].pt_updown = 0;
    partv[1].pt_updown = 1;
    partv[2].pt_updown = -1;
    part = 0;

    if (argc == 0)
	readtoeof ();
    else
	while (argc > 0) {
	    if ((file_open (*argv)) < 0)
		fprintf (stderr, "Can't read file %s\n", *argv);
	    else {
		if (put_monitor == 1)
		    fprintf (stderr, "Reading file %s fd %d\n", *argv, fd);
		readtoeof ();
	    }
	    argc--;
	    argv++;
	}
    exit( 0 );
}

readtoeof () {
    while (getline () >= 0) {
	if (within_ms) {
	    getline ();
	    execute ();
	}
	else			/* not within ".MS", ".ME" */
	    if (get_monitor == 0 && set_monitor == 0) {
		putline ();
	    }
    }
}

execute () {			/* do one piece */
    getpiece ();
    if (nerrs ()) {
	fprintf (stderr, "Abandoning after get because of errors\n");
	abandon (1);
    }
    if (!get_monitor) {
	setpiece ();
	if (nerrs ()) {
	    fprintf (stderr, "Abandoning after set because of errors\n");
	    abandon (1);
	}
	if (!set_monitor)
	    putpiece ();
    }
}

