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

#include "cmt_constituent.h"
#include "cmt_generator.h"
#include "cmt_system.h"
#include "cmt_database.h"

/*----------------------------------------------------------*/
/*                                                          */
/*  Operations on Constituent                               */
/*                                                          */
/*----------------------------------------------------------*/

//----------------------------------------------------------
void Constituent::show (const cmt_string& name)
{
  Constituent* cptr = find (name);
  if (cptr == 0) return;

  const Constituent& constituent = *cptr;

  constituent.show ();
}

//----------------------------------------------------------
void Constituent::parse_all ()
{
  static ConstituentVector& Constituents = constituents ();

  int number;

  for (number = 0; number < Constituents.size (); number++)
    {
      Constituent& constituent = Constituents[number];

      constituent.parse ();
    }
}

//----------------------------------------------------------
void Constituent::show_all ()
{
  static ConstituentVector& Constituents = constituents ();

  int number;

  for (number = 0; number < Constituents.size (); number++)
    {
      const Constituent& constituent = Constituents[number];

      constituent.show ();
    }
}

//----------------------------------------------------------
void Constituent::show_names ()
{
  static ConstituentVector& Constituents = constituents ();

  int number;

  for (number = 0; number < Constituents.size (); number++)
    {
      Constituent& constituent = Constituents[number];
      cout << constituent.name << endl;
    }
}

//----------------------------------------------------------
Constituent* Constituent::find (const cmt_string& name)
{
  static ConstituentVector& Constituents = constituents ();

  int constituent_index;

  if (Constituents.size () == 0) return (0);

  for (constituent_index = 0;
       constituent_index < Constituents.size ();
       constituent_index++)
    {
      Constituent& constituent = Constituents[constituent_index];

      if (constituent.name == name)
        {
          return (&constituent);
        }
    }

  return (0);
}

class constituents_action_iterator
{
public:
  typedef enum
  {
    ready,
    need_include
  } states;

  constituents_action_iterator (Constituent& c) : m_constituent (c)
  {
    m_state = ready;
  }

  void set (const cmt_string& w)
  {
    int equal;

    if (w == "") return;

    if (m_state == need_include)
      {
        m_state = ready;

        cmt_string& include = m_constituent.includes.add ();
        include = w;
      }

    if (w == "-OS9")
      {
        m_constituent.need_OS9 = true;
      }
    else if ((w == "-Windows") ||
             (w == "-windows"))
      {
        m_constituent.windows = true;
      }
    else if (w == "-no_share")
      {
        m_constituent.no_share = true;
      }
    else if (w == "-no_static")
      {
        m_constituent.no_static = true;
      }
    else if (w == "-prototypes")
      {
        m_constituent.need_prototypes = true;
      }
    else if (w == "-no_prototypes")
      {
        m_constituent.need_prototypes = false;
      }
    else if (w == "-check")
      {
        m_constituent.need_check = true;
      }
    else if (w == "-triggers")
      {
        if (m_constituent.type == Library)
          {
            m_constituent.build_triggers = true;
          }
      }
    else if (w == "-no_triggers")
      {
        if (m_constituent.type == Library)
          {
            m_constituent.build_triggers = false;
          }
      }
    else if (w == "-I")
      {
        m_state = need_include;
      }
    else if (w.substr (0, 3) == "-s=")
      {
        w.substr (3, m_subdir);
      }
    else if (w.substr (0, 8) == "-import=")
      {
        cmt_string& import = m_constituent.imports.add ();
        w.substr (8, import);
      }
    else if (w.substr (0, 7) == "-group=")
      {
        cmt_string group_name = "";

        w.substr (7, group_name);
        
        m_constituent.group = Group::add (group_name);
      }
    else if (w.substr (0, 8) == "-suffix=")
      {
        w.substr (8, m_constituent.suffix);
      }
    else if ((equal = w.find ("=")) != cmt_string::npos)
      {
        cmt_string variable_name;
        cmt_string variable_value;
        
        w.substr (0, equal, variable_name);
        w.substr (equal + 1, variable_value);
        
        Variable* v = Variable::find (m_constituent.variables, variable_name);
        if (v == 0)
          {
            v = &(m_constituent.variables.add ());
            v->set (variable_name);
          }

        (*v) = variable_value;
      }
    else
      {
        // We have a normal source module
 
        cmt_string& module = m_constituent.modules.add ();
        
        module.erase (0);
          
        //
        // The prefix explicitly provided in (w) has priority
        // over the currently specified (m_subdir) when it is an
        // absolute path
        //
        if (CmtSystem::absolute_path (w))
          {
            module += w;
          }
        else
          {
            cmt_string prefix;
            cmt_string name = w;

            CmtSystem::dirname (name, prefix);
            if (prefix == "../src") CmtSystem::basename (name, name);

            module += m_subdir;

            if (module != "")
              {
                module += CmtSystem::file_separator ();
              }

            module += name;
          }
      }
  }

  Constituent& m_constituent;
  cmt_string m_subdir;
  states m_state;
};

