#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/time.h>

#include "goo.h"

//written by l0om

int main(int argc, char **argv)
{
	int i, max, wait = 10, res = 20;
	FILE *fd;
	char *pxy = NULL, *req;
	goo_t goo;
	cli_ans ans;
	time_t thetime;

	if(argc > 1) {
		for(i = 1; i < argc; i++)
			if(argv[i][0] == '-')
				switch(argv[i][1]) {
					case 'm':
						res = atoi(argv[++i]);
						if(!res) {
							printf("%s illegal value\n",argv[i+1]);
							return 0;
						}
						break;
					case 'w':
						wait = atoi(argv[++i]);
						if(!wait) {
							printf("%s illegal value\n",argv[i+1]);
							return 0;
						}
						break;
					case 'p':
						pxy = argv[++i];
						break;
					default:
						printf(">> %s << unknown\n",argv[i]);
						return 0;
					}
			else req = argv[i];
	} 
	else {
		printf("%s <request> [options]\n",argv[0]);
		return 0;
	}

	goo = init_goo();
	if(goo.resultc != SUCCESS) {
		goo_prnt_er(&goo);
		return 0;
	}
	goo_cli(1,&goo);
	if(!goo_cli_cnt(&goo)) {
		goo_prnt_er(&goo);
		return 0;
	}
	goo_cli_set(1, GETURLS, NULL, &goo);
	if(pxy != NULL)
		goo_cli_set(1, SETPRXY, pxy, &goo);

	goo_cli_req(1, req, &goo, res);

	thetime = time(NULL);
	while(res > 0) {
		if(thetime < (time(NULL)-wait)) break;
		ans = goo_results(&goo);
		if(goo.resultc != NOANSWER)   {  // read answers for 10 seconds
			if(goo.resultc != SUCCESS) {
				goo_prnt_er(&goo);
				break;
			}
			for(i = 0; i < ans.urls; i++)
				printf("%s\n",ans.data[i]);
			res -= ans.urls;
			if(ans.urls < 10) break;
		}
		usleep(100000);
	}
	delete_goo(&goo);
	return 1;
}






