/*	Reads arguments from CLI or Workbench, and loads the specified puzzle.
	
	Martin J. Round January 1990
*/

char line[MAXLINE];

extern struct WBStartup *WBenchMsg;

extern void message (UBYTE *);
extern void print (char *);
extern struct Screen *ReadILBM(BPTR);

extern int runningfromcli;

extern struct IntuiText instrtext[6];
extern int sqrsacross,sqrsdown;
extern unsigned char start [MAXACROSS] [MAXDOWN];
extern unsigned char goal  [MAXACROSS] [MAXDOWN];
extern int topleftx,toplefty;
extern int sqrwidth,sqrheight;
extern int slide;
extern int scorex,scorey,scoreapen,scorebpen;

int readline(BPTR fp, char *p, int max)	/*	returns # chars. -1 = fail	*/
	{
	int i = 0;
	char c;

	--max;
	
	if (Read(fp,&c,1) <=0)
		return (-1);

	while ((c != '\n') && (i < max))
		{
		*p++ = c;
		i++;
		if (Read(fp,&c,1) <=0)
			return (-1);
		}
	
	*p = 0;
	
	return(i);
	}
	
int value(char *p)
	{	
	return (atoi(strtok(stpblk(p)," ;")));
	}

int readfi(BPTR fp)	/*	1 = not a puzz, 2 = wrong ver. 3 = bad	*/
	{
	int i,j;
	char *p;
	
	if ((i = readline(fp,line,MAXLINE)) < 8) return (1);
	
	if (strncmp(line,"PUZZ ",5)) return (1);
	
	if (strncmp(line,"PUZZ 1.0",8)) return (2);
	
	for (i=0; i<6; i++)
		{
		if (readline(fp,line,MAXLINE) < 0) return(3);
		strncpy((instrtext[i]).IText,line,26);
		}
	
	if (readline(fp,line,MAXLINE) < 0) return(3);
	sqrsacross = value(line);

	if (readline(fp,line,MAXLINE) < 0) return(3);
	sqrsdown = value(line);
	
	for (j=0; j<sqrsdown; j++)
		{
		if (readline(fp,line,MAXLINE) < 0) return(3);
		p = strtok(stpblk(line)," ,");
		start [0] [j] = (unsigned char) atoi(p);
		for (i=1; i<sqrsacross; i++)
			{
			if (i < (sqrsacross-1))
				p = strtok(NULL," ,");
			else
				p = strtok(NULL," ;");
			start [i] [j] = (unsigned char) atoi(p);
			}
		}

	for (j=0; j<sqrsdown; j++)
		{
		if (readline(fp,line,MAXLINE) < 0) return(3);
		p = strtok(stpblk(line)," ,");
		goal [0] [j] = (unsigned char) atoi(p);
		for (i=1; i<sqrsacross; i++)
			{
			if (i < (sqrsacross-1))
				p = strtok(NULL," ,");
			else
				p = strtok(NULL," ;");
			goal [i] [j] = (unsigned char) atoi(p);
			}
		}

	if (readline(fp,line,MAXLINE) < 0) return(3);
	topleftx = value(line);

	if (readline(fp,line,MAXLINE) < 0) return(3);
	toplefty = value(line);

	if (readline(fp,line,MAXLINE) < 0) return(3);
	sqrwidth = value(line);

	if (readline(fp,line,MAXLINE) < 0) return(3);
	sqrheight = value(line);

	if (readline(fp,line,MAXLINE) < 0) return(3);
	slide = value(line);
	
	if (readline(fp,line,MAXLINE) < 0) return(3);
	scorex = value(line);
	
	if (readline(fp,line,MAXLINE) < 0) return(3);
	scorey = value(line);
	
	if (readline(fp,line,MAXLINE) < 0) return(3);
	scoreapen = value(line);
	
	if (readline(fp,line,MAXLINE) < 0) return(3);
	scorebpen = value(line);
	
	do
		{
		if (readline(fp,line,MAXLINE) < 0) return(3);
		}	while (strncmp(line,"**ILBM**",8));

	return(0);
	}

struct Screen *readfile(BPTR fp,char *file)
	{
	struct Screen *screen;
	
	switch (readfi(fp))
		{
		case 1:
			sprintf(line,"%s is not a puzzle file.",file);
			message(line);
			Close(fp);
			return(NULL);
			break;
		case 2:
			sprintf(&(line[8])," or later is required.");
			message(line);
			Close(fp);
			return(NULL);
			break;
		case 3:
			sprintf(line,"%s has wrong format.",file);
			message(line);
			Close(fp);
			return(NULL);
			break;
		}
	if ((screen = ReadILBM(fp)) == NULL)
		message("Bad or Missing ILBM section.");
	Close(fp);
	return(screen);
	}

struct Screen *loadpuzzle (int argc,char **argv)	/*	NULL = failed	*/
	{
	BPTR fp,olddir;
	struct Screen *screen;
	
	/*	Check we've got something to do	*/
	if (runningfromcli && argc == 1)
		{
		print("Useage:      PUZZ puzzlename\n\n");
		print("For example: PUZZ 15.puzzle\n\n");
		return(NULL);
		}

	if (!runningfromcli && ((WBenchMsg==NULL) || (WBenchMsg->sm_NumArgs < 2)))
			{
			message ("You must click on a puzzle icon - try '15.puzzle'.");
			return(NULL);
			}

	if (runningfromcli)
		{
		if ((fp=Open(argv[1],MODE_OLDFILE))==NULL)
			{
			sprintf(line,"Can't open file: %s\n",argv[1]);
			print(line);
			return(NULL);
			}
		else
			{
			screen = readfile(fp,argv[1]);
			}

		}
	else
		{	/*	running from Workbench	*/
		olddir = CurrentDir (WBenchMsg->sm_ArgList[1].wa_Lock);
		if
			(
				(fp = Open(WBenchMsg->sm_ArgList[1].wa_Name,MODE_OLDFILE))
				== NULL
			)
			{
			sprintf
				(
				line,
				"Can't open file: %s",
				WBenchMsg->sm_ArgList[1].wa_Name
				);
				
			message(line);
			if (olddir) CurrentDir(olddir);
			return(NULL);
			}
		else
			{
			screen = readfile(fp,WBenchMsg->sm_ArgList[1].wa_Name);
			}
		if (olddir) CurrentDir(olddir);
		}
	return (screen);
	}