/* -*-ObjC-*-
*******************************************************************************
*
* File:         nextmail.m
* RCS:          /usr/local/sources/CVS/mailapp-utilities/nextmail.m,v 1.7 1998/02/14 17:34:47 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:     Fri Feb 13 20:05:47 1998 Tom Hageman <tom@basil.icce.rug.nl>
* Language:     Objective C
* Package:      mailapp-utilities
* Status:       Experimental
*
* (C) Copyright 1993-1998, but otherwise this file is perfect freeware.
*
*******************************************************************************
*/

#import <libc.h>
//#import <stdlib.h>
//#import <stdio.h>
#import "compat.h"
#import "MailProxy.h"
#import "mailutil.h"
#import "mailtoc.h"
#import "optutil.h"

#if NEXTSTEP
#  define NXHOST  "NXHost"
#else
#  define NXHOST  "NSHost"
#endif

#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\
 -m mailer    use `mailer' instead of default (\"dread -g Mailer\", or Mail.app)\n\
 -NXHost host connect to Mailer on given host\n\
 -V           show version\n\
 -H           this help\n\
"

#define EXIT(status) \
   POOL_RELEASE \
   return(status)

int main(int ac,char *av[])
{
   id 		mailProxy;
   char *to=0,*subject=0,*cc=0,*content=0;
   const char *mailer=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="";
   int status=EXIT_SUCCESS;
   POOL_INIT

   set_progname(av[0]);

   while((c=getopt(ac,av,"s:c:m: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 'm':
      mailer=optarg;
      break;
    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);

   if (editflg && ![MailProxy canEditMessage])
   {
      fprintf(stderr,"%s: [-e] editing not supported on this platform\n", progname());
      EXIT(EXIT_USAGE);
   }

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

   mailProxy = [[MailProxy alloc] initWithMailer:mailer host:host forceLaunch:YES];
   if (mailProxy == nil)
   {
      fprintf(stderr,"%s: unable to connect to %s.app on %s\n", progname(), mailer, host ? host : "localhost");
      EXIT(EXIT_FAILURE);
   }
   if (![mailProxy isConnected])
   {
      fprintf(stderr,"%s: unable to connect to %s.app on %s\n", progname(), [mailProxy mailer], [mailProxy hostname]);
      EXIT(EXIT_FAILURE);
   }

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

   // zero-terminate strings:
   if (to)      appstring(&to,&tolen,&tomaxlen,"",1);
   if (subject) appstring(&subject,&subjectlen,&subjectmaxlen,"",1);
   if (cc)      appstring(&cc,&cclen,&ccmaxlen,"",1);
   if (content) appstring(&content,&contentlen,&contentmaxlen,"",1);

   if (![mailProxy sendMailTo:to subject:subject body:content cc:cc deliver:!editflg])
   {
      fprintf(stderr, "%s: unable to send message to %s.app on %s\n", progname(), [mailProxy mailer], [mailProxy hostname]);
      EXIT(EXIT_FAILURE);
   }
   
   [mailProxy release];

   EXIT(EXIT_SUCCESS);
}
