#include <stdio.h>
#include <strings.h>

#define TRUE (1)
#define FALSE (0)

extern char *getenv();

struct gstr *mesglist;

struct gstr
{
  char *group, *last;
  int unread;
  struct gstr *next;
};

/* GetGroupList checks for a legit .nmsgsrc file, opens it, and puts
 * it's contents into a linked list for later reference.
 */

void GetGroupList(start, end, names)
     int start, end;
     char **names;
{
  char *homedir, *name, *dump, *rcpath, *last = NULL;
  FILE *nmsgsrc;
  struct gstr *tptr, *otptr;
  int count, width, width2, nocommandliners;

  char *GetLine();
  void CheckError(), error();

  tptr = mesglist = NULL;

/* Check for .nmsgsrc. */

  homedir = getenv("HOME");
  rcpath = (char *)malloc(strlen(homedir)+strlen("/.nmsgsrc")+1);
  if (rcpath == NULL)
    CheckError("Nmsgs: GetGroupList: malloc");
  strcpy(rcpath, homedir);
  nmsgsrc = fopen(strcat(rcpath, "/.nmsgsrc"), "r");

  if (nmsgsrc != NULL)
    {
      dump = GetLine(nmsgsrc);

/* Small sanity check -- not perfect, but it's a start. */

      if (strcmp(dump, "Message_list") != 0)
	{
	  fclose(nmsgsrc);
	  printf("Your ~/.nmsgsrc is bad.  Removing it.\n");
	  if (unlink(rcpath) != 0)
	    error("Remove failed.  Exiting.");
	}

/* Run through the .nmsgsrc file, making an entry in mesglist for each
 * line found there.
 */

      nocommandliners = (start == end);
      while (TRUE)
	{
	  dump = GetLine(nmsgsrc);
	  if (*dump == '\0')
	    break;
	  else
	    {
	      if (mesglist == NULL)
		{
		  tptr = mesglist = (struct gstr *)malloc(sizeof(struct gstr));
		  if (tptr == NULL)
		    CheckError("Nmsgs: GetGroupList: malloc");
		}
	      else
		{
		  tptr->next = (struct gstr *)malloc(sizeof(struct gstr));
		  if (tptr->next == NULL)
		    CheckError("Nmsgs: GetGroupList: malloc");
		  tptr = tptr->next;
		}
	      tptr->next = NULL;

	      width = index(dump, ' ') - dump;
	      if (width <= 0)
		error("Malformed .nmsgsrc file -- exiting.");
	      width2 = strlen(dump) - width;
	      name = (char *)malloc(sizeof(char) * (width + 1));
	      last = (char *)malloc(sizeof(char) * width2);
	      if (name == NULL | last == NULL)
		CheckError("Nmsgs: GetGroupList: malloc");
	      strncpy(name, dump, width);
	      name[width + 1] = '\0';
	      strncpy(last, (dump + width + 1), width2);
	      last[width2] = '\0';
	      tptr->group = name;
	      tptr->last = last;
	      tptr->unread = nocommandliners;
	    }
	}
      fclose(nmsgsrc);
      free(rcpath);
    }

  if (nocommandliners)
    return;

  if (mesglist == NULL)
    {
      for (count = start; count < end; count++)
	{
	  if (*names[count] == '-')
	    continue;
	  if (mesglist == NULL)
	    {
	      tptr = mesglist = (struct gstr *)malloc(sizeof(struct gstr));
	      if (tptr == NULL)
		CheckError("Nmsgs: GetGroupList: malloc");
	    }
	  else
	    {
	      tptr->next = (struct gstr *)malloc(sizeof(struct gstr));
	      if (tptr->next == NULL)
		CheckError("Nmsgs: GetGroupList: malloc");
	      tptr = tptr->next;
	    }
	  tptr->group = names[count];
	  tptr->last = (char *)malloc(sizeof(char) * 16);
	  if (tptr->last == NULL)
	    CheckError("Nmsgs: GetGroupList: malloc");
	  strcpy(tptr->last, "-1");
	  tptr->unread = TRUE;
	  tptr->next = NULL;
	}
      return;
    }
  else
    {
      for (count = start; count < end; count++)
	{
	  if (*names[count] == '-')
	    continue;
	  tptr = mesglist;
	  while (tptr != NULL)
	    {
	      if (strcmp(tptr->group, names[count]) == 0)
		break;
	      otptr = tptr;
	      tptr = tptr->next;
	    }
	  if (tptr != NULL)
	    {
	      tptr->unread = TRUE;
	      continue;
	    }
	  tptr = otptr;
	  tptr->next = (struct gstr *)malloc(sizeof(struct gstr));
	  if (tptr->next == NULL)
	    CheckError("Nmsgs: GetGroupList: malloc");
	  tptr = tptr->next;

	  tptr->group = names[count];
	  tptr->last = (char *)malloc(sizeof(char) * 16);
	  if (tptr->last == NULL)
	    CheckError("Nmsgs: GetGroupList: malloc");
	  strcpy(tptr->last, "-1");
	  tptr->unread = TRUE;

	  tptr->next = NULL;
	}
    }
  return;
}
