#include <X11/Intrinsic.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

extern char *ProgramName;

#ifndef SEEK_END
#define SEEK_END 2
#endif

static FILE *fp;

void InitLoadPoint(void)
{
  fp = fopen("/var/local/www/logs/webload.pid", "w");
  fprintf(fp, "%lu\n", getpid());
  fclose(fp);
  if (!(fp = fopen("/var/local/www/logs/access_log","r")))
  {
    fprintf(stderr,
	    "%s: can't open /var/local/www/logs/access_log for reading\n",
	    ProgramName);
    exit(1);
  }
  fseek(fp, 0L, SEEK_END);
}

static int avg[8], iavg = -1, totavg;

void GetLoadPoint( w, closure, call_data )
     Widget	w;		/* unused */
     caddr_t	closure;	/* unused */
     caddr_t	call_data;	/* pointer to (double) return value */
{
  double *ret = (double *)call_data;
  int ch, lines = 0;

  clearerr(fp);
  while ((ch = getc(fp)) != EOF)
    if (ch == '\n')
      lines++;
  if (iavg == -1)
  {
    for (iavg = 0; iavg < 8; iavg++)
      avg[iavg] = lines;
    totavg = lines << 3;
    iavg = 0;
  }
  else
  {
    totavg -= avg[iavg];
    avg[iavg] = lines;
    iavg = (iavg+1) & 7;
    totavg += lines;
  }
  *ret = (double)totavg / 80.0;
}
