/*
 *     make [-f makefile] [-ins] [target(s) ...]
 *
 *     (Better than EON mk but not quite as good as UNIX make)
 *
 *     -f makefile name
 *     -i ignore exit status
 *     -n Pretend to make
 *     -a Make everything, timestamps ignored.
 *     -p Print all macros & targets
 *     -q Question up-to-dateness of target.  Return exit status 1 if not
 *     -r Don't not use inbuilt rules
 *     -s Make silently
 *     -t Touch files instead of making them
 *     -e enviroment variables take priority over macros.
 */

#include <stdio.h>
#include "h.h"

#ifdef unix
#if !msoft
#include <sys/errno.h>
#else
#include <errno.h>
#endif
#endif

char *			myname;
char *			makefile;	/*  The make file  */

bool			domake = TRUE;  /*  Go through the motions option  */
bool			ignore = FALSE; /*  Ignore exit status option  */
bool			silent = FALSE; /*  Silent option  */
bool			print = FALSE;  /*  Print debuging information  */
bool			rules = TRUE;   /*  Use inbuilt rules  */
bool			dotouch = FALSE;/*  Touch files instead of making  */
bool			quest = FALSE;  /*  Question up-to-dateness of file  */
bool			makeall = FALSE;/*  make all */ 
bool			efirst = FALSE;	/*  enviroment variables take priority over macros */

/* from PAMAKE */

FILE			*ifile[4] = {0};
int				fln[4] = {0,0,0,0}; 
char			fname[4][80] = {"stdin"};
int				nestlvl = 0;
char			ifmessage = '#';

void main(argc, argv) int argc; char *argv[];
{
	register char			*p;		/*  For argument processing  */
	int			estat = 0;      /*  For question  */
	register struct name *  np;

	myname = (argc-- < 1) ? "make" : *argv++;

	while ((argc > 0) && (**argv == '-'))		/* dash options */
	{
		argc--;									/* One less to process	*/
		p = *argv++;    						/*  Now processing this one  */

		while (*++p != '\0')					/* while options */
		{	switch(*p) {
			case 'f':							/*  Alternate file name  */
				if (*++p == '\0')				/* if not this arg */
				{	if (argc-- <= 0) usage();	/* try next arg, else usage */
					p = *argv++;
				}
				makefile = p;					/* set makefile name */
				goto end_of_args;
			case 'n': domake  = FALSE; break;	/* Pretend mode  */
			case 'i': ignore  = TRUE;  break;	/* Ignore fault mode  */
			case 's': silent  = TRUE;  break;	/* Silent about commands  */
			case 'a': makeall = TRUE;  break;	/* Make all  */
			case 'p': print   = TRUE;  break;	/* print structures */
			case 'r': rules   = FALSE; break;	/* use built-in rules */
			case 't': dotouch = TRUE;  break;	/* touch files */
			case 'q': quest   = TRUE;  break;	/* question up-to-date */
			case 'e': efirst  = TRUE;  break;	/* environment vars first */
			default:	 /*  Wrong option  */
				usage();
			}
		}
	end_of_args:;
	}

	if (strcmp(makefile,"-") == 0)				/*  Can use stdin as makefile  */
	{	ifile[0] = stdin;
	}
	else if (!makefile)							/*  If no file, then use default */
	{	if ((ifile[0] = fopen(DEFN1, "r")) == NULL)
			fatal("Can't open %s", DEFN1);
		strncpy(fname[0],DEFN1,80);
	}
	else
	{	if ((ifile[0] = fopen(makefile, "r")) == NULL)
			fatal("Can't open %s", makefile);
		strncpy(fname[0],makefile,80);
	}

	makerules();

	setmacro("$", "$");

	while (argc && (p = index(*argv, '=')))
	{
		char		c;
		c = *p;
		*p = '\0';
		setmacro(*argv, p+1);
		*p = c;

		argv++;
		argc--;
	}

	input(); 			/*  Input all the gunga	*/
/*	fclose(ifile[0]); *//*  Finished with makefile  */
	lineno = 0; 	 	/*  Any calls to error now print no line number */

	if (print) prt();  /*  Print out structures  */

	np = newname(".SILENT");
	if (np->n_flag & N_TARG) silent = TRUE;

	np = newname(".IGNORE");
	if (np->n_flag & N_TARG) ignore = TRUE;

	precious();

	if (!firstname) fatal("No targets defined");

	circh();	 /*  Check circles in target definitions	*/

	if (!argc)
		estat = make(firstname, 0);
	else while (argc--)
	{
#if !MSDOS  /* Cute -- */
		if (!print && !silent && strcmp(*argv, "love") == 0)
			printf("Not war!\n");
#endif
		estat |= make(newname(*argv++), 0);
	}

	if (quest) exit(estat);
	else exit(0);
}

usage()
{	fprintf(stderr, "Usage: %s [-f makefile] [-inpqrst] [macro=val ...] [target(s) ...]\n", myname);
	exit(1);
}

void
fatal(msg, a1, a2, a3, a4, a5, a6)
char   *msg;
{
	fprintf(stderr, "%s: ", myname);
	fprintf(stderr, msg, a1, a2, a3, a4, a5, a6);
	fputc('\n', stderr);
	exit(1);
}
