#include <stdio.h>
#include <dbm.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

main()
{
  char line[2048], *key, *data, nicer[2048];
  FILE *f;
  int d, nptr;
  datum ky, dt;

  d = open("auth.dir", O_CREAT | O_WRONLY | O_TRUNC, 0644);
  if (d < 0)
    {
      fprintf(stderr, "Couldn't touch auth.dir (%d).\n", errno);
      exit(1);
    }
  close(d);

  d = open("auth.pag", O_CREAT | O_WRONLY | O_TRUNC, 0644);
  if (d < 0)
    {
      fprintf(stderr, "Couldn't touch auth.pag (%d).\n", errno);
      exit(1);
    }
  close(d);

  f = fopen("auth.txt", "r");
  if (f == NULL)
    {
      fprintf(stderr, "Couldn't open auth.txt.\n");
      exit(1);
    }

  dbminit("auth");

  while (!feof(f))
    {
      if (fgets(line, sizeof(line), f))
	{
	  key = line;
	  while (isspace(*key) && (*key != '\0')) key++;
	  data = key;
	  while ((*data != ':') && (*data != '\0')) data++;
	  if (*data == '\0')
	    continue;
	  *data = '\0';
	  data++;
	  nptr = 0;
	  while (1)
	    {
	      while (isspace(*data)) data++;
	      if (*data == '\0')
		break;
	      while (!isspace(*data) && *data != '\0')
		nicer[nptr++] = *data++;
	      nicer[nptr++] = '\0';
	    }
	  if (nptr == 0)
	    continue;
	  ky.dptr = key;
	  ky.dsize = strlen(key) + 1;
	  dt.dptr = nicer;
	  dt.dsize = nptr;
	  if (0 > store(ky, dt))
	    fprintf(stderr, "error storing %s\n", key);
	}
    }

  fclose(f);
}
