#include "make.h"

/*----------------------------------------------------------------*/
char *GetFn( argc, argv )
int *argc;
char ***argv;
   {
   /* is there a character after the current command char ? */
   if ( *( (*argv)[0]+2 ) )  /* skip over the -<c> */
      return( (*argv)[0]+2 );

   if (*argc == 0 || *((*argv)[1])== '-' )
      err ( "File name for -%lc option missing\n", *( (*argv)[0]+1) );

   (*argc)--;
   return( *( ++(*argv)) );
   }

/*----------------------------------------------------------------*/
main( argc, argv )
int argc;
char **argv;
   {
   char *MakeFileName, *BuiltInFile, *BatchFileName, *s, *First;
   char PrintTree, *ReadFile();

   /* copy _main opened files to tiny stdio handles */
   stdin  = Input();
   stdout = Output();
   stderr = Open("*", MODE_NEWFILE);

   PrintTree      = 0;
   BatchFileName  = NULL;
   BuiltInFile    = BUILTINS;
   MakeFileName   = MAKEFILE;
   cmdbuff        = gmem(MAXCMD);

   /* first process the arguments */
   while( --argc > 0 && (*++argv)[0] == '-')
      {
      s = argv[0]+1;
      switch (*s) {
         case 'a':  /* rebuild all targets regardless of status */
             AlwaysRebuild = 1;
             break;

         case 'b':  /* specify name of built in rules file */
             BuiltInFile = GetFn(&argc, &argv);
             break;

         case 'c': /* create a batch file of all commands */
             CreateBatch = 1;
             NoExecuteMode = 1;
             BatchFileName = GetFn(&argc, &argv);
             break;

#ifdef DEBUF
         case 'd':  /* debug mode - print info on times and files examined */
             DebugMode = 1;
             break;
#endif

         case 'f':  /* specify name of makefile */
             MakeFileName = GetFn(&argc, &argv);
             break;

         case 'i':  /* ignore any errors returned by commands */
             IgnoreErrors = 1;
             break;

         case 'k':  /* on error stop work on this branch only */
             ContinueBranches = 1;
             break;

         case 'n':  /* print commands but do not execute them */
             NoExecuteMode = 1;
             break;

#if 0
         case 'o':
             RespectCase = 1;
             break;
#endif
         case 'p':  /* print out the complete set of macro definitions */
             PrintMakeFile = 1;
             break;

         case 'q':  /* say whether target file is up to date or not in rc */
             QuestionMode = 1;
             break;

         case 'r':  /* do not use the built in rules file */
             BuiltInFile = NULL;
             break;

         case 's':  /* do not print command lines before executing */
             SilentMode = 1;
             break;

#ifdef DEBUG
         case 't':  /* touch the target files rather than issue commands */
             err("Updating times not yet supported\n");
             break;

         case 'w':  /* Walk all the dependancy trees printing the contents*/
             PrintTree = 1;
             break;
#endif
         default:
            err("Invalid command line option '%lc'\n", *s);
         }
      }

   /* first read in any default rules file */
   if (BuiltInFile != NULL)
      ReadFile( BuiltInFile );

   First = ReadFile( MakeFileName );

   Inputline = 0;

   if (BatchFileName != NULL)
      {
      if ( (OutputFile = Open(BatchFileName, "a")) == NULL)
         err("Unable to append to '%s'", BatchFileName);
      }
   else
      OutputFile = Output();

#ifdef DEBUG
   if (PrintTree)
      trav( Root );

   if (DebugMode)
      {
      msg("First one to make is '%s'", First);
      msg(" argc=%ld\n", argc);
#endif

   while( argc-- > 0 )
      {
      First = NULL;

#ifdef DEBUG
      if (DebugMode)
         msg("Going to make '%s'\n", argv[0]);
#endif

      make( argv[0] );
      argv++;
      }

   if (First != NULL)
      make( First );

   exit(0);
   }

/*----------------------------------------------------------------*/
exit(rc)
{
	_exit(rc);
}

_exit(rc)
{
 	Close(stderr);
	XCEXIT(rc);
}
