//==================================================================================================
// P r o g r a m                                                                          Interface
// B u i l d _ m a k e f i l e
// C o n s o l e
//                                                                                By Bruno Bachelet
//==================================================================================================
// Copyright (c) 1999-2016
// Bruno Bachelet - bruno@nawouak.net - http://www.nawouak.net
//
// This file is part of the B++ Library. This library is free software; you can redistribute it
// and/or modify it under the terms of the GNU Library General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
// the GNU Library General Public License for more details (http://www.gnu.org).

/*DESCRIPTION*/
/* This module provides facilities to execute the program in console mode. */

// File Name //-------------------------------------------------------------------------------------
#line __LINE__ "program/build_makefile/console.hpp"

// Guardian //--------------------------------------------------------------------------------------
#ifndef guProgramBuildMakefileConsole
#define guProgramBuildMakefileConsole

// Headers //---------------------------------------------------------------------------------------
#include <bpp/meta_model.hpp> /*INCLUDE*/

namespace bpp {

// Importation/Exportation //-----------------------------------------------------------------------
#ifdef PROGRAM_BUILD_MAKEFILE_DLL
 #define dll_export DLL_EXPORT
#else
 #define dll_export DLL_IMPORT
#endif

// Namespaces //------------------------------------------------------------------------------------
#define public_area  programBuildMakefileConsole
#define private_area programBuildMakefileConsole_private

namespace public_area  { /*NAMESPACE*/ using namespace metaModel; }
namespace private_area { using namespace public_area; }

extern_module_name;

// Initialization //--------------------------------------------------------------------------------
#define iniProgramBuildMakefileConsole
has_initializer;

// Macrocommands //---------------------------------------------------------------------------------

// Types & Classes //-------------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// Functions Interface //---------------------------------------------------------------------------
namespace public_area  { function tyReturn run(tyCardinal,tcString []); }
namespace private_area {}

// Errors //----------------------------------------------------------------------------------------
namespace public_area { /*ERROR*/ extern_error erSyntax; /* Syntax error in the command line. */ }

// Constants & Variables //-------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// X X X  Interface //------------------------------------------------------------------------------
namespace {}

// Functions Inline //------------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// X X X  Inline //---------------------------------------------------------------------------------
namespace {}

// End //-------------------------------------------------------------------------------------------
}
#undef dll_export
#undef public_area
#undef private_area
#endif
 
//==================================================================================================
// P r o g r a m                                                                     Implementation
// B u i l d _ m a k e f i l e
// C o n s o l e
//                                                                                By Bruno Bachelet
//==================================================================================================
// Copyright (c) 1999-2016
// Bruno Bachelet - bruno@nawouak.net - http://www.nawouak.net
//
// This file is part of the B++ Library. This library is free software; you can redistribute it
// and/or modify it under the terms of the GNU Library General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
// the GNU Library General Public License for more details (http://www.gnu.org).

// File Name //-------------------------------------------------------------------------------------
#line __LINE__ "program/build_makefile/console.cpp"

// DLL Belonging //---------------------------------------------------------------------------------
#define PROGRAM_BUILD_MAKEFILE_DLL

// Headers //---------------------------------------------------------------------------------------
#include <bpp/program/build_makefile/console.hpp> /*INTERFACE*/

