#include <ncbi.h>
#include <gishlib.h>
#include "blastapp.h"

#define PERIOD	60

static AlarmBlkPtr	abp;
static int	ticks;

static void
tickproc(userp)
	Nlm_VoidPtr	userp;
{
	static int	cnt;

	if (cnt > 0)
		return;
	++cnt;

	AlarmReset(abp, PERIOD);
	if (userp != NULL) {
		FILE	*fp = (FILE *)userp;

		++ticks;
		putc('.', fp);
		if (ticks >= 60) {
			ticks = 0;
			putc('\n', fp);
			putc(' ', fp);
			putc(' ', fp);
			putc(' ', fp);
			putc(' ', fp);
		}
		fflush(fp);
	}
	--cnt;
}

static void
waitproc(userp)
	Nlm_VoidPtr	userp;
{
	static int	cnt;

	if (cnt > 0)
		return;
	++cnt;

	if (userp != NULL) {
		FILE	*fp = (FILE *)userp;

		++ticks;
		putc('*', fp);
		if (ticks >= 60) {
			ticks = 0;
			putc('\n', fp);
			putc(' ', fp);
			putc(' ', fp);
			putc(' ', fp);
			putc(' ', fp);
		}
		fflush(fp);
	}
	--cnt;
}


void
RunWild(s, n, func)
	char	*s; /* Human readable name for the task to perform */
	unsigned long	n; /* Number of discrete subtasks */
	void	(*func) PROTO((void));
{
	printf("%s", s);
	fflush(stdout);

	ticks = 0;

	abp = AlarmEvery(PERIOD, (FnPtr)waitproc, (VoidPtr)stdout);

#ifdef MPROC_AVAIL
#define run_wild mrun_wild
#endif
	numprocs = run_wild(n, func, stdout, tickproc, NTICKS, numprocs, &nprocs, SigTerm);
	if (numprocs == 0 && n > 0)
		bfatal(ERR_MPFORK, "Could not fork for multiprocessing; Is your OS current?");
	AlarmClr(abp);
	printf("done\n");
	fflush(stdout);
}
