/*************************************************************************
NIXMAIL
--------------------------------------------------------------------------
Copyright (c) Anthony Rumble 1992
--------------------------------------------------------------------------
RCS Info

$Header: /home/smilie/emsi/RCS/main.c,v 1.5 1992/12/06 09:32:09 smilie Exp $

$Log: main.c,v $
 * Revision 1.5  1992/12/06  09:32:09  smilie
 * run thru with Wall.Fixing warnings
 *
 * Revision 1.3  1992/06/22  06:39:08  smilie
 * forgot to disconnect when busy
 *
 * Revision 1.2  1992/06/22  06:15:40  smilie
 * added config
 *
 * Revision 1.1  1992/06/14  16:17:45  smilie
 * Initial revision
 *

*************************************************************************/

/* Feature test switches */
#define _POSIX_SOURCE 1
#define _MAIN_C

/* System Headers */
#include <stdio.h>
#include <string.h>
#include <unistd.h>

/* Local Headers */

#include "nixmail.h"

/* Macros */

/* File scope variables */

static char main_rcsid[] = "$Id: main.c,v 1.5 1992/12/06 09:32:09 smilie Exp $";
#define RCSID main_rcsid

/* External variables */

/* External Functions */

/* Structures and unions */

/* Functions */

/************************************************************************
				   MAIN
-------------------------------------------------------------------------
Command Line Arguments :-
nixmail [-p] zone:net/node.point
-p = Dial system, even if there is no mail waiting
-------------------------------------------------------------------------
Error Level Returns..
0 - Mail Sent OK		ERRET_OK
1 - FATAL ERROR			ERRET_FATAL
2 - No mail queued		ERRET_NOMAIL
3 - System is BUSY		ERRET_BUSY
4 - System is not answering	ERRET_NOANSWER
*************************************************************************/
void main(int argc, char *argv[])
{
int loop = 0;
/**/
CONFIG.poll = FALSE;

/* Check Arguments */
if ((argc>3) || (argc<2))
	{
	flog("Incorrect number of command line arguments\n");
	fprintf(stderr, "Usage: %s [-p] zone:net/node.point\n", argv[0]);
	exit(ERRET_FATAL);
	}

/* Load in Outbound node number */
sscanf(argv[argc-1], "%hu:%hu/%hu.%hu", &CONFIG.address.zone, &CONFIG.address.net, &CONFIG.address.node, &CONFIG.address.point);

if (argc == 3)
	{
	if (strcasecmp(argv[1], "-P") == 0)
		CONFIG.poll = TRUE;
	}

/* Load in General Config files */
load_config();

/* Load the outbound request list */
if (!load_outbound())
	{
	if (!CONFIG.poll)
		{
		flog("No outbound mail queued, exiting..\n");
		exit(ERRET_NOMAIL);
		}
	}
	
/* Open the Modem and Initialise */
open_modem();

/* Attempt to Dial the System CONFIG.maxdial times */
while (loop<CONFIG.maxdial)
	{
	loop++;
	sleep(1);				/* Slight Pause */
	if (dial_system())			/* Dial the System */
		{
		connect_emsi();			/* Connect EMSI and transfer */
		disconnect();			/* Hang up */
		#ifdef DEBUG
		flog("Exited correctly\n");
		#endif
		exit(ERRET_OK);
		}
	}
disconnect();					/* Hang Up */
#ifdef DEBUG
flog("Could not connect to system stoped redialing\n");
#endif
exit(ERRET_BUSY);
}



