/*****************************************************************************

			      PilotForth

      Copyright (C) 1997, Ivan A. Curtis.  All rights reserved.

			       based on
C-FORTH: a portable, C-coded figFORTH interpreter by Allan Pratt April 1985.

This program may not be redistibuted in part or in whole without this Copyright
Message being included at the top of each file.

   This is free software, licensed under the GNU Public License V2.
		  See the file COPYING for details.

*******************************************************************************
*******************************************************************************

Filename:	Forth/bootstrap/buildpdb.c

Description:	Build a pdb file from stdin

Update History:   (most recent first)
   I. Curtis  15-May-97 20:59 -- v0.1 Released.
   I. Curtis  30-Apr-97 21:14 -- Created.

******************************************************************************/
#include <stdio.h>

void dump_int32(FILE *fout, unsigned int data)
{
  fputc((data >> 24) & 0xff, fout);
  fputc((data >> 16) & 0xff, fout);
  fputc((data >> 8) & 0xff, fout);
  fputc(data & 0xff, fout);
}

void dump_int16(FILE *fout, unsigned int data)
{
  fputc((data >> 8) & 0xff, fout);
  fputc(data & 0xff, fout);
}

void dump_header(FILE *fout, char *name, char *creator, char *type,
	    int nrecs, int recsize)
{
  int offset, recno;
  int i;
				/* Database name */
  for (i = 0; name[i] && i < 31; i++) {
    fputc(name[i], fout);
  }
  for (; i < 32; i++) {
    fputc(0, fout);
  }
  dump_int32(fout, 0);		/* don't know */
  dump_int32(fout, 0x06d144ae);	/* Time1 */
  dump_int32(fout, 0x06d144ae);	/* Time2 */
  dump_int32(fout, 0x0);	/* Time3 */
  dump_int32(fout, 0x0);	/* LastSync */
  dump_int32(fout, 0x0);	/* Sort */
  dump_int32(fout, 0x0);	/* Categories */
  fputs(type, fout);		/* Type */
  fputs(creator, fout);		/* Creator */
  dump_int32(fout, 0);		/* don't know */
  dump_int32(fout, 0);		/* don't know */
  dump_int16(fout, nrecs);	/* NumRecs */
  /* offset information */
  if (nrecs % 2 == 1) {
    nrecs ++;
  }
  offset = 0x50 + 8 * nrecs;
  recno = 0x0001ac00;
  while (nrecs > 0) {
    dump_int32(fout, offset);
    dump_int32(fout, recno);
    offset += recsize;
    recno++;
    nrecs--;
  }
  dump_int16(fout, 0);		/* padding */
}

int main(int argc, char **argv)
{
  int a, ccount;
  int nrecs;
  int size = 0;
  char *dbname = "ForthDB";
  char *creator = "i4th";
  char *type = "Data";
  int recsize = 2048;
  int swapsize = 2;

  for (a = 1; a < argc; a++) {
    if (strcmp(argv[a], "-n") == 0) { /* database name */
      a++;
      dbname = argv[a];
    } else if (strcmp(argv[a], "-c") == 0) { /* creator name */
      a++;
      creator = argv[a];
    } else if (strcmp(argv[a], "-t") == 0) { /* type name */
      a++;
      type = argv[a];
    } else if (strcmp(argv[a], "-s") == 0) { /* file size */
      a++;
      if (sscanf(argv[a], "%d", &size) != 1) {
	fprintf(stderr, "E - expected int for file size\n");
	exit(1);
      }
    } else if (strcmp(argv[a], "-w") == 0) { /* swap size */
      a++;
      if (sscanf(argv[a], "%d", &swapsize) != 1) {
	fprintf(stderr, "E - expected int for swap size\n");
	exit(1);
      }
    } else if (strcmp(argv[a], "-r") == 0) { /* record size */
      a++;
      if (sscanf(argv[a], "%d", &recsize) != 1) {
	fprintf(stderr, "E - expected int for record size\n");
	exit(1);
      }
    } else {
      fprintf(stderr, "E - bad argument \"%s\"\n", argv[a]);
      exit(1);
    }
  }

  nrecs = size / recsize;
  if (size % recsize != 0) {
    nrecs ++;
  }

  fprintf(stderr, "Database is %d bytes (%d records)\n", size, nrecs);
  dump_header(stdout, dbname, creator, type, nrecs, recsize);

  ccount = 0;
  do {
    char buff[4];
    int i, c;
    for (i = 0; i < swapsize; i++) {
      if ((c = fgetc(stdin)) == EOF) {
	fprintf(stderr, "E - EOF seen\n");
	exit(1);
      }
      buff[i] = c;
    }
    for (i--; i >= 0; i--) {
      fputc(buff[i], stdout);
    }
    ccount += swapsize;
  }
  while (ccount < size);
  
  for(; ccount < nrecs * recsize; ccount++) {
    fputc(0, stdout);
  }
  exit(0);
}
