// src/python_cpu_policy_manager.cc - Copyright 2005, 2006, University
//               of Padova, dept. of Pure and Applied
//               Mathematics
//
// This file is part of SGPEMv2.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SGPEMv2 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#include "python_cpu_policy_manager.hh"
#include <sgpemv2/global_preferences.hh>
#include <sgpemv2/cpu_policies_gatekeeper.hh>

#include <sgpemv2/templates/deletor.tcc>

#include <Python.h>
#include <glibmm/ustring.h>
#include <glibmm/timer.h>
#include <glibmm/fileutils.h>
#include <glibmm/pattern.h>

#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <string>
#include <iostream>
#include <unistd.h>

using namespace sgpem;
using std::vector;
using std::cout;
using std::endl;


// Concatenate a string with all the policies directories
struct pol_dirs_concat : public std::unary_function<void, const Glib::ustring&>
{
public:
  pol_dirs_concat(Glib::ustring& cat) : _cat(cat) {}
  void operator()(const Glib::ustring& add)
  {
    // Please note that this string will end finishing with
    // and additional ","!
    _cat += "'" + add + "', ";
  }
private:
  Glib::ustring& _cat;
};


PythonCPUPolicyManager::PythonCPUPolicyManager()
{
  PyEval_InitThreads();

  Py_Initialize();

  // The following lines are ugly, but necessary if we use
  // non-standard installation directories. Theoretically,
  // it should be up to the user to set correct
  // environment variables.

  GlobalPreferences& prefs = GlobalPreferences::get_instance();
  Glib::ustring importdirs = "import sys\n"
                             "sys.path[:0] = [ ";
  for_each(prefs.policies_dir_begin(),
           prefs.policies_dir_end(),
           pol_dirs_concat(importdirs));
  importdirs += " '" SHAREDIR "' ]\n";

  PyRun_SimpleString(importdirs.c_str());

  // Okay, here we go.
  // Black magic at work.
  collect_policies();
}


PythonCPUPolicyManager::~PythonCPUPolicyManager()
{
  for_each(_policies.begin(), _policies.end(), memory::deletor<CPUPolicy>());
}


const vector<CPUPolicy*>&
PythonCPUPolicyManager::get_avail_policies()
{
  return _policies;
}

void
PythonCPUPolicyManager::collect_policies()
{
  GlobalPreferences& prefs = GlobalPreferences::get_instance();
  GlobalPreferences::dir_iterator dir_it = prefs.policies_dir_begin();
  GlobalPreferences::dir_iterator dir_end = prefs.policies_dir_end();

  for (; dir_it != dir_end; ++dir_it)
  {
    Glib::Dir dir(dir_it->c_str());

#ifndef NDEBUG
    std::clog << "Opening directory " << *dir_it << " looking for python policies..." << endl;
#endif

    for (Glib::DirIterator file_it = dir.begin(); file_it != dir.end(); ++file_it)
    {

      Glib::PatternSpec dot_py("*.py");

      if (dot_py.match(*file_it))
      {
#ifndef NDEBUG
	std::clog << *file_it << " appears to be a Python script. Attempting to load..." << std::endl;
#endif
        //strip extension
        std::string policy_name = (*file_it).substr(0, (*file_it).size() - 3);

        try
        {
          PythonCPUPolicy *pypolicy = new PythonCPUPolicy(policy_name.c_str());

          _policies.push_back(pypolicy);
        }
        catch (const MalformedPolicyException& e)
        {
          std::clog << "POLICY LOAD ERROR: " << e.what() << endl;
        }
      }
    }
  }
}

