/*
                  SSMTP -- (re)spool mail as SMTP mail

 	Genesis     : Tue Feb 18 15:22:12 1992
 	Last Change : Sun Feb 23 10:51:15 1992

	Developed and debugged with SAS/C Compiler V5.10a
	running under Kickstart 34.5, Workbench 34.20 and ARP 39.1
	on an european PAL-Amiga 2000 B Rev 4.3 with 3MB RAM.

	                        ___  _______
	Ralf S. Engelschall,    /__)(_  /_              rse@angle.muc.sub.org
	 Weblinger Weg 28,     / \____)(___           rse@cyvaned.muc.sub.org
	  8060 Dachau,        /
	   West Germany          ...!smurf.sub.org!brumuc!{angle,cyvaned}!rse
																			*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>

#include "dos_lib.h"
#include "lbio_lib.h"
#include "str_lib.h"

#include "sup.h"

#include "cus.h"
#include "defines.h"
#include "ssmtp.h"

#define SPOOLDIR_FROM "uuspool:"
#define SPOOLDIR_TO   "uumail:"
#define LSYSFILE      "uulib:l.sys"

static char *cpaFromFiles[RANGE_10BIT];
static char caFromFiles[RANGE_14BIT];
static char caFromPath[RANGE_8BIT];

static char caToPath[RANGE_8BIT];
static char caToFile[RANGE_8BIT];

int main(int argc, char **argv)
{
	char opt;
	char *optarg;
	char *optstr;
	int optidx;

	char *cpFromDir = SPOOLDIR_FROM;
	char *cpToDir = SPOOLDIR_TO;
	char caToDir[RANGE_8BIT];
	char *cpProgname = strlwr(argv[0]);
	char *cpSystem = NULL;
	char caTmp[RANGE_6BIT];
	int nFromFiles;
	int rc = DOSRC_SUCCESS;
	int i;
	int pcnt = 0;


	optidx = 1;
	optstr = "ftx";
	for ( ; (optarg = argopt(argc, argv, optstr, &optidx, &opt)) != NULL; ) {
		switch(opt) {
			case 'v':	giveversioninfo(cpProgname);
						CU(DOSRC_SUCCESS);
			case '?':	giveusage(cpProgname);
						CU(DOSRC_SUCCESS);
		}
	}
	for ( ; optidx < argc; optidx++) {
		if (cpSystem == NULL) {
			cpSystem = argv[optidx];
		}
		else {
			conmsg(cpProgname, "too much arguments specified");
			giveusage(cpProgname);
			CU(DOSRC_FAIL);
		}
	}

	if (cpSystem == NULL) {
		conmsg(cpProgname, "missing system name");
		giveusage(cpProgname);
		CU(DOSRC_FAIL);
	}
	cpSystem = strlwr(cpSystem);
	if (!inlsys(LSYSFILE, cpSystem)) {
		conmsg(cpProgname, "sorry, system %s not in l.sys file %s", cpSystem, LSYSFILE);
		CU(DOSRC_FAIL);
	}


	if (!exists(cpFromDir)) {
		conmsg(cpProgname, "cannot found source (uucp spool) dir %s", cpFromDir);
		CU(DOSRC_FAIL);
	}
	sprintf(caToDir, "%ssmtp.queue/%s/", cpToDir, cpSystem);
	if (!exists(caToDir)) {
		conmsg(cpProgname, "cannot found target (smtp) dir %s", caToDir);
		CU(DOSRC_FAIL);
	}
	if ((nFromFiles = getfnlst(cpFromDir, caFromFiles, sizeof(caFromFiles), 1)) == -1) {
		conmsg(cpProgname, "cannot read filenames of source (uucp spool) dir %s", cpFromDir);
		CU(DOSRC_FAIL);
	}
	nFromFiles = strbplst(cpaFromFiles, sizeof(cpaFromFiles), caFromFiles);

	for (i = 1; i < nFromFiles; i++) {
		if (cpaFromFiles[i][0] == 'C' && cpaFromFiles[i][1] == '.') {
			strcpy(caTmp, cpSystem);
			if (strlen(caTmp) > 7)
				caTmp[7] = NUL;
			if (strnicmp(caTmp, &cpaFromFiles[i][2], strlen(caTmp)) == 0) {
				if (processfile(caToDir, cpFromDir, cpaFromFiles[i])) {
					printf("processed\n");
					++pcnt;
				}
				else {
					printf("not processed\n");
				}
			}
		}
	}

	printf("\nProcessed %d spooled mails\n\n", pcnt);

	CUS:
	return rc;
}

