/* <wuinfo.c>  24feb05
**
** This is a simple program to print the progress recorded in
** the <work/wuinfo_xx.dat> files.
*/

#include <stdio.h>
#include <string.h>

#ifndef SYSTYPE			/* Really it should always be compiler arg */
# define SYSTYPE	0
#endif

#define WIFILE "work/wuinfo_??.dat"

/* Structure of <wuinfo_xx.dat> file */

typedef unsigned int u32;

struct wif
{	u32	core;		/* 000 Core number */
	char	pname[80];	/* 004 Project (protein) name */
	u32	tsteps;		/* 084 Total steps */
	u32	csteps;		/* 088 Completed steps */
	char	z092[420];	/* 092 */
};

char fbuf[300];
struct wif wibuf;
char *fdir;			/* Folding directory (default "") */

void cfn(char *, int);		/* Construct file name (in fbuf) */

int main()			/* wuinfo */
{
int i, j, k;
FILE *fp;

	fdir = "";
	j = 0;
	for (i = 0; i < 10; ++i)
	{	cfn(WIFILE, i);
		if ((fp = fopen(fbuf, "rb")) == NULL)
			continue;
		k = fread(&wibuf, 1, sizeof(wibuf), fp);
		fclose(fp);
		if (k < sizeof(struct wif))
		{	printf(" index %d: - can't read file \"%s\"\n", i, fbuf);
			continue;
		}
		++j;
		printf(" index %d:\n   Core: Core_%x\n   Name: %s\n   Progress: %u%% (%u of %u steps)\n", i,
				wibuf.core, wibuf.pname, (wibuf.csteps * 100 + wibuf.tsteps / 2) / wibuf.tsteps,
				wibuf.csteps, wibuf.tsteps);
	}
	if (j == 0)
		printf(" There is no work currently in progress in this directory.\n");
	return (0);
}

/* Construct file name (in fbuf) */

void cfn(char *fn, int n)
{
char *p;
int i;
char w[4];

	if	(	((i = strlen(fdir)) == 0)
		||	(strncmp(fn, "/", 1) == 0)
		||	(strncmp(fn, "./", 2) == 0)
		||	(strncmp(fn, "../", 3) == 0)
#if (SYSTYPE == 1)
		||	(strncmp(fn, "\\", 1) == 0)
		||	(strncmp(fn, ".\\", 2) == 0)
		||	(strncmp(fn, "..\\", 3) == 0)
		||	(fn[1] == ':')
#endif
		) sprintf(fbuf, "%.200s", fn);
	else if (fdir[i - 1] == '/')
		sprintf(fbuf, "%.200s%.90s", fdir, fn);
	else
		sprintf(fbuf, "%.200s/%.90s", fdir, fn);
	if ((p = strstr(fbuf, "??")) != NULL)
	{	sprintf(w, "%02d", n);
		*p++ = w[0];
		*p = w[1];
	}
#if (SYSTYPE == 1)
	for (p = fbuf; *p != '\0'; ++p)
		if (*p == '/') *p = '\\';
#endif
}
