/* <wuinfo.c>  24feb05
**
** This is a simple program to print the progress recorded in
** the <work/wuinfo_xx.dat> files.
**
** Copyright (C) 2002-2005 Richard P. Howell IV.
** Copyright (C) 2005-2008 Sebastiaan Couwenberg
** This is free software; you can distribute it and/or modify it under
** the terms of the GNU General Public License.  There is no warranty
** whatsoever.
**
** To compile (for example):
**
**  cc -DSYSTYPE=0 -o wuinfo wuinfo.c
**
** Define SYSTYPE as needed for system being compiled for...
**  0 - Linux                  (x86 CPU)
**  1 - Windows NT, 2K, ME, XP (x86 CPU)
**  2 - Mac OS X               (PPC CPU)
**  3 - Mac OS X               (x86 CPU)
**
** The program accepts the following flag arguments...
**
**  -u      Print this usage message (and exit)
**  -f dir  Explicitly specify folding directory
*/

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

#ifndef SYSTYPE			/* Really it should always be compiler arg */
# if defined (__i386__) && ((defined(__linux__) || defined(__unix__)) && !defined(__APPLE__))
#  define SYSTYPE	0
# elif defined (__i386__) && (defined(__WIN32__) || defined(WIN32))
#  define SYSTYPE	1
# elif defined(__ppc__) && defined(__APPLE__)
#  define SYSTYPE	2
# elif defined(__i386__) && defined(__APPLE__)
#  define SYSTYPE	3
# else
#  define SYSTYPE	-1
# endif
#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(int argc, char *argv[])	/* wuinfo */
{
char *argv0;
char *s1;
char *q;
int i, j, k;
u32 percent;
FILE *fp;

	argv0 = argv[0];
	fdir = "";

	/* Parse arguments */
        while (--argc > 0)
        {       ++argv;
                if (*argv[0] == '-')
                {       s1 = *argv;
                        while (*++s1 != '\0')
                                switch (*s1)
                                {
                                default:
us:                                     fprintf(stderr, "\n\
Usage: %.200s [<flags> <arguments required by flags>]*\n", argv0);
                                        fprintf(stderr,"\
 -u      Print this usage message (and exit)\n\
 -f dir  Explicitly specify folding directory\n");
                                        exit(1);

                                case 'f':               /* Explicitly specify folding directory */
                                        if (--argc <= 0)
                                        {       q = "Missing folding directory name argument";
                                                fprintf(stderr,"\n%.200s: %.200s\n", argv0, q);
                                                goto us;
                                        }
                                        fdir = *++argv;
                                        break;
				}
			continue;
		}
		else goto us;
	}


	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;

		percent = 0;
		if(wibuf.csteps > 0 && wibuf.tsteps > 0)
		{
			percent = (wibuf.csteps * 100) / wibuf.tsteps;
		}

		printf(" index %d:\n   Core: Core_%x\n   Name: %s\n   Progress: %u%% (%u of %u steps)\n", 
				i, wibuf.core, wibuf.pname, percent, 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
}

