﻿// indexer.cpp : Defines the entry point for the console application.
//

//=====================================================================================================================
// Includes
//=====================================================================================================================
#include "stdafx.h"
#include "StringUtil.h"
#include "CMantis.h"
#include "CInventory.h"

using namespace std;

//=====================================================================================================================
// Constants
//=====================================================================================================================
const string kAppVersion("1.0");
const std::string prompt = "indexservice >";

//=====================================================================================================================
// Main program
//=====================================================================================================================
int main(int argc, char* argv[])
{
  // Check command line arguments.
  if (argc < 2)
    {
      std::cerr << "Usage: " << argv[0]  << " <IndexServiceRoot> [noxml]\n";
      return 1;
    }

  CInventory::setRoot(argv[1]);
  // initialize
  bool noxml = false;
  if (argc > 2 && !strcmp(argv[2], "noxml"))
    noxml = true;
  
  CMantis service = CMantis(noxml);
  CInventory::Instance(0);

  cout << "READY: indexservice version " << kAppVersion << endl;
  
  int result = 0;
  while (true)
  {
    string line;
    while (true)
    {
      char ch;
      cin.get(ch);
      if (ch == '\r' || ch == '\n')
        break;
      line += ch;
    }
    if (line.length() == 0)
    {
      continue;
    }

    // Handle incoming command
    line = Trim(line);
    // cerr << "Incoming command: '" << (line.length() > 80 ? line.substr(0, 80) + "..." : line) << "'\n";

    // Pull off first word
    string cmd, remainder = "";
    size_t pos = line.find(' ');
    if (pos != string::npos)
    {
      cmd = line.substr(0, pos);
      if (line.length() > pos)
      {
        remainder = line.substr(pos + 1, line.length() - pos + 1);
      }
    }
    else
    {
      cmd = line;
    }

    // Lowercase the command
    std::transform(cmd.begin(), cmd.end(), cmd.begin(), ::tolower);

    // Dispatch commands
    if (cmd.compare("quit") == 0 || cmd.compare("exit") == 0)
    {
      cout << CMantis::CreateSuccessResponse("Exiting cleanly");
      break;
    }
    else if (cmd.compare("ver") == 0)
    {
      ostringstream msg;
      msg << "<version>" << kAppVersion << "</version>";
      cout << CMantis::CreateSuccessResponse(msg.str());
    }
    else if (cmd.compare("crash") == 0)
    {
      // Crash the index server - for testing crash recovery in the Node server.
      cout << CMantis::CreateSuccessResponse("Crashing index server!");
      cout.flush();
      int i = 1 / 0;
      break;
    }
    else
    {
      // Send everything else to CMantis
      service.ProcessCommand(cmd, remainder);
    }
  }

	return result;
}
