#include <resultset.h>
#include <iostream>
#include <fstream>
#include <string>
#include "ps.h"

#define HOST "127.0.0.1"
#define USER "root"
#define PASS "blah"
#define DB   "plum_data"

using namespace std;

string drawRack(string rack,string location,string module);

int main(int argc, char** argv){

  if(argc!=2 && argc!=6)
    {
      cout<<"Usage: "<<argv[0]<<" <filename> [<host> <db> <user> <pass>]\n";
      return 1;
    }

  string host=(argc==6?argv[2]:HOST);
  string   db=(argc==6?argv[3]:DB);
  string user=(argc==6?argv[4]:USER);
  string pass=(argc==6?argv[5]:PASS);

  ofstream out;out.open(argv[1]);
  printFile(out);

  MYSQL conn=ResultSet::makeConn(host,user,pass,db);
  ResultSet res(&conn);
  res.query<<"select racks.acronym as rack, modules.acronym as module, locations.name as location "
	   <<"from racks, locations, modules "
	   <<"where racks.location=locations.idx and locations.module=modules.idx "
	   <<"order by module";
  res.store();
  while(res.next())
    {
      //cout<<res["rack"]<<", "<<res["location"]<<", "<<res["module"]<<'\n';
      if(((string)res["location"]).length()==6)
	out<<drawRack(res["rack"],res["location"],res["module"])<<'\n';
    }

  out<<"showpage\n";
  out.close();
}


string drawRack(string rack,string location,string module){
  string line="("+location+") ("+rack+") ";
  int loc=location[5]-'0';

  if(module=="AIR")
    {
      line+="AIR ";
      line+="0 ";
      switch(location[4])
	{
	case 'O':line+="3 ";break;
	case 'A':line+="2 ";break;
	case 'D':line+="1 ";break;
	case 'F':line+="0 ";break;
	}
      line+="rack";
    }
  else if(module=="APM")
    {
      line+="APM ";
      line+=4-loc+'0';
      switch(location[4])
	{
	case 'O':line+=" 3";break;
	case 'A':line+=" 2";break;
	case 'D':line+=" 1";break;
	case 'F':line+=" 0";break;
	}
      line+=" rack";
    }
  else if(module=="CAM")
    {
      line+="CAM ";
      switch(location[4])
	{
	case 'A':line+="0 ";break;
	case 'F':line+="1 ";break;
	case 'P':line+="2 ";break;
	case 'S':line+="3 ";break;
	}
      line+=4-loc+'0';
      line+=" rack";
    }
  else if(module=="HAB")
    {
      line+="HAB ";
      line+=6-loc+'0';
      switch(location[4])
	{
	case 'O':line+=" 3";break;
	case 'A':line+=" 2";break;
	case 'D':line+=" 1";break;
	case 'F':line+=" 0";break;
	}
      line+=" rack";
    }
  else if(module=="JEM")
    {
      line+="JEM ";
      line+=6-loc+'0';
      switch(location[4])
	{
	case 'O':line+=" 3";break;
	case 'A':line+=" 2";break;
	case 'D':line+=" 1";break;
	case 'F':line+=" 0";break;
	}
      line+=" rack";
    }
  else if(module=="JEM-ELM-PS")
    {
      line+="JEMELMPS ";
      switch(location[4])
	{
	case 'A':line+="0 ";break;
	case 'F':line+="1 ";break;
	case 'P':line+="2 ";break;
	case 'S':line+="3 ";break;
	}
      line+=2-loc+'0';
      line+=" rack";
    }
  else if(module=="LAB")
    {
      line+="LAB ";
      line+=6-loc+'0';
      switch(location[4])
	{
	case 'O':line+=" 3";break;
	case 'P':line+=" 2";break;
	case 'D':line+=" 1";break;
	case 'S':line+=" 0";break;
	}
      line+=" rack";
    }
  else return "";

  return line;
}
