#ifndef lint
static char sccsid[] = "@(#)main.c	1.11 (mg@ukc.ac.uk) 5/24/90";
#endif
#include "cs.h"			/*					MAIN.C	*/
#include "soundio.h"

int	odebug = 0;		/* orch control switches	*/
int	initonly = 0;		/*   and default values		*/
int	sfwrite = 1;
int	output_format = OF_SHORT;
int	sampsize = 2, bufsmps = OUTBUFSIZ/2;
int	ftdisplay = 1;
int	dsploption = 1;		/* text output by default */
int	msglevel = 7;
char	*outfile;
int	outfd;		/* sound output file descriptor, if "-N -" given */
FILE	*scorefp;	/* file pointer open to score file */

main(argc,argv)
 int argc;
 char **argv;
{
	register char c, *s;
#ifdef SYS5
	char buf[80];
#endif
	extern char getenv();

	if (!(--argc))
		die("insufficient orch arguments");
	
#ifdef XWINDOWS
	/* Select X support autoimatically if we are in an X environment */
	if (getenv("DISPLAY")) dsploption = 2;
#endif

	do {
		s = *++argv;
		if (*s++ != '-')		/* read all orch flags:  */
			break;
		while ((c = *s++) != '\0')
			switch(c) {
			case 'i': initonly = 1;		/* i-only implies */
			case 'n': sfwrite = 0;		/* nosound	  */
				  break;
			case 'f': output_format = OF_FLOAT;/* sndfile floats */
				  sampsize = 4;
				  bufsmps = OUTBUFSIZ/4;
				  break;
#ifdef ULAW
			case 'u':
				output_format = OF_ULAW;
				sampsize = 1;
				bufsmps = OUTBUFSIZ;
				break;
#endif
#ifdef XWINDOWS

			case 'e': {
				extern int x_single_step;
				x_single_step = 1;
				break;
				  }
#endif
			case 'v': odebug = 1;		/* verbose otran  */
				  break;
			case 't': ftdisplay = 0;	/* no ftable dsplays */
				  break;
			case 'd': sscanf(s,"%d",&dsploption);
				  while (*++s);
				  break;
			case 'm': sscanf(s,"%d",&msglevel);
				  while (*++s);
				  break;
			case 'k': sscanf(s,"%f",&ekr);	/* control rate */
				  while (*++s);
				  break;
			case 's': sscanf(s,"%f",&esr);	/* sample rate */
				  while (*++s);
				  break;
				
			case 'N': outfile = s;		/* soundfile name */
				  while (*++s);
				  if (strcmp(outfile, "-") == 0) {
					/* Do it now before any output */
					/* Divert all future writes to fd1 to
					 * stderr; send sound output to the
					 * stream inherited as fd 1 */
					outfd = dup(1);
					if (outfd < 0) die("too many open files");
					dup2(2,1); /* point stdout at stderr */
				  }
				  break;
			default:  fprintf(stderr,"csound: Unknown flag \"-%c\".\n",c);
				  die("unknown orch flag");
			}
	} while (--argc);

	/* find out where to read score from */
	switch (argc) {
	case 0: die("insufficient orch arguments");
		break;
	case 1:	scorefp = stdin;
		break;
	case 2:	scorefp = fopen(*++argv, "r");
		if (scorefp == NULL) {
			char buf[64];
			sprintf(buf, "csound: Cannot open score file \"%s\".",
				*argv);
			die(buf);
		}
		break;
	default:
		die("csound: too many command-line arguments.");
	}

#ifdef SYS5
	if (odebug)
	    setvbuf(stdout,buf,_IOLBF,80);
#else
#ifndef THINK_C
	if (odebug)
	    setlinebuf(stdout);
#endif
#endif

#ifdef XWINDOWS
	switch (dsploption) {
	case 2:
	case 3:
		x_init();
		break;
	}
#endif

	musmon(--s);

#ifdef XWINDOWS
	if (dsploption == 3) {
		x_wait_for_button();
	}
#endif
	return(0);
}
