/* 
 *   Copyright (c) 1990 Rodney Volz
 *   ALL RIGHTS RESERVED 
 * 
 *   THIS SOFTWARE PRODUCT CONTAINS THE
 *   UNPUBLISHED SOURCE CODE OF RODNEY VOLZ
 *
 *   The copyright notices above do not evidence
 *   intended publication of such source code.
 *
 */

/*
 * MERCURY UUCP SOURCECODE [uupoll.c]
 */

#define _UUPOLL

#ifndef MWC
#include "uulib.c"

#include "v24.c"

overlay "main"
#else
#include "define.h"
#endif

main(argc,argv)	/* UUCP (Pollen) */
int argc;
char *argv[];
{
	static char sys_drive[LLEN],sys_mqueue[LLEN],sys_hosts[LLEN];
	static char sys_myname[LLEN],hst[LLEN],system[LLEN],baud[LLEN];
	static char s[LLEN],s2[LLEN],s3[LLEN],give[LLEN];
	static char phone[LLEN],nametrig[LLEN],logname[LLEN];
	static char pwtrig[LLEN],pw[LLEN],hostdir[LLEN];
	static char modeminit[LLEN],uucico[LLEN],tim[LLEN];
	static char logdir[LLEN];
	static char tmpdir[LLEN],*cp;
	int anz,inh,outh,n,i,c,retry,code;

	long xerr,xout,stime,sratio,xin,rtime,rratio;
	
	getconf(21,tmpdir);
	sys_impdirn(tmpdir);
	getconf(1,sys_drive);
	getconf(15,sys_hosts);
	getconf(2,sys_myname);
	getconf(5,logdir);
	getconf(14,sys_mqueue);
	getconf(7,uucico);
	sys_impdirn(sys_mqueue);
	getconf(10,s);
	retry = val(s);

	/* Use the right discdrive */
	gemdos(0x0e,(int)(toupper(sys_drive[0]) - 'A'));

	strcpy((char *)argv[argc - 1],system);
	strcpy(system,hst);

	send("UUPOLL - ");
	send(VERSION);
	cr(1);
	
	if (system[0] == '\0')
	{
		send("Usage: uupoll <systemname>");
		cr(1);
		return(-1);
	}

	strcpy(sys_drive,s);
	sys_impdirn(s);
	sys_chdir(s);
	
	sys_chdir(sys_drive);
	strcpy(sys_drive,give);
	
	stradd("\\uucico.par",give);
	
	strcpy(sys_hosts,s3);
	
	inh = open(s3,2);	/* Open Host-DB readonly */
	if (inh <= 0)
	{
		beep();
		send("Couldn't find ");
		send(s3);
		cr(1);
		return(-1);
	}
	
	c = 0;
	n = -1;
	while (0 == 0)
	{
		c = uu_getline(inh,s);
		
		if (c == -1)
			break;
		
		strcpys(s,s2,0,linstr(s,'|') - 1);
		if (strcmp(s2,system) == TRUE)
		{
			n = 0;
			break;
		}
	}
	close(inh);
	
	if (n == -1)
	{
		beep();
		send("No such host in host database.");
		cr(1);
		return(-1);
	}

	if (retry == 0)
	{
		send("Retry was set to zero - using default.");
		cr(1);
		retry = 1;
	}
	
	strcpys(s,hst,0,linstr(s,'|') - 1);	/* Pos. 0. bis 1. '|' nach s2 */
	
	strcpys(s,s2,linstr(s,'|') + 1,strlen(s));	/* Cut to next ':' */
	strcpy(s2,s);
	
	strcpys(s,modeminit,0,linstr(s,'|') - 1);
	
	strcpys(s,s2,linstr(s,'|') + 1,strlen(s));	/* Cut to next ':' */
	strcpy(s2,s);

	strcpys(s,tim,0,linstr(s,'|') - 1);
	
	strcpys(s,s2,linstr(s,'|') + 1,strlen(s));	/* Cut to next ':' */
	strcpy(s2,s);

	strcpys(s,baud,0,linstr(s,'|') - 1);
	
	strcpys(s,s2,linstr(s,'|') + 1,strlen(s));	/* Cut to next ':' */
	strcpy(s2,s);

	strcpys(s,phone,0,linstr(s,'|') - 1);
	
	strcpys(s,s2,linstr(s,'|') + 1,strlen(s));	/* Cut to next ':' */
	strcpy(s2,s);

	strcpys(s,nametrig,0,linstr(s,'|') - 1);
	
	strcpys(s,s2,linstr(s,'|') + 1,strlen(s));	/* Cut to next ':' */
	strcpy(s2,s);
	
	strcpys(s,logname,0,linstr(s,'|') - 1);
	
	strcpys(s,s2,linstr(s,'|') + 1,strlen(s));	/* Cut to next ':' */
	strcpy(s2,s);

	strcpys(s,pwtrig,0,linstr(s,'|') - 1);
	
	strcpys(s,s2,linstr(s,'|') + 1,strlen(s));	/* Cut to next ':' */
	strcpy(s2,s);

	strcpys(s,pw,0,linstr(s,'|') - 1);
	
	strcpys(s,s2,linstr(s,'|') + 1,strlen(s));	/* Cut to next ':' */
	strcpy(s2,s);

	strcpys(s,hostdir,0,strlen(s));
	sys_impdirn(hostdir);
	
	
	strcpy(give,s2);
	outh = open(s2,1);	/* Create parameter-file for UUCICO */
	if (outh <= 0)
	{
		beep();
		send("Error sys_fopen(??,1)");
		cr(1);
		return(-1);
	}

	putline(outh,sys_myname);
	putline(outh,hst);
	putline(outh,modeminit);
	putline(outh,baud);
	putline(outh,phone);
	putline(outh,nametrig);
	putline(outh,logname);
	putline(outh,pwtrig);
	putline(outh,pw);
	putline(outh,hostdir);
	putline(outh,sys_mqueue);
	putline(outh,tim);
	putline(outh,logdir);
	str(retry,s);
	putline(outh,s);
	putline(outh,"0");	/* Debugging level */
	putline(outh,"***** OVERHEAD *****");	
	close(outh);

	if (stty(val(baud)) == -1)
	{
		beep();
		send("Invalid baudrate entry in host file.");
		cr(1);
		return(-1);
	}


	strcpy(uucico,s); 
	
	strcpy("UUPOLL: Calling ",s2);
	stradd(hst,s2);
	
	log(s2,"uucico");
	
	i = noenv_exec(s,give);

	log("UUCICO returned control to uupoll. Finished.","uucico");
	sys_remove(give);

	if (exist("\\uucico.bck") == TRUE)
	{
		inh = open("\\uucico.bck",2);

		getline(inh,s);
		code = val(s);

		getline(inh,s);
		xerr = lval(s);

		getline(inh,s);
		xout = lval(s);

		getline(inh,s);
		stime = lval(s);

		getline(inh,s);
		sratio = lval(s);

		getline(inh,s);
		xin = lval(s);

		getline(inh,s);
		rtime = lval(s);

		getline(inh,s);
		rratio = lval(s);

		close(inh);
#if 0
		sys_remove("\\uucico.bck");
#endif

		sys_impdirn(logdir);
		sprintf(give,"%suustatus",logdir);
		mktmp(tmpdir);

		outh = open(tmpdir,1);
		if (exist(give) == TRUE)
		{
			inh = open(give,2);
			while (getline(inh,s) != -1)
			{
				strcpys(s,s2,0,linstr(s,' ') - 1);
				if (strcmp(s2,system) != TRUE)
					uu_putline(outh,s);
			}
			close(inh);
		}
		else
		{
			inh = open(give,1);
			/*              "0             14     21    28   33      41    47   52" */
			uu_putline(outh,"                     Received             Sent");
			uu_putline(outh,"System     Xerr   Bytes Time Ratio   Bytes Time Ratio");
			close(inh);
		}

#if 1 /* Simple or verbose output? */
		switch(code)
		{
			case 0: cp = "Fail";		break;
			case 1: cp = "Abort";		break;
			case 2: cp = "Succ";		break;
		}


		strcpy("",s);

		sprintf(s2,"%s                    ",system);
		s2[12] = '\0';
		stradd(s2,s);
		
		sprintf(s2,"%3ld                   ",(long)xerr);
		s2[5] = '\0';
		stradd(s2,s);

		/* Receive-Part */
		
		sprintf(s2,"%6ld                   ",(long)xin);
		s2[6] = '\0';
		stradd(s2,s);
		
		sprintf(s2,"%5ld                   ",(long)rtime);
		s2[7] = '\0';
		stradd(s2,s);
		
		sprintf(s2,"%4ld                   ",(long)rratio);
		s2[6] = '\0';
		stradd(s2,s);

		/* Now we do the send-part */
		
		sprintf(s2,"%6ld                   ",(long)xout);
		s2[6] = '\0';
		stradd(s2,s);
		
		sprintf(s2,"%5ld                   ",(long)stime);
		s2[7] = '\0';
		stradd(s2,s);
		
		sprintf(s2,"%4ld                   ",(long)sratio);
		s2[6] = '\0';
		stradd(s2,s);

		sd(s2);
		s2[rinstr(s2,':')] = '\0';
		stradd(s2,s);

		sprintf(s2,"  %s",cp);
		stradd(s2,s);

		uu_putline(outh,s);
		log(s,"uucico");
#else /* Simpley */
		switch(code)
		{
			case 0: cp = "No Answer";		break;
			case 1: cp = "Conversation failed";	break;
			case 2: cp = "Successful";		break;
		}

		sd(s2);
		sprintf(s,"%s (%s): %s",system,s2,cp);
		uu_putline(outh,s);
#endif

		close(outh);
		sys_fcopy(tmpdir,give);
	}

	curs();
	cr(1);
}

