/*
 * Copyright (C) 2008 The Trustees of Columbia University in the City of New York. 
 * All rights reserved.
 *
 * See the file LICENSE included in this distribution for details.
 */


#include "p2pconfig.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef _WENGO
#include "sysutils.h"
#endif

#ifdef _WENGO
#include "p2pp/sys/sysutils.h"
#endif

#include <string>
using namespace std;

P2PConfig::P2PConfig() {

}

P2PConfig::~P2PConfig() {

}

int P2PConfig::parseFile(char* pchFile) {
  FILE* fp;
  char c;
  int ret, flag;
  string first="", second="", token="";

  //Initialize to default values
  m_BASE=2;

  //open the config file
  fp = fopen(pchFile, "r");
  if(NULL == fp) {
    printf("Error: cannot open config file: %s\n", pchFile);
    exit(-1);
  }
  
  flag=0;
  while(!feof(fp)) {
    ret=fread(&c, 1, 1, fp);

    if(flag==3 && c!='\n'){
      continue;
    }
    switch(c) {
    case ' ':
    case '\t': 
      token=""; 
      if(flag==0 && !first.empty())
	flag=1;
      if(flag==1 && !second.empty())
	flag=3;
      break;
    case '\r':
    case '\n': token=""; flag=2;
      break;
    case '#':
      flag=3; 
      break;
    default:
      token += c;
    }
    if(feof(fp)){
      token=""; flag=2;
    }
    
    if(0 == flag) {
      first = token;
    } else if(1 == flag) {
      second = token;
    } else if(2 == flag){
      //take care of pure comment line
      if(first.empty()){
	flag=0;
	continue;
      }
      //Peer ID
      if(0 == strcasecmp("ID",first.c_str())) {
	//objNode.setID((char*)second.c_str());
        strcpy(m_ID, (char*)second.c_str()); //unsafe

      } else if(0 == strcasecmp("TCP_PORT",first.c_str())) { //TCP port
        m_TCP_PORT = atoi(second.c_str());

      } else if(0 == strcasecmp("UDP_PORT",first.c_str())) { //UDP port
        m_UDP_PORT = atoi(second.c_str());
	
      } else if(0 == strcasecmp("P2P",first.c_str())) { //P2P algorithm

        char* pchP2P = (char*)second.c_str();
        if(0 == strcasecmp("Chord",pchP2P)) {
          m_P2P = eCHORD;
        } else if(0 == strcasecmp("Kademlia", pchP2P)) {
          m_P2P = eKADEMLIA;
        } else if(0 == strcasecmp("Bamboo", pchP2P)) {
          m_P2P = eBAMBOO;
        }
	
      } else if(0 == strcasecmp("HASH_ALG",first.c_str())) { //HASH algorithm

        char* pchAlg = (char*)second.c_str();
        if(0 == strcasecmp("None", pchAlg)) {
          m_HASH_ALG = eNONE;
        }
        else if(0 == strcasecmp("SHA1", pchAlg)) {
          m_HASH_ALG = eA_SHA1;
        }
        else if(0 == strcasecmp("MD5", pchAlg)) {
          m_HASH_ALG = eA_MD5;
        }
        else if(0 == strcasecmp("MD4", pchAlg)) {
          m_HASH_ALG = eA_MD4;
        }
        else if(0 == strcasecmp("SHA256", pchAlg)) {
          m_HASH_ALG = eA_SHA256;
        }
      } else if(0 == strcasecmp("HASH_SIZE",first.c_str())) { //HASH SIZE
        m_HASH_SIZE = atoi(second.c_str());
	
      } else if(0 == strcasecmp("REC",first.c_str())) {
        m_REC = atoi(second.c_str());	

      } else if(0 == strcasecmp("PARALLEL",first.c_str())) {
        m_PARALLEL = atoi(second.c_str());	
	
      } else if(0 == strcasecmp("CMD",first.c_str())) {
        m_CMDLINE = atoi(second.c_str());
	
      } else if(0 == strcasecmp("MODE",first.c_str())) {
        
        char* pchMode = (char*)second.c_str();
        
        if(0 == strcasecmp("BOOTSTRAP", pchMode)) {
          m_NODE_TYPE = eBOOTSTRAP_SERVER;
        } else if(0 == strcasecmp("PEER", pchMode)) {
          m_NODE_TYPE = ePEER;
        } else if(0 == strcasecmp("CLIENT", pchMode)) {
          m_NODE_TYPE = eCLIENT;
        } else if(0 == strcasecmp("DIAGNOSTIC_SERVER", pchMode)) {
          m_NODE_TYPE = eDIAGNOSTIC_SERVER;
        } else if(0 == strcasecmp("AUTO", pchMode)) {
          m_NODE_TYPE = eAUTO;
        }

      } else if(0 == strcasecmp("BOOTIP",first.c_str())) { //BOOTIP
        strncpy(m_BOOT_IP, (char*)second.c_str(), 16);

      } else if(0 == strcasecmp("BOOTPORT",first.c_str())) { //BOOTIP
        m_BOOT_PORT = atoi((char*)second.c_str());
        
      } else if(0 == strcasecmp("USERID", first.c_str())) {
        strcpy((char*)m_USERID, (char*)second.c_str());
      }
      else if(0 == strcasecmp("BASE", first.c_str())) {
        m_BASE = atoi((char*)second.c_str());
      } else if(0 == strcasecmp("DIAGNOSTICS_FILE", first.c_str())) {
        strcpy((char*)m_Diagnosticsfile,(char*)second.c_str());
      }
      //printf("first: %s second: %s\n", first.c_str(), second.c_str());
      first.clear();second.clear();
      flag=0;
    }

  }//end while

#ifndef DEBUG 
  #ifdef _WENGO
  m_CMDLINE = 0;
  #endif
#endif

  fclose(fp);
  return 1;

}//parseFile
