#include "test.h"

int main(int argc, char *argv[])
{	
	MemoryManager memory;
	FILE* input;
	int *Result, *Filled;
	int Constraints, Transitions, i;
	
	char IDesc[] = "matrix D, matrix Dm, matrix Dp, indexarray Tuc, indexarray Tuo, matrix L, matrix F, matrix C, arrayi b, arrayi m0";
	
	matrix D, Dm, Dp, L, F, C;
	int *b, *m0, *Tuc, *Tuo;
	int bCount, m0Count, TucCount, TuoCount;
	
	/*Get a pointer to the input file.*/
	if (! (input = ParseCmdLine(argc,argv)))
	{
		return 0;
	}
	
	memory = CreateMemoryManager(5, 6, 0, 0);
	
	while (ParseStructure(input, IDesc, &Filled, &memory, &D, &Dm, &Dp, &Tuc, &TucCount, &Tuo, &TuoCount, &L, &F, &C, &b, &bCount, &m0, &m0Count))
	{	/*Find out how many constraints and transitions have been defined.*/
		Constraints = Filled[5] ? NumberOfRows(L) : (Filled[6] ? NumberOfRows(F) : (Filled[7] ? NumberOfRows(C) : 0));		
		Transitions = Filled[0] ? NumberOfColumns(D) : (Filled[1] ? NumberOfColumns(Dm) : (Filled[2] ? NumberOfColumns(Dp) : 0));
		
		/*Display the problem.*/		
		DisplayStructure(IDesc, Filled, &D, &Dm, &Dp, Tuc, TucCount, Transitions, Tuo, TuoCount, Transitions, &L, &F, &C, b, bCount, m0, m0Count);
	
		/*If an incidence matrix was supplied fill in the i/o matrices.*/
		FillDmDp(Filled, &D, &Dm, &Dp, &memory);

		/*Solve the problem.*/
		Result = isadm(Filled[1] ? &Dm : 0, Filled[2] ? &Dp : 0, Filled[5] ? &L : 0, Filled[8] ? b : 0, Filled[9] ? m0 : 0, Filled[6] ? &F : 0, Filled[7] ? &C : 0, Filled[3] ? TucCount : 0, Tuc, Filled[4] ? TuoCount : 0, Tuo);
		
		/*Display the solution.*/
		if (Result)
		{
			printf("ILPADM Result:\n");
			for (i = 0; i < Constraints; i++)
			{
				printf("%d\t", Result[i]);
			}
			ManageMemory(&memory, Result);
			printf("\n");
		}
		else
		{
			merror(0, "TEST-ILPADM: ilpadm call returned null!");
			FreeMemory(&memory);
			return 1;
		}
		printf("---------------------------------------------------------------\n");
		FreeMemory(&memory);
		memory = CreateMemoryManager(5, 6, 0, 0);
	}
	FreeMemory(&memory);
	return 0;
}
