/* 	netstat.c by Juha 22-Dec-91,	Pasi 29-Dec-91

	Funny thing, you know, I wrote this whole program in 1 hour and I didn't
	test it even once. When I compiled it for the very first time there was
	no errors and when I ran this it worked just as I planned. What am I doing
	wrong here? :-o Was this an early christmas gift for me??? O:-)

	Btw, I made this compatible to normal net.. and optimized a bit.. t: Pasi
*/

#include <exec/types.h>
#include <exec/memory.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <libraries/dosextens.h>
#include <libraries/dos.h>
#include <proto/all.h>


#define MINCC 		100		/* min value for condition code (not used) */
#define MAXCC 		106		/* max value for condition code (not used) */
#define MAXGROUPS	256
#define NAMELEN		64

char *vers="\0$VER: netstat 1.0 (26.10.92)\n";

char 	*months[]={"","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
int	 	year, month, day, NumGrp=0;
char	Groups[MAXGROUPS+1][NAMELEN],
		Host[NAMELEN]="amiga.pasbox.fi"; /* %thismachine */

char *get_time(void);
int  chkgrp(char *string);	/* returns group index (string== "<id> <num> <group>")*/
int	 chknum(char *string);	/* returns <num> (usually number of messages) */
void read_config(void);
void separate(char *date);


char *get_time()
{	long joo;

	joo=time(&joo);
	return asctime(localtime(&joo));
}

void separate(char *date)
{	/* Pasi, is this tight enough? :-) */
	/* Yes, if you know the date format.. :-) */

	day=month=year=0;

	while(strncmp(date+4,months[++month],3));
	day=atoi(date+8);
	year=atoi(date+22);
}


void read_config()
{	FILE *handle;
	char line[80];
	int found=0;

	if(handle=fopen("AXsh:etc/rc","rb"))	/* luetaan config */
	{
		while(!feof(handle))
		{
			if(fgets(line,80,handle)==NULL)
				break;

			if(!found)
			{
				if(!strncmp(line,"%thismachine:",13))
					found=1;
			}
			else
			{	strcpy(Host,line);
				found=0;
			}
		}
		fclose(handle);
	}
	else
		printf("AXsh:etc/rc not found!\n");
}


int chkgrp(char *string)
{	int o=5,p=0;

	while(string[o] && string[o]!=' ')	o++;
	while(strcmp(Groups[p],string+o+1) && p<NumGrp)	p++;
	if(p==NumGrp && NumGrp<MAXGROUPS-1)
		strcpy(Groups[NumGrp++],string+o+1);
	return p;
}


int chknum(char *string)
{	int i=0;

	while(string[i] && string[i]!=' ')	i++;
	return atoi(string+i);
}


void main(int argc, char *argv[])
{
	FILE	*fp;
	char	temp[80];
	int		netday=0,netmonth=0,netyear=0;
	int		connection=0,tmp;
	int		rec[MAXGROUPS+1],rej[MAXGROUPS+1],rtotal=0,total=0,iex=0,ex=0,files=0;

	for(tmp=1;tmp<argc;tmp++)
	{
		if(!strncmp(argv[tmp],"-?",2))
		{	printf("Usage: netstat [dd-mm-yy]\n");
			exit(0);
		}
		if(strlen(argv[tmp])==8)
		{
			strncpy(temp,argv[tmp],2);
			netday=atoi(temp);
			strncpy(temp,argv[tmp]+3,2);
			netmonth=atoi(temp);
			strncpy(temp,argv[tmp]+6,2);
			netyear=atoi(temp);
			if(netday<1 || netday>31 || netmonth<1 || netmonth>12 || netyear<1)
			{
				printf("Come on.. Use a real date.\n");
				exit(0);
			}
		}
	}

	read_config();

	for(tmp=MAXGROUPS;tmp>=0;tmp--)	rec[tmp]=rej[tmp]=0;
	if(fp=fopen("AXsh:etc/adm/netlog","rb"))
	{
		if(netday)	/* if netday!=0, we had a date on arguments */
		{
			do
			{
				if(fgets(temp,80,fp)==NULL)
					break;

				temp[strlen(temp)-1]=0;	/* get the \n out of the string */
				if(!strncmp(temp,"100",3))
				{
					separate(temp+4);
					if(year>=netyear && month>=netmonth && day>=netday)
					{
						connection++;
						break;
					}
				}
			}
			while(!feof(fp));
		}
		do
		{
			if(fgets(temp,80,fp)==NULL)
				break;

			temp[strlen(temp)-1]=0;	/* get the \n out of the string */

			switch(atoi(temp))
			{
				case 100:
					connection++;
					break;
				case 102:
					rec[chkgrp(temp)]+=chknum(temp);
					rtotal+=chknum(temp);
					break;
				case 103:
					rej[chkgrp(temp)]+=chknum(temp);
					break;
				case 104:
					total+=chknum(temp);
					break;
				case 105:
					iex++;
					break;
				case 106:
					ex++;
					break;
				case 110:
					files+=chknum(temp);
					break;
				default:
					break;
			}
		}
		while(!feof(fp));
		fclose(fp);
		printf("Net statistics for %s:\n",Host);
		printf("Connections:                %d\n",connection);
		printf("Normal exits:               %d\n",ex);
		printf("Irregular exits:            %d\n",iex);
		printf("Total messages received:    %d (%d)\n",total,rtotal);
		printf("Files received:             %d\n",files);
		printf("Groups:                     %d\n\n",NumGrp);

		if(rtotal)
			for(tmp=0;tmp<NumGrp;tmp++)
				printf("%-30s: %5d received, %4d rejected  %2d.%c %%\n",Groups[tmp],rec[tmp],rej[tmp],rec[tmp]*100/rtotal,((rec[tmp]*1000+rtotal/2)/rtotal)%10+'0');
		if(rec[MAXGROUPS] || rej[MAXGROUPS])
				printf("%-30s: %5d received, %4d rejected  %2d.%c %%\n","Other",rec[MAXGROUPS],rej[MAXGROUPS],rec[MAXGROUPS]*100/rtotal,((rec[MAXGROUPS]*1000+rtotal/2)/rtotal)%10+'0');
	}
}