//----------------------------------------------------------
void Constituent::action (ConstituentType type,
                          const CmtSystem::cmt_string_vector& words)
{
  cmt_string generator;
  cmt_string name;
  Constituent* constituent;

  int i = 1;

  if (type == Document)
    {
      generator = words[i];
      if (generator == "") return;
      i++;
    }

  name = words[i];
  if (name == "") return;
  i++;

  constituent = add (type, name, generator);

  for (;i < words.size (); i++)
    {
      const cmt_string& w = words[i];
      cmt_string& parameter = constituent->parameters.add ();
      parameter = w;
    }
}

//----------------------------------------------------------
void Constituent::parse ()
{
  if (parameters.size () == 0) return;

  Constituent& me = *this;

  modules.clear ();

  constituents_action_iterator it (me);

  for (int i = 0; i < parameters.size (); i++)
    {
      const cmt_string& w = parameters[i];
      cmt_string ew = w;

      Symbol::expand (ew);
      if (ew != w)
        {
          CmtSystem::cmt_string_vector ws;

          CmtSystem::split (ew, " ", ws);

          for (int j = 0; j < ws.size (); ++j)
            {
              const cmt_string& w = ws[j];
              it.set (w);
            }
          
          continue;
        }

      it.set (w);
    }

  parameters.clear ();
}

//----------------------------------------------------------
Constituent* Constituent::add (ConstituentType type,
                               const cmt_string& name,
                               const cmt_string& generator)
{
  static ConstituentVector& Constituents = constituents ();

  {
    Constituent* constituent;

    if (name == "") return (0);

    constituent = find (name);
    if (constituent != 0) return (constituent);
  }

  Constituent& constituent = Constituents.add ();
  constituent.clear ();

  constituent.name      = name;
  constituent.generator = generator;
  constituent.type      = type;
  constituent.need_prototypes = Cmt::need_prototypes ();

  return (&constituent);
}


//----------------------------------------------------------
void Constituent::clear_all ()
{
  static ConstituentVector& Constituents = constituents ();

  for (int i = 0; i < Constituents.size (); i++)
    {
      Constituent& c = Constituents[i];
      c.clear ();
    }
  Constituents.clear ();
}

//----------------------------------------------------------
Constituent::ConstituentVector& Constituent::constituents ()
{
  static Database& db = Database::instance ();
  static ConstituentVector& Constituents = db.constituents ();

  return (Constituents);
}

//----------------------------------------------------------
Constituent::Constituent ()
{
  clear ();
}

//----------------------------------------------------------
Constituent::~Constituent ()
{
}

//----------------------------------------------------------
void Constituent::clear ()
{
  name      = "";
  generator = "";
  type = Document;
  group     = 0;
  modules.clear ();
  parameters.clear ();
  need_OS9        = false;
  windows         = false;
  no_static       = false;
  no_share        = false;
  need_prototypes = false;
  need_check      = false;
  build_triggers  = false;
  includes.clear ();
  imports.clear ();
  variables.clear ();
}

//----------------------------------------------------------
void Constituent::build_all_makefiles (bool simulation)
{
  static ConstituentVector& Constituents = constituents ();

  int i;

  for (i = 0; i < Constituents.size (); i++)
    {
      Constituent& constituent = Constituents[i];

      constituent.build_makefile (simulation);
    }
}

//----------------------------------------------------------
void Constituent::build_all_msdev_files (bool simulation)
{
  static ConstituentVector& Constituents = constituents ();

  int i;

  Generator::build_msdev_workspace (Constituents);

  for (i = 0; i < Constituents.size (); i++)
    {
      Constituent& constituent = Constituents[i];

      constituent.build_msdev_file (simulation);
    }
}

//----------------------------------------------------------
void Constituent::build_makefile (bool simulation) const
{
  if (!simulation)
    {
      Generator::build_constituent_makefile (*this);
    }
    //else cout << command << endl;
}

//----------------------------------------------------------
void Constituent::build_msdev_file (bool simulation) const
{
  if (!simulation)
    {
      Generator::build_msdev (*this);
    }
    //else cout << command << endl;
}

//----------------------------------------------------------
void Constituent::show () const
{
  int i;

  switch (type)
    {
      case Library:
        cout << "library";
        break;
      case Application:
        cout << "application";
        break;
      case Document:
        cout << "document " << generator;
        break;
    }
  
  cout << " " << name;
  
  if (group != 0)
    {
      cout << " -group=" << group->name ();
    }
  
  if (suffix != 0)
    {
      cout << " -suffix=" << suffix;
    }
  
  if ((type == Application) && need_check)
    {
      cout << " -check";
    }
  
  if ((type == Library) && no_share)
    {
      cout << " -no_share";
    }
  
  if ((type == Library) && no_static)
    {
      cout << " -no_static";
    }
  
  if ((type == Library) && build_triggers)
    {
      cout << " -triggers";
    }
  
  for (i = 0; i < (modules.size ()); i++)
    {
      const cmt_string& module_name = modules[i];
      
      cout << " " << module_name;
    }
  
  for (i = 0; i < (variables.size ()); i++)
    {
      const Variable& v = variables[i];
      
      cout << " " << v.name << "=" << v.value;
    }
  
  cout << endl;
}
