#include <sys/types.h>
//#include <unistd.h>  // needed to be changed
#include <LINK/userobj/PostAnalyze.h>

int
find_distribution (int num_verts, double prob, int num_graphs, char* type, long rand_seed)
{

int num, j, count = 1, prev_num_graphs = 0;
char* path;
Graph* g;
Sequence<Vertex*> vector;
Vertex* vertex;
int kl_placement, grdy_placement;

	Link_setSeed(rand_seed);
	int* vertex_ave = new int[num_verts];


	fstream file;

	char* dir_place =  getenv("NGDIST_ROOT");
	path = form_path(dir_place, type);
	char* file_name = format(path, num_verts, prob);

	initialize_file(file_name, file, num_verts, prev_num_graphs, vertex_ave);
	fstream log_file;

	log(file_name, num_verts, prob, num_graphs, type, rand_seed);


	for (int i=0; i < num_graphs; i++)
	{
		j = 0;

		g = GenRandomGraph(num_verts, prob);
		KLFM(g);
		vector = PostAnalyze(g);
		
		Iterator<Vertex*> vector_pass(&vector);
		
		while(vector_pass(vertex))
		{
			j++;
			getAttribute((Graph*) vertex, "greediness", grdy_placement);
			getAttribute((Graph*) vertex, "partition", kl_placement);
			if (grdy_placement)
				vertex_ave[j] = vertex_ave[j] + 1;
		}
		if ( count == 10)
		{
			num = prev_num_graphs + i + 1;
			file.open(file_name, ios::out);
			write_data(num_verts, num, vertex_ave, file);
			file.close();
			count = 0;
		}
		count++;
		delete g;
	}
	
	
	file.open(file_name, ios::out);
	num = num_graphs + prev_num_graphs;
	write_data(num_verts, num, vertex_ave, file);
	file.close();
	delete file_name;
	delete vertex_ave;
	return prev_num_graphs;
}


char*
form_path(char* dir_place, char* path)
{
	char* buf;

	ostrstream iss;
	iss << dir_place << "/" << path;
	iss << ends;
	buf = iss.str();

	return buf;
}

char*
format(char* path, int num_verts, double prob)
{
	char* buf;
	char* s;
	ostrstream uss;

	uss << prob; 
	uss << ends;
	s = uss.str();

	ostrstream oss;

	oss << path << "/" << num_verts; 
	oss << s + 1;
	oss << ends;

	buf = oss.str();
	delete s;
	return buf;
}


void
log(char* out_name, int num_verts, double prob, int num_graphs,
     char* type, int seed)
{
	char* buf;
	int graphs_done = 0;
	ostrstream lss;
	fstream log_file;
	fstream file;

	file.open(out_name, ios::in);
	file >> graphs_done;
	file.close();

	char* path = getenv("NGDIST_LOG");
	lss << path << "/" << "DistributionLog";
	lss << ends;
	buf = lss.str();


	log_file.open(buf, ios::app);
	log_file.seekg(ios::end);
	log_file << "FindDistribution " << num_verts << " " << prob;
	log_file << " " << num_graphs << " " << type << " " << seed;
        //log_file << "  pid: " << getpid() <<  " graphs completed: ";  // changed 
	log_file << graphs_done << endl;
	log_file.close();
	delete buf;
}

void
initialize_file(const char* file_name, fstream& file, int num_verts,
		int& num_graphs, int* vertex_ave)
{
	int temp, i;
	file.open(file_name, ios::in);
	file.seekg(ios::beg);
	if (file.fail())		// needed to be changed
	{
		file >> temp;
		num_graphs = num_graphs+temp;
		for(i = 0; i < num_verts; i++)
		{
			file >> temp;
			file >> vertex_ave[i];
			//cout << vertex_ave[i] << endl;
		}
	} else {
		for (i=0; i < num_verts; i++)
			vertex_ave[i] = 0;
	}
	file.close();
}




void
write_data(int num_verts, int num_graphs, int* vertices_ave, fstream& file)
{
	file.seekg(ios::beg);
	int i;
	file << num_graphs << endl;
	for (i = 0; i < num_verts; i++)
	{
		file << i << ' ' << vertices_ave[i] << endl;
	}
} 



/*
void main (int argc, char* argv[])
{
//cout << "Enter number of graphs: ";
//int num_graphs;
//cin >> num_graphs;
//cout << "Enter verts: ";
//int verts;
//cin >> verts;
//cout << "Enter prob: ";
//double prob;
//cin >> prob;
//cout << "Enter name: ";
//char* type = new char[6];
//cin >> type;

int verts = atoi(argv[1]);
double prob = atof(argv[2]);
int num_graphs = atoi(argv[3]);
long seed = atoi(argv[5]);
find_distribution(verts, prob, num_graphs, argv[4], seed);
}

*/
