
#include <vector>



#include <string>
#include <fstream>
#include <vector>

#include <iostream>
#include <sstream>
#include <cfloat>
#include <stdio.h>
#include <string>
#include <stdlib.h>


using namespace std;

bool looseBinding=false, indirectRendering=false;
std::string looseBegin= "loose binding = ";
std::string indirectBegin= "indirect rendering = ";

void processLine(string line){
    string sub = line.substr(0, 21);
    if(sub.compare(indirectBegin) ==0){
        string theVal= line.substr(21,4);
        if (theVal.compare("true") == 0){
            indirectRendering=true;
        }
    }
    else {
        string sub1 = line.substr(0,16);
        if (sub1.compare(looseBegin) ==0){
            string theVal= line.substr(16,4);
        if (theVal.compare("true") == 0){
            looseBinding=true;
        }
            
            
        }
    }
    
}

bool isLooseBinding(){
    return looseBinding;
}

bool isIndirectRendering(){
    return indirectRendering;
}



string resumeCompiz(){
 string resumeCompizCmd = "/usr/bin/compiz --replace --sm-disable --ignore-desktop-hints ccp";
    if (isLooseBinding()) resumeCompizCmd += " --loose-binding";
    if (isIndirectRendering()) resumeCompizCmd += " --indirect-rendering";
    resumeCompizCmd += "&";
    return resumeCompizCmd;
}


int main(int argc, char *argv[])
{
    
  vector<string> text_file;
  string verboseStr = "";
  verboseStr = argv[1];
  bool isVerbose=false;
  if (verboseStr.compare("verbose") == 0){
      isVerbose=true;
  }
  
  string homePath = getenv("HOME");
  string fullPath = homePath + "/.config/compiz/fusion-icon";
  
  // If fusion-icon file exists
  ////////////////////////////////
  bool flag = false;
fstream fin;
fin.open(fullPath.c_str(),ios::in);
if( fin.is_open() )
{
flag=true;
}
fin.close();
if (flag){
  if (isVerbose) cout << "Fusion Icon settings detected... Disabling Compiz for World of Goo\n";
  ifstream ifs( fullPath.c_str() );
  string temp;

  while( getline( ifs, temp ) )
     text_file.push_back( temp );
  int i=0;
  while(i<text_file.size()){
  processLine(text_file.at(i));
  i++;
  }
  ifs.close();
  
  // Call compiz --replace [more parameters]
  //////////////////////////////
 system(resumeCompiz().c_str());
}

// Else Fusion settings have not been configured correctly
//////////////////////////////
else{
   if (isVerbose) cout << "fusion-icon settings not found!\n";
   system(resumeCompiz().c_str());
}


      return 0;
}



