// file: $isip/util/speech/isip_make_sof/isip_make_sof.cc
// version: $Id: isip_make_sof.cc 10616 2006-08-24 16:02:11Z dom5 $
//

// isip include files
//
#include "isip_make_sof.h"

// isip_make_sof: data transformation utility
//
// this is the general data transformation utility. it can be used for
// feature extraction, feature translation, or audio conversions.
//
int main(int argc, const char** argv) {

  // declare primary objects
  //
  FrontEnd fe;
  Sdb sdb;
  CommandLine cmdl(sdb);

  // declare local variables
  //
  DebugLevel debug_level;
  Filename lfile;
  Filename pfile;
  Filename input_file;
  File::TYPE type;
  DebugLevel verbosity;

  //---------------------------------------------------------------------------
  //
  // configure and parse the command line:
  //  add only the options that are not included by default. note that
  //  even though debug_level and verbosity could be buried in command line,
  //  it is about the same amount of work to specify them externally.
  //  (takes about the same amount of user code.) the names for these
  //  options, however, are standardized in command line.
  // 
  //---------------------------------------------------------------------------

  // set up messages
  //
  cmdl.setUsage(
#include "usage_message.text"
                );
  cmdl.setHelp(
#include "help_message.text"
               );

  cmdl.setIdent("$Revision: 10616 $",
		"$Name: $",
		"$Date: 2006-08-24 11:02:11 -0500 (Thu, 24 Aug 2006) $");
		 
  // set up options
  //
  cmdl.addOptionParam(debug_level, CommandLine::DEF_PARAM_DEBUG);
  cmdl.addOptionParam(lfile, CommandLine::DEF_PARAM_LFILE,
		      CommandLine::DEF_LFILE_NAME);
  cmdl.addOptionParam(pfile, CommandLine::DEF_PARAM_PFILE, FE_PARAM_FILE);
  cmdl.addOptionParam(type, CommandLine::DEF_PARAM_TYPE);
  cmdl.addOptionParam(verbosity, CommandLine::DEF_PARAM_VERBOSITY);

  // parse the command line
  //
  cmdl.parse(argc,argv);

  // configure the console
  //
  if (lfile.ne(CommandLine::DEF_LFILE_NAME)) {
    Console::open(lfile);
  }
  
  // echo the command line
  //
  if (verbosity > Integral::BRIEF) {
    cmdl.printCommandLine();
    cmdl.printVersion();
  }
  
  //---------------------------------------------------------------------------
  //
  // configure a front end
  //
  //---------------------------------------------------------------------------

  if (verbosity >= Integral::BRIEF) {
    String msg(L"loading parameter file: ");
    msg.concat(pfile);
    Console::put(msg);
  }

  if (!fe.load(pfile)) {
    String msg(L"unable to open parameter file (\n");
    msg.concat(pfile);
    msg.concat(L")");
    Console::put(msg);
    Error::handle(PROG_NAME, L"main", Error::ARG, __FILE__, __LINE__);
  }    
  
  fe.setDebug(debug_level);
  fe.setVerbosity(verbosity);

  //--------------------------------------------------------------------------
  //
  // override any parameter file parameters that were set
  // by command line options. note that not all parameters
  // that can be set by the command line appear in the parameter file.
  // at present, extension, suffix, and output type are the only
  // ones that need to be set. the others are handled directly
  // by the front end.
  //
  //---------------------------------------------------------------------------
  String cmdl_value;

  if (cmdl.getValue(cmdl_value, CommandLine::DEF_PARAM_DIRECTORY)) {
    fe.setOutputDirectory(cmdl_value);
  }

  if (cmdl.getValue(cmdl_value, CommandLine::DEF_PARAM_BASENAME)) {
    fe.setOutputBasename(cmdl_value);
  }

  if (cmdl.getValue(cmdl_value, CommandLine::DEF_PARAM_EXTENSION)) {
    fe.setOutputExtension(cmdl_value);
  }  

  if (cmdl.getValue(cmdl_value, CommandLine::DEF_PARAM_PRESERVE)) {
    long preserve;
    cmdl_value.get(preserve);
    fe.setOutputPreserve(preserve);
  }

  if (cmdl.getValue(cmdl_value, CommandLine::DEF_PARAM_SUFFIX)) {
    fe.setOutputSuffix(cmdl_value);
  }

  if (cmdl.getValue(cmdl_value, CommandLine::DEF_PARAM_TYPE)) {
    fe.setOutputType(type);
  }

  //--------------------------------------------------------------------------
  //
  // main processing loop
  //
  //---------------------------------------------------------------------------

  // initialize Sdb
  //
  if (!sdb.gotoFirst()) {
    cmdl.printUsage();
    Error::handle(PROG_NAME, L"main",
		  Error::NO_INPUT_FILE, __FILE__, __LINE__);
  }

  // increase the indention
  //
  if (verbosity >= Integral::BRIEF) {
    Console::increaseIndention();
  }
  
  // loop over all files: count the number of files processed
  //  num_files is the number of input files
  //  num_processed is the number of successfully processed files
  //
  long num_files = 0;
  long num_processed = 0;

  do {

    // fetch the next filename
    //
    sdb.getName(input_file);
    num_files++;

    // display a status message
    //
    if (verbosity >= Integral::BRIEF) {
      String output(L"processing file ");
      output.concat(num_files);
      output.concat(L": ");
      output.concat(input_file);
      Console::putNoWrap(output);
    }
    
    // process the data
    //
    if (fe.run(input_file)) {
      num_processed++;
    }
    
  } while (sdb.gotoNext());

  // decrease the indention and report results
  //
  if (verbosity >= Integral::BRIEF) {
    Console::decreaseIndention();
    cmdl.printFinalReport(num_processed, num_files);
  }
  
  // exit gracefully
  //
  Integral::exit();
}
