/*
 * This is sample code generated by rpcgen.
 * These are only templates and you can use them
 * as a guideline for developing your own functions.
 */

#include "sumUp.h"


int
sumupprog_1(char *host, int von, int bis)
{
	CLIENT *clnt;
	int  *result_1;
	params  sumup_1_arg;

#ifndef	DEBUG
	clnt = clnt_create (host, SUMUPPROG, SUMVERS, "tcp");
	if (clnt == NULL) {
		clnt_pcreateerror (host);
		exit (1);
	}
#endif	/* DEBUG */

	sumup_1_arg.a = von;
	sumup_1_arg.b = bis;

	result_1 = sumup_1(&sumup_1_arg, clnt);


	if (result_1 == (int *) NULL) {
		clnt_perror (clnt, "call failed");
	}


	printf("Antwort von Client, Ergebnis ist %d\n", *result_1);

#ifndef	DEBUG
	clnt_destroy (clnt);
#endif	 /* DEBUG */

	return *result_1;
}


int
main (int argc, char *argv[])
{
	char *host;
	int bloecke = 0;
	int start = 0;
	int stop = 0;
	int gesamtergebnis = 0;

	//Ab dem 5. Argument kommen Hosts
	if (argc < 5) {
		printf ("usage: %s <Jobanzahl> <von> <bis> <host 1> <host 2> <host n>\n", argv[0]);
		exit (1);
	}

	if(atoi(argv[1]) > argc-4){
		printf("Fehler:\tEs darf nicht mehr Jobs als hosts geben\n");
		exit (2);
	}

	/* Debugkram
	printf("Hostliste:\n");
	for(int i=4; i<argc; i++){
		printf(" %s\n", argv[i]);
	}
	*/

	//Feststellen in wieviel Bloecke der sumUpJob zerteilt werden muss
	bloecke = atoi(argv[3]) / atoi(argv[1]);

//printf("\n%d\n", bloecke);
	start = atoi(argv[2]);
	stop = atoi(argv[3]) - (bloecke * (atoi(argv[1])-1));
	for(int i=0; i < atoi(argv[1]); i++){
		gesamtergebnis = gesamtergebnis + sumupprog_1 (argv[i+4], start, stop);
		start = stop + 1;
		stop = stop + bloecke;
	}

	printf("Das Gesamtergebnis ist: %d\n", gesamtergebnis);

	host = argv[1];
	//sumupprog_1 (host, atoi(argv[2]), atoi(argv[3]));
exit (0);
}