/**************************************************************************

			   Scan & check options

       Copyright (C) 1994 by Alexandros Loghis,  All Rights Reserved

**************************************************************************/

#include <ctype.h>	/* toupper */
#include <string.h>	/* strcmp, strchr */
#include <stdlib.h>	/* atoi */

#include "gfx2grob.h"
#include "PrintMsg.h"
#include "GetOpts.h"


#ifdef AMIGA
#define ARGMAXNUMBER	4	/* Inputfile, Outputfile -a | -b | -i */
#else
#define ARGMAXNUMBER	3	/* Inputfile, Outputfile -a | -b */
#endif

static boolean TestHelpOption(char *Arg1);

/*************************************************************************/

static boolean TestHelpOption(char *Arg1)
{
  char **p;

  static char *HelpOptions[] = {  "?",	"h",  "H",
				 "-?", "-h", "-H",
				 "/?", "/h", "/H",
				 NULL
			       };


  for (p = HelpOptions; *p; p++)
    if (!strcmp(Arg1, *p)) return (TRUE);

  return (FALSE);
}

/*************************************************************************/

extern void GetOptions(int ArgC, char *ArgV[], OptionsType *Options)
{
  u_char i;

  char *pChar;

  boolean Error = FALSE;


  if ((ArgC == 1) || (ArgC > ARGMAXNUMBER + 1))
    PrintMsg(MSG_USAGE_S, ArgV[0]);
  if ((ArgC == 2) && TestHelpOption(ArgV[1])) PrintMsg(MSG_HELP);

  for (i = 1; i < ArgC; i++) {
    if (ArgV[i][0] == '-')			/* get arguments */
      switch (toupper(ArgV[i][1])) {

	case 'G' :
	  if (Options->Option == OT_NOONE) {
	    Options->Option = OT_TOASCII;
	    if (ArgV[i][2])
	      if (pChar = strchr(&ArgV[i][2], 'x')) {
		*pChar++ = '\0';
		Options->Width   = atoi(&ArgV[i][2]);
		Options->Height  = atoi(pChar);
	      }
	      else
		Error = TRUE;
	  }
	  else
	    Error = TRUE;
	break;

	case 'R' :
	  if (Options->Option == OT_NOONE)
	    Options->Option = OT_TOBIN;
	  else
	    Error = TRUE;
	break;

#ifdef AMIGA
	case 'I' :
	  if (Options->Option == OT_NOONE)
	    Options->Option = OT_TOIFF;
	  else
	    Error = TRUE;
	break;
#endif

	default :
	    Error = TRUE;
	break;
      }
    else if (!*Options->InputfileName)
	   Options->InputfileName = ArgV[i];
	 else if (!*Options->OutputfileName)
		Options->OutputfileName = ArgV[i];
	      else
		Error = TRUE;
  }
  if (Error || !*Options->InputfileName || Options->Option == OT_NOONE)
    PrintMsg(MSG_USAGE_S, ArgV[0]);
}
