// version $Id: capture_documentation.cc 8114 2002-04-16 16:11:33Z huang $
//
// include isip files
//
#include <File.h>
#include <Filename.h>
#include <Console.h>
#include <String.h>
#include <CommandLine.h>

// program to check and fix standard index.html files
//

int main(int argc, const char** argv) {

  CommandLine cmdl;
  Filename in_file_name;

  cmdl.setUsage(L"\nusage: capture_documentation [Flags] [<file_list> || <URL>]\n\n  <file_list> is the list of file names (normally via an environment variable>\n  that are to be processed.  Not needed with -reset or -help.\n  <URL> The relative URL which should be jumped to from the root web page.\n        Used with -reset.\n\nFlags:\n\n -cgi   Change filenames to eliminate \"$\" and replace \"?\" with \"-\"\n\n -reset Add an html statement to index.html to forward the browser\n        to the root of the IFC documentation pages (default) or to the\n        specified URL, instead of the isip root.\n        \n -help  Show detailed example.\n\n");

  cmdl.setHelp(L"\n\necho Running wget to get files . . . .\n\nwget -r -l50 -k -R.gz,.text -I/ -X/cgi/class,/cgi/include,/cgi/lib,/cgi/util,/search,/templates,/data,/rt,/software_checklist,/publications,/projects/jeida,/projects/nbest_pronunciations,/projects/nsf_itr,/projects/southern_accents,/projects/stock_market_simulation,/projects/switchboard,/projects/t1_interface,/projects/usfs,/projects/speech/experiments,/projects/speech/overview,/projects/speech/software,/projects/speech/support,/projects/speech/education/demos,/projects/speech/education/tutorials/asr_alphadigits,/projects/speech/education/tutorials/asr_swb,/projects/speech/education/tutorials/asr_tidigits,/projects/speech/education/toolkits,/projects/speech/education/publications,/projects/speech/education/web_stuff,/about_us,/administration,/audio,/conferences,/contact,/webapps,/whats_new -o wget.log http://www.isip.msstate.edu/projects/speech/education\n\necho Setting automatic link from root to documentation\n\ncd www.isip.msstate.edu\ncapture_documentation -reset projects/speech/education/tutorials/isip_env/index.html\ncd ..\n\necho Renaming cgi/bin files . . . .\n\ncd www.isip.msstate.edu/cgi/bin\nfiles=`ls`\ncapture_documentation -cgi $files\ncd ../../..\n\necho Setting automatic link from root to documentation\n\ncd www.isip.msstate.edu\ncapture_documentation -reset\ncd ..\n\necho Changing references to ifc_document.pl parameters\n\nfiles=`find www.isip.msstate.edu/ -name index.html -print`\ncapture_documentation $files\n\necho Building tar file . . . .\n\ntar cf ifc_documentation.tar www.isip.msstate.edu\n\necho Tar file is ifc_documentation.tar\n\n# echo Removing work files . . .\n# rm wget.log\n# rm -rf www.isip.msstate.edu\n\necho Job completed.\n\n");
  
  // set up flags for cgi vs reset vs html mode
  //
  String cgi_str(L"cgi");
  Boolean cgi_mode(false);
  cmdl.addFlagParam(cgi_mode, cgi_str);

  String reset_str(L"reset");
  Boolean reset_mode(false);
  cmdl.addFlagParam(reset_mode, reset_str);


  // parse the parameters from the commandline
  //
  cmdl.parse(argc, argv);

  if (reset_mode) {
    String buf;
    Filename in_file_name(L"index.html");
    File index_file;
    String default_URL(L"projects/speech/education/tutorials/isip_env/index.html");
    String URL;

    if (cmdl.numArguments() > 0) {
      cmdl.getArgument(URL, 0);
    }
    if (URL.eq(L"")) {
      URL.assign(default_URL);
    }

    if (!index_file.open(in_file_name, File::READ_ONLY)) {
      String msg(L"Cannot open ");
      msg.concat(in_file_name);
      msg.concat(L".");
      Console::put(msg);
    }
    else {
      File output_file;
      Filename out_file_name(L"index.out");

      if (!output_file.open(out_file_name, File::WRITE_ONLY)) {
	Error::handle(output_file.name(), L"open", Error::IO, __FILE__, __LINE__);
      }

      // insert Refresh statement after <html> tag
      //
      while (index_file.get(buf)) {
	if (buf.firstStr(L"<html>") != Integral::NO_POS) {
	  output_file.put(L"<html>\n<meta http-equiv=\"Refresh\" content=\"1; URL=");
	  output_file.put(URL);
	  output_file.put(L"\">\n");
	}
	else {
	  output_file.put(buf);
	  output_file.put(L"\n");
	}
      }
      index_file.close();
      output_file.close();
      File::remove(in_file_name);
      File::rename(out_file_name, in_file_name);	
    }
    
    // exit gracefully
    //
    return(Integral::exit());
  }

  if (cgi_mode) {
    
    Console::put(L"cgi flag detected\n");
      
    // process cgi/bin files
    //
    Filename new_file_name;
    long i=0;
    while (cmdl.getArgument(in_file_name, i++)) {
      new_file_name.assign(in_file_name);
      in_file_name.replace(L"$", L"\\$");
      boolean file_name_changed = new_file_name.replace(L"$",L"");
      file_name_changed |= new_file_name.replace(L"?",L"-");
      if (file_name_changed) {
	File::rename(in_file_name, new_file_name);
      }
    }
    
    // exit gracefully
    //
    return(Integral::exit());
  }
  
  // process html files
  //
  
  long i=0;
  while (cmdl.getArgument(in_file_name, i++)) {
    
    in_file_name.replace(L"$",L"\\$");
    
    File input_file;
    String buf;
    boolean changes_made=false;
    boolean error_flag=false;
    
    // return an error if the files cannot be opened
    //
    if (!input_file.open(in_file_name, File::READ_ONLY)) {
      String msg(L"Cannot open ");
      msg.concat(in_file_name);
      msg.concat(L".");
      Console::put(msg);
      error_flag=true;
    }
    
    if (!error_flag) {
      Console::put(in_file_name);
      
      File output_file;
      Filename out_file_name;
      
      out_file_name.assign(in_file_name);
      out_file_name.concat(L".out");
      if (!output_file.open(out_file_name, File::WRITE_ONLY)) {
	Error::handle(output_file.name(), L"open", Error::IO, __FILE__, __LINE__);
      }
      
      while (input_file.get(buf)) {
	
	// replace "?" with "-"  and "$" with ""
	// in calls to ifc_document.pl?
	//
	if (buf.firstStr(L"ifc_document.pl?")!=Integral::NO_POS) {
	  changes_made |= buf.replace(L"file=$", L"file=");
	}
	changes_made |= buf.replace(L"ifc_document.pl?", L"ifc_document.pl-");
	
	// write the line
	//
	output_file.put(buf);
	output_file.put(L"\n");
      }
      
      // close the files
      //
      input_file.close();
      output_file.close();
      
      if (changes_made) {          // if changes were made, rename
	Console::put(L"Change(s) made.\n");
	File::remove(in_file_name);
	File::rename(out_file_name, in_file_name);
      }
      else {
	Console::put(L"No changes made.\n");
	File::remove(out_file_name);
      }
    }
  }
  
  // exit gracefully
  //
  return(Integral::exit());
}
