/* Routinen geklaut/abgeändert aus ShowUMSFields von Chr. Rüttgers (?) */

void
ShowUMSField (UMSUserAccount account, UMSMsgNum MsgNum)
{
  LONG HeaderLength;		/* evtl. auch MessageInfo-Struktur aus ums.h ? */
  LONG TextLength;
  LONG Date;
  UMSMsgNum ChainUp;
  UMSMsgNum ChainDn;
  UMSMsgNum ChainLt;
  UMSMsgNum ChainRt;
  UMSSet GlobalStatus;
  UMSSet UserStatus;
  UMSSet LoginStatus;
  UMSMsgNum HardLink;
  UMSMsgNum SoftLink;
  LONG CDate;

  UMSMsgTextFields MsgArray;

  ULONG result = 0;
  int loop = 0, fi = 0;
  char buffer[256];

  if (MsgNum)
    {
      if (!UMSReadMsgTags (account,
			   UMSTAG_RMsgNum, MsgNum,
			   UMSTAG_RNoUpdate, 1,
			   UMSTAG_RTextFields, MsgArray,
			   UMSTAG_RHeaderLength, &HeaderLength,
			   UMSTAG_RTextLength, &TextLength,
			   UMSTAG_RMsgDate, &Date,
			   UMSTAG_RChainUp, &ChainUp,
			   UMSTAG_RChainDn, &ChainDn,
			   UMSTAG_RChainLt, &ChainLt,
			   UMSTAG_RChainRt, &ChainRt,
			   UMSTAG_RGlobalFlags, &GlobalStatus,
			   UMSTAG_RUserFlags, &UserStatus,
			   UMSTAG_RLoginFlags, &LoginStatus,
			   UMSTAG_RHardLink, &HardLink,
			   UMSTAG_RSoftLink, &SoftLink,
			   UMSTAG_RMsgCDate, &CDate,
			   UMSTAG_RReadAll, NULL,
			   TAG_DONE))
	printf ("Kann Msg Nr. :%lu nicht lesen, UMS-Error: %s\n", MsgNum, UMSErrTxt (account));
      else
	/* Message korrekt eingelesen, Buffer sind gültig: */
	{
	  printf ("Message %ld\n", MsgNum);

	  for (loop = 1; loop <= 40; loop++)
	    {
	      if (MsgArray[loop])
		printf ("%20.20s: %.60s\n", ums_field[loop], MsgArray[loop]);
	    }
	  if (MsgArray[127])
	    printf ("TempFileName:        %s\n", MsgArray[127]);

	  /* nun die numerischen Felder/Flags */
	  if (HeaderLength)
	    printf (" HeaderLength: %lu byte\n", HeaderLength);
	  if (TextLength)
	    printf ("   TextLength: %lu byte\n", TextLength);
	  if (Date)
	    printf ("         Date: %s\n", UMS2AmigaDate (Date, buffer));
	  if (CDate)
	    printf ("        CDate: %s\n", UMS2AmigaDate (CDate, buffer));
	  if (ChainUp)
	    printf ("      ChainUp: %lu\n", ChainUp);
	  if (ChainDn)
	    printf ("      ChainDn: %lu\n", ChainDn);
	  if (ChainLt)
	    printf ("      ChainLt: %lu\n", ChainLt);
	  if (ChainRt)
	    printf ("      ChainRt: %lu\n", ChainRt);
	  if (HardLink)
	    printf ("     HardLink: %lu\n", HardLink);
	  if (SoftLink)
	    printf ("     SoftLink: %lu\n", SoftLink);
	  if (GlobalStatus)
	    printf (" GlobalStatus: %s\n", ParseGlobalFlags (GlobalStatus, buffer));
	  if (UserStatus)
	    printf ("   UserStatus: %s\n", ParseUserFlags (UserStatus, buffer));
	  if (LoginStatus)
	    printf ("  LoginStatus: %s\n", ParseLocalFlags (LoginStatus, buffer));

	  /* Buffer freigeben */
	  UMSFreeMsg (account, MsgNum);
	}			/* else */
    }				/* if MsgNum */

}				/* main */

/* ------------------------------------------------------------------------ */

char *
UMS2AmigaDate (ULONG RecTime, char *buffer)
{
  struct ClockData *clockdata;

  strcpy (buffer, "-");
  if (clockdata = AllocMem (sizeof (struct ClockData), MEMF_CLEAR))
    {
      Amiga2Date (RecTime, clockdata);
      sprintf (buffer, "%02ld:%02ld %02ld.%02ld.%ld",
	       clockdata->hour, clockdata->min, clockdata->mday, clockdata->month, clockdata->year);
      FreeMem (clockdata, sizeof (struct ClockData));
    }
  return (buffer);
}

/* ------------------------------------------------------------------------ */

char *
ParseGlobalFlags (ULONG flags, char *buffer)
{
  strcpy (buffer, '\0');
  if (flags & UMSGSTATF_Deleted)
    strcat (buffer, "Deleted ");
  if (flags & UMSGSTATF_Expired)
    strcat (buffer, "Expired ");
  if (flags & UMSGSTATF_Exported)
    strcat (buffer, "Exported ");
  if (flags & UMSGSTATF_Orphan)
    strcat (buffer, "Orphan ");
  if (flags & UMSGSTATF_Link)
    strcat (buffer, "Link ");
  if (flags & UMSGSTATF_HardLink)
    strcat (buffer, "Hardlink ");
  if (flags & UMSGSTATF_Parked)
    strcat (buffer, "Parked ");
  if (flags & UMSGSTATF_HasFile)
    strcat (buffer, "HasFile ");
  return (buffer);
}

/* ------------------------------------------------------------------------ */

char *
ParseUserFlags (ULONG flags, char *buffer)
{
  strcpy (buffer, '\0');
  if (flags & UMSUSTATF_Archive)
    strcat (buffer, "Archive ");
  if (flags & UMSUSTATF_Junk)
    strcat (buffer, "Junk ");
  if (flags & UMSUSTATF_PostPoned)
    strcat (buffer, "PostPoned ");
  if (flags & UMSUSTATF_Selected)
    strcat (buffer, "Selected ");
  if (flags & UMSUSTATF_Filtered)
    strcat (buffer, "Filtered ");
  if (flags & UMSUSTATF_Old)
    strcat (buffer, "Old ");
  if (flags & UMSUSTATF_WriteAccess)
    strcat (buffer, "WriteAccess ");
  if (flags & UMSUSTATF_ReadAccess)
    strcat (buffer, "ReadAccess ");
  if (flags & UMSUSTATF_ViewAccess)
    strcat (buffer, "ViewAccess ");
  if (flags & UMSUSTATF_Owner)
    strcat (buffer, "Owner ");
  return (buffer);
}

/* ------------------------------------------------------------------------ */

char *
ParseLocalFlags (ULONG flags, char *buffer)
{
  int i = 0;
  char intbuf[30];

  strcpy (buffer, '\0');

  /* nur untere 16 Bits sind verfügbar */
  for (i = 0; i <= 15; i++)
    {
      if (flags & (1L << i))
	{
	  sprintf (intbuf, "Bit%ld ", i);
	  strcat (buffer, intbuf);
	}
    }
  return (buffer);
}
