/* main.c - The main function for the glulx assembler */

#include "glulxa.h"
#include "globals.h"
#include "opt.h"
#include <ctype.h>
#include <string.h>

#ifdef STANDALONE
int main (int argc, char * argv[])
#else
int glulxamain (int argc, char * argv[])
#endif
{
  int c, err_flag = 2;
  extern char	*optarg;
	extern int	optind;

  while ((c = getopt(argc, argv, "i:o:")) != -1)
		switch(tolower(toascii(c)))
		{
    case '?':
      err_flag ++;
      break;
      
    case 'i':
      infilename = strdup(optarg);
      err_flag --;
      break;
      
    case 'o':
      outfilename = strdup(optarg);
      err_flag --;
      break;

    default:
      err_flag ++;
      break;
    }

  if (err_flag)
  {
    printf ("FATAL - arguments error on invocation.\n\n");
    printf ("syntax :\n");
    printf ("glulxa -i <sourcefile> -o <gamefile>\n");
    
    exit (-1);
  }

  CreateOpcodeTable();
  
  OpenFiles();

  strcpy (filename, infilename);

  /* Do the first pass.  Create offsets, table of labels */
  pass = 1;
  Assemble();

  /* Allocate blocks for the data and code sections */
  AllocateMemory();
  
  CorrectLabels();
  InitHeader();

  if (!asserrors)
  {
    /* Do the second pass. Finalise the code and data sections */
    pass = 2;
    Assemble();
    
    Finalise();
    
    OutputGameFile();
  }
  
  CloseFiles();

  return 0;
}


  
  
  
  
