/* -*-C-*-
*******************************************************************************
*
* File:         nextmail.m
* RCS:          /usr/local/sources/CVS/mailapp-utilities/nextmail.m,v 1.3 1997/04/19 17:37:26 tom Exp
* Description:  A 'mail' compatible program to open a Mail.app Compose window
* Author:       Carl Edman
* Created:      Thu Mar  4 13:42:52 1993
* Modified:     Sat Nov 30 23:29:19 1996 Tom Hageman <tom@basil.icce.rug.nl>
* Language:     Objective C
* Package:      mailapp-utilities
* Status:       First release
*
* (C) Copyright 1993, but otherwise this file is perfect freeware.
*
*******************************************************************************
*/

#import <appkit/appkit.h>
#import <stdlib.h>
#import <stdio.h>
#import "MailSpeaker.h"
#import "mailutil.h"
#import "mailtoc.h"
#import "optutil.h"

#define USAGE "\
Usage: %s [-e] [-s subject] [-c user]... [-NXHost host] [-V|-H] [user...]\n"

#define HELP "\
 -e           edit message in Compose window (default is to deliver immediately)\n\
 -s subject   set Subject: to `subject'\n\
 -c user      cc: to `user' (allowed multiple times)\n\
 -NXHost host connect to Mail.app on given host\n\
 -V           show version\n\
 -H           this help\n\
"

void main(int ac,char *av[])
{
   id 		speaker;
   port_t 	mailPort;
   int window;
   char *to=0,*subject=0,*cc=0,*content=0;
   int tolen=0,subjectlen=0,cclen=0,contentlen=0;
   int tomaxlen=0,subjectmaxlen=0,ccmaxlen=0,contentmaxlen=0;
   int editflg=0,c,i;
   char *host=NULL;
   int status=EXIT_SUCCESS;

   set_progname(av[0]);

   while((c=getopt(ac,av,"s:c:eN:VH"))!=EOF) switch(c)
   {
    case 's':
      if (subject) appstring(&subject,&subjectlen,&subjectmaxlen," ",1);
      appstring(&subject,&subjectlen,&subjectmaxlen,optarg,strlen(optarg));
      break;
    case 'c':
      if (cc) appstring(&cc,&cclen,&ccmaxlen,",",1);
      appstring(&cc,&cclen,&ccmaxlen,optarg,strlen(optarg));
    case 'e':
      editflg++;
      break;
    case 'N':
      if ((strcmp(optarg,"XHost")==0||strcmp(optarg,"SHost")==0)&&(optind<ac))
      {
	 host=av[optind++];
	 break;
      }
      status=EXIT_USAGE;
      break;
    case 'V':
      status=EXIT_VERSION;
      break;
    case 'H':
      status=EXIT_HELP;
      break;
    case '?':
    default:
      status=EXIT_USAGE;
      break;
   }
   handle_usage_help_version(status, USAGE, HELP);

   for(;optind<ac;optind++)
   {
      if (to) appstring(&to,&tolen,&tomaxlen,",",1);
      appstring(&to,&tolen,&tomaxlen,av[optind],strlen(av[optind]));
   }

   while((i=fread(growstring(&content,&contentlen,&contentmaxlen,LINELEN*16),
                  1,16*LINELEN,stdin))>0)
      contentlen+=i;

   speaker = [[MailSpeaker alloc] init];
   mailPort = NXPortFromName("Mail", host);
   mailPort = NXPortFromName("MailSendDemo", host);

   if (mailPort == PORT_NULL) 
   {
      fprintf(stderr,"%s: unable to connect to Mail.app on %s\n", progname(), host ? host : "localhost");
      exit(EXIT_FAILURE);
   }
   
   [speaker setSendPort: mailPort];
   [speaker openSend:&window];
   if (to)
   {
      appstring(&to,&tolen,&tomaxlen,"\0",1);
      [speaker setTo:to inWindow:window];
   }
   if (subject)
   {
      appstring(&subject,&subjectlen,&subjectmaxlen,"\0",1);
      [speaker setSubject:subject inWindow:window];
   }
   if (cc)
   {
      appstring(&cc,&cclen,&ccmaxlen,"\0",1);
      [speaker setCc:cc inWindow:window];
   }
   if (content)
   {
      appstring(&content,&contentlen,&contentmaxlen,"\0",1);
      [speaker setBody:content inWindow:window];
   }
   if ((!editflg) && to) [speaker deliver:window];
   
   [speaker free];

   exit(EXIT_SUCCESS);
}
