//==================================================================================================
// P r o g r a m                                                                          Interface
// R e n u m _ F i l e s
// 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/renum_files/console.hpp"

// Guardian //--------------------------------------------------------------------------------------
#ifndef guProgramRenumFilesConsole
#define guProgramRenumFilesConsole

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

namespace bpp {

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

// Namespaces //------------------------------------------------------------------------------------
#define public_area  programRenumFilesConsole
#define private_area programRenumFilesConsole_private

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

extern_module_name;

// Initialization //--------------------------------------------------------------------------------
#define iniProgramRenumFilesConsole
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 {}

// End //-------------------------------------------------------------------------------------------
}
#undef dll_export
#undef public_area
#undef private_area
#endif
 
//==================================================================================================
// P r o g r a m                                                                     Implementation
// R e n u m _ f i l e s
// 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/renum_files/console.cpp"

// DLL Belonging //---------------------------------------------------------------------------------
#define PROGRAM_RENUM_FILES_DLL

// Headers //---------------------------------------------------------------------------------------
#include <bpp/program/renum_files/console.hpp> /*INTERFACE*/
#include <bpp/file_name.hpp> /*NEED*/

namespace bpp {

// Namespaces //------------------------------------------------------------------------------------
#define public_area  programRenumFilesConsole
#define private_area programRenumFilesConsole_private
#define dll_export   DLL_EXPORT

namespace public_area  {}
namespace private_area {}

static_module_name("Program/Renum_files/Console");

// Initialization //--------------------------------------------------------------------------------
#undef iniProgramRenumFilesConsole
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;
  std_vector(clString) lcFormatS;

  tyCardinal lcCounter = 1;

  environment_private::goProgramName = "R e n u m   F i l e s";

  tyInteger  lcFirstNumber = 1;
  tcString   lcFolder      = ".";
  tyCardinal lcNbDigit     = 6;

  tyBoolean lcCaseSensitive = false;
  tyBoolean lcDisplayed     = true;

  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 renames files by numbering them. The files that match a",true,true);
     environment::out(" given file format are renamed according to a new file format described",true,true);
     environment::out(" by a string ('%' locates the position of the number) and a number of",true,true);
     environment::out(" digits for the numbering.",true,true);
     environment::nextLine();
     environment::out(" * Syntax: [options] [commands]",true,true);
     environment::nextLine();
     environment::out(" * Options:",true,true);
     environment::out("   -digits <n> = defines the number of digits of the numbers (the",true,true);
     environment::out("                 default value is 6).",true,true);
     environment::out("   -hide       = deactivates the display of information.",true,true);
     environment::out("   -path <f>   = defines the folder to scan.",true,true);
     environment::out("                 Without this option, the current folder is scanned.",true,true);
     environment::out("   -sensitive  = forces the scanning to be case sensitive.",true,true);
     environment::out("   -start <n>  = defines the start number of the numbering (the default",true,true);
     environment::out("                 value is 1).",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("   +change <f1> <f2> = renames the files that match the format <f1>",true,true);
     environment::out("                       according to the new format <f2>. The '%'",true,true);
     environment::out("                       character of this format locates the position",true,true);
     environment::out("                       of the file number.",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();
    }

    //----------------------------------------------------------------------------------------Digits
    else if (lcCommand=="-digits") {
     if (agCounter-lcCounter>1) {
      lcNbDigit=cardinal(agParameterS[lcCounter+1]);
      lcCounter+=1;
      lcError=false;
     }
    }

    //-----------------------------------------------------------------------------------------Start
    else if (lcCommand=="-start") {
     if (agCounter-lcCounter>1) {
      lcFirstNumber=integer(agParameterS[lcCounter+1]);
      lcCounter+=1;
      lcError=false;
     }
    }

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

    //------------------------------------------------------------------------------------------Path
    else if (lcCommand=="-path") {
     if (agCounter-lcCounter>1) {
      lcFolder=agParameterS[lcCounter+1];
      lcCounter+=1;
      lcError=false;
     }
    }

    //-------------------------------------------------------------------------------------Sensitive
    else if (lcCommand=="-sensitive") lcCaseSensitive=true;

    else if (lcCommand=="+change") {
     if (agCounter-lcCounter>2) {
      if (lcDisplayed) environment::nextLine();

      renumFiles(lcFolder,agParameterS[lcCounter+1],agParameterS[lcCounter+2],
                 lcFirstNumber,lcNbDigit,lcCaseSensitive,lcDisplayed);

      lcCounter+=2;
      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("Renum Files - Use the '+help' parameter to get some help.");
   }

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

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