/* ??? Uses non-ANSI mktemp(), unlink(), vfork(), wait() */

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>

#include "sysdep.h"
#include "bool.h"
#include "MTreg.h"
#include "MsgType.h"
#include "darray.h"
#include "FieldCore.h"

#ifdef __STDC__
extern const char *mktemp(char *);
#else
extern const char *mktemp();
#endif

/* MT_OPTION should be long enough that it does not conflict with any existing
 * MH options.  */

#define MT_OPTION "-mt"

#define COMP_COMMAND "comp"

static char formfilename[]="/tmp/alcomp.MHformXXXXXX";
static char *progname;		/* Contains a copy of argv[0] */

static NORET fail(msg)
     const char *msg;
{
  fprintf(stderr, "%s: %s\n", progname, msg);
  exit(1);
}

int main(argc, argv)
     int argc;
     char **argv;
{
	char	**p1 = argv,
		**p2, **p3;
	char *MT_name;
	Bool MT_found = Bool_FALSE;

	progname = argv[0];
	p3  = p2 = (char **)Memory_allocate((argc + 3) * sizeof(char **));
	while (*p1 != (char *)NULL) 
	{
/************
	No longer have the MT_OPTION switch
	***********************************

		if (strncmp(*p1, MT_OPTION, sizeof(MT_OPTION)-1) == 0)
		{	++p1;
			if (*p1 == (char *)NULL)
				fail("Message type name missing");
			MT_name = *p1++;
			MT_found = Bool_TRUE;
		}
		else 
************/
		if(!strcmp(*p1, "-help") || !strcmp(*p1, "-h"))
		{	printf("Usage: %s [message-type]\n", progname);
			exit(0);
		}
		else if(**p1 != '-' && p1 != argv)
		{	MT_name = *p1++;
			MT_found = Bool_TRUE;
		}
		else if (MT_found != Bool_FALSE) 
			*p2++ = *p1++;
		else
		/***	p1++, p2++;	***/
			*p2++ = *p1++;	/*since p1&p2 don't start the same now THM*/
	}
	*p2 = (char *)NULL;
  /* Now process the message type (if specified) into an MH form */
 
  if (MT_found != Bool_FALSE) {
    FILE *formfile;
    AlMsgType mtype;
    Darray mtype_fields;
    unsigned int i, num_fields;

    Al_init();

    if ((formfile = fopen(mktemp(formfilename), "w")) == (FILE *)NULL)
      fail("Unable to create temporary form file");

    if ((mtype = AlMTreg_get_obj_with_name(MT_name)) == (AlMsgType)NULL) {
      (NORET)unlink(formfilename);
      fail("Unknown message type");
    }

    mtype_fields = Darray_create();
    AlMsgType_all_fields(mtype, mtype_fields);
    num_fields = Darray_len(mtype_fields);

    for (i = 0; i < num_fields; ++i) {
      (NORET)fprintf(formfile, "%s\n", 
	      AlFieldCore_get_key((AlFieldCore)Darray_get(mtype_fields, i)));
    }
    (NORET)fprintf(formfile, "Msg-Type: %s\n", MT_name);
    (NORET)fprintf(formfile, "--------\n");
    Darray_destroy(mtype_fields);

    if (fclose(formfile) == EOF) {
      fail("Unable to close temporary form file.");
    }
    *p2++ = "-form";		/* HACK:  assumes that space is available
				 * in the argv array */
    *p2++ = formfilename;
    *p2 = (char *)NULL;
  }

  /* execute the inc command in a subprocess */

  {
    int child_pid;

    if ((child_pid = vfork()) == 0)
      execvp(COMP_COMMAND, p3);
    if (child_pid != wait(0))
      fail("Unexpected child PID");
  }

  /* Try to clean up the temporary form file */
  if (MT_found != Bool_FALSE) {
    (NORET)unlink(formfilename);
    Al_cleanup();
  }

  return 0;
}
