#include "PDBobject.h"

using namespace std;

int main(int argc, char *argv[]) {
	if (argc != 4) {
		cerr << "Welcome to SBFIRST" << endl;
		cerr << "Please provide the following THREE arguments:" << endl;
		cerr << " 1. a PDB input structure filename." << endl;
		cerr << " 2. a hydrogen bond energy cutoff in kcal/mol." << endl;
		cerr << " 3. cryophile flag: 1 for cryophilic, 0 for not" << endl;
		exit(0);
	}
	
	string pdbFilename;
	double eCut;
	int Cryo;
	
	stringstream fin;
	fin << argv[1];
	pdbFilename = fin.str();
	eCut = atof(argv[2]);
	Cryo = atoi(argv[3]);
	
	PDBThing foo;
	bool goodread = false;
	goodread = readPDBFile(foo, pdbFilename);
	if (!goodread) {
		cerr << "Oops, bad read on " << pdbFilename << endl;
		exit(1);
	}

	foo.setData();
	foo.clearCov();
	foo.initialiseGrid();
	foo.fillGrid();
	foo.getClosebyForCov();
	foo.conectToCov();
	foo.completeCov();

	stringstream fout;
	fout << pdbFilename << ".map";
	string oname = fout.str();
	bool goodwrite = false;
	goodwrite = foo.writeMap(oname);
	if (!goodwrite) {
		cerr << "Oops, bad write on " << oname << endl;
		exit(1);
	}

	stringstream fout2;
	fout2 << pdbFilename << ".cov";
	oname = fout2.str();
	goodwrite = false;
	goodwrite = foo.writeCov(oname);
	if (!goodwrite) {
		cerr << "Oops, bad write on " << oname << endl;
		exit(1);
	}

	foo.fillPolarH(); //set array
	foo.detectGuani();
	foo.detectCOO();
	foo.detectImi();
	foo.assignPolarityLabels();
	foo.assignPhobicRadii();
	foo.seekPhobicPairs(0.5); //margin +0.5 over radii
	foo.seekHbondsByPolarityLabels(2, eCut); //options: 1 = FIRST, energy 2 = safetyFIRST, 0 or none = native

	stringstream fout3;
	fout3 << pdbFilename << ".hb";
	oname = fout3.str();
	goodwrite = false;
	goodwrite = foo.writeHydrogenBonds(oname);
	if (!goodwrite) {
		cerr << "Oops, bad write on " << oname << endl;
		exit(1);
	}

	stringstream fout4;
	fout4 << pdbFilename << ".ph";
	oname = fout4.str();
	goodwrite = false;
	goodwrite = foo.writePhobicTethers(oname, Cryo);
	if (!goodwrite) {
		cerr << "Oops, bad write on " << oname << endl;
		exit(1);
	}
	
	exit(0);
}