namespace bpp {

// Namespaces //------------------------------------------------------------------------------------
#define public_area  programBuildMakefileConsole
#define private_area programBuildMakefileConsole_private
#define dll_export   DLL_EXPORT

namespace public_area  {}
namespace private_area {}

static_module_name("Program/Build_makefile/Console");

// Initialization //--------------------------------------------------------------------------------
#undef iniProgramBuildMakefileConsole
static_constant(private_area::clInitializer,goInitializer);

// Errors //----------------------------------------------------------------------------------------
namespace public_area { static_error erSyntax; }

// Constants & Variables //-------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// Static Members //--------------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// Functions Implementation //----------------------------------------------------------------------
namespace public_area {
 //----------------------------------------------------------------------------------------------Run
 /*FUNCTION*/
 /* Start function of the program. It deals with the commands the user gives through a command
    line and executes the appropriate functions. */
 function tyReturn run(tyCardinal agCounter,tcString agParameterS[]) {
  method_name("run");

  clString  lcCommand;
  tyBoolean lcError;

  clDependency lcDependency;

  tyCardinal lcCounter   = 1;
  tyBoolean  lcDisplayed = true;
  tyBoolean  lcParallel  = false;

  environment_private::goProgramName = "B u i l d   M a k e f i l e";

  try {
   if (agCounter==1) send_error(erSyntax);

   while (lcCounter<agCounter) {
    lcCommand=agParameterS[lcCounter];
    lcError=true;

    //---------------------------------------------------------------------------------------Context
    if (lcCommand=="+context") {
     lcError=false;
     environment::nextLine();
     environment::outContext();
    }

    //------------------------------------------------------------------------------------------Help
    else if (lcCommand=="+help") {
     lcError=false;
     environment::nextLine();
     environment::inform("-=-=- H E L P -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
     environment::out(" This program generates a makefile from C++ files. It also gives the",true,true);
     environment::out(" dependency of a given C++ file.",true,true);
     environment::nextLine();
     environment::out(" * Syntax: [options] [commands]",true,true);
     environment::nextLine();
     environment::out(" * Options:",true,true);
     environment::out("   -hide = deactivates the display of information.",true,true);
     environment::out("   -parallel = generates a parallelizable makefile (less portable).",true,true);
     environment::nextLine();
     environment::out(" * Commands:",true,true);
     environment::out("   +context        = shows the context of the program's running.",true,true);
     environment::out("   +help           = shows this help.",true,true);
     environment::out("   +info           = shows information about the program.",true,true);
     environment::out("   +license        = shows the license of the program.",true,true);
     environment::out("   +add <a> <l>    = adds a library <l> of alias <a> to the list of",true,true);
     environment::out("                     paths to treat.",true,true);
     environment::out("   +create <f>     = creates a makefile <f> from the given libraries.",true,true);
     environment::out("   +dependency <f> = gives the dependency of the file <f>.",true,true);
     environment::out("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-",true,true);
    }

    //------------------------------------------------------------------------------------------Info
    else if (lcCommand=="+info") {
     lcError=false;
     environment::nextLine();
     environment::outInformation();
    }

    //---------------------------------------------------------------------------------------License
    else if (lcCommand=="+license") {
     lcError=false;
     environment::nextLine();
     environment::outLicense();
    }

    //------------------------------------------------------------------------------------------Hide
    else if (lcCommand=="-hide") {
     lcDisplayed=false;
     lcError=false;
    }

    //--------------------------------------------------------------------------------------Parallel
    else if (lcCommand=="-parallel") {
     lcParallel=true;
     lcError=false;
    }

    //-------------------------------------------------------------------------------------------Add
    else if (lcCommand=="+add") {
     if (agCounter-lcCounter>2) {
      lcDependency.paths().insert(clAliasX::value_type(clString(agParameterS[lcCounter+1]),
                                  clString(agParameterS[lcCounter+2])));

      lcCounter+=2;
      lcError=false;
     }
    }

    //----------------------------------------------------------------------------------------Create
    else if (lcCommand=="+create") {
     if (agCounter-lcCounter>1) {
      getDependency(lcDependency,lcDisplayed);
      makeRules(agParameterS[lcCounter+1],lcDependency,lcParallel,lcDisplayed);
      lcCounter+=1;
      lcError=false;
     }
    }

    //------------------------------------------------------------------------------------Dependency
    else if (lcCommand=="+dependency") {
     if (agCounter-lcCounter>1) {
      clFileS::const_iterator lcCurrentFile;
      clFileS                 lcFileS;
      clFileS::const_iterator lcLastFile;

      environment::nextLine();
      environment::inform("Dependency Of '",false);
      environment::out(agParameterS[lcCounter+1]);
      environment::out("':",true);
      getDependency(lcFileS,agParameterS[lcCounter+1]);
      lcCurrentFile=lcFileS.begin();
      lcLastFile=lcFileS.end();
      if (lcCurrentFile==lcLastFile) environment::out("None",true,true);

      while (lcCurrentFile!=lcLastFile) {
       environment::out((*lcCurrentFile).data(),true,true);
       lcCurrentFile++;
      }

      lcCounter+=1;
      lcError=false;
     }
    }

    if (lcError) send_error(erSyntax);
    lcCounter++;
   }
  }

  program_catch;
 }
}

namespace private_area {}

// X X X  Implementation //-------------------------------------------------------------------------
namespace {}

// I n i t i a l i z e r  Implementation //---------------------------------------------------------
namespace private_area {
 //--------------------------------------------------------------------------------------------Start
 property void clInitializer::start(void) {
  if (atCounter++ == 0) {
   try {
    #include <bpp/modules.hpp> /*NEED*/
    registerStop(this);
    environment::informInitialization(goModuleName);

    erSyntax.create("Build Makefile - Use the '+help' parameter to get some help.");
   }

   initializer_catch;
  }
 }
 //---------------------------------------------------------------------------------------------Stop
 property void clInitializer::stop(void) { environment::informTermination(goModuleName); }
}

// End //-------------------------------------------------------------------------------------------
}