#define MAINPROGRAM

#include "xvars.h"
#include "alive.h"

char *OrgFile = ORGAN_FILE;

main(argc,argv)
int	argc;
char	*argv[];
{	int	i;

	makeToplevel(&argc,argv);
	for (i = 1; i < argc; i++) {
		DisplayOrgans[DisplayOrganNum++] = argv[i];
	}
	readOrgan(OrgFile);
	if (DisplayOrganNum == 0) {
		DisplayOrgans[0] = MyOrgan;
		DisplayOrganNum = 1;
	}

	gethost(SortHosts);
	WhNum = 0;
	for (i = 0; i < DisplayOrganNum; i++) 
		checkHost(DisplayOrgans[i]);
	
	if (WhNum == 0) {
		fprintf(stderr,"No host.\n");
		exit(0);
	}
	initializeWidgets();
	initializeBitmaps(Toplevel);
	stay();
	exit(0);
}

getWhoData(wd,file)
struct whod	*wd;
char		*file;
{	FILE	*f;
	int	j;
	static
	char	whodfile[FNLEN];

	sprintf(whodfile,"%s/whod.%s",SPOOLDIR,file);
	if ((f = fopen(whodfile,"r")) == NULL)
		return -1;
	for (j = 0; j < MAXWHO; j++)
		wd->wd_we[j].we_utmp.out_line[0] = '\0';
	fread(wd,sizeof(struct whod),1,f);
	fclose(f);
	return 0;
}

printOk(name,org)
char	*name,*org;
{
	if (strcmp(org,"all") == 0)
		return inAnyOrgan(name);
	if (strcmp(org,"others") == 0 &&
		outOfOrgan(name))
		return 1;
	if (strcmp(org,"everything") == 0)
		return 1;
	return inOrgan(name,org);
}

checkHost(org)
char *org;
{	int	i;

	for (i = 0; i < HostNum; i++) {
		if (printOk(HostNames[i],org))
			getWhoData(&Whodata[WhNum++],HostNames[i]);
	}
}

static int	hostn;

gethost(sort)
bool sort;
{
	DIR *fd;
	Directory *dp;
	int i,j;
	char whodname[FNLEN];

	hostn = 0;
	HostNum = 0;

	if (sort) {
		if ((fd = opendir(SPOOLDIR)) == NULL) {
			fprintf(stderr,"Can't open spool\n");
			exit(2);
		}

		while ((dp = readdir(fd)) != NULL) {
			if (strncmp(dp->d_name,"whod.",5) == 0)
				addHost(dp->d_name+5);
		}
		closedir(fd);
	}
	else {
		for (i = 0; i < DisplayOrganNum; i++)
			getHostOfOrgan(DisplayOrgans[i]);
	}
}

getHostOfOrgan(org)
char *org;
{
	int i;
	DIR *fd;
	Directory *dp;

	if (strcmp(org,"all") == 0 ||
	    strcmp(org,"everything") == 0 ||
	    strcmp(org,"others") == 0) {
		for (i = 0; i < OrganNum; i++)
			addHostOfOrgan(Organizations[i].h_list);

		if ((fd = opendir(SPOOLDIR)) == NULL) {
			fprintf(stderr,"Can't open spool\n");
			exit(2);
		}
		while ((dp = readdir(fd)) != NULL) {
			if (strncmp(dp->d_name,"whod.",5) == 0 &&
			    printOk(dp->d_name+5,"others")) {
				strcpy(HostBuffer[hostn], dp->d_name+5);
				HostNames[HostNum++] = HostBuffer[hostn++];
			}
		}
		closedir(fd);
	}
	else {
		for (i = 0; i < OrganNum; i++) {
			if (strcmp(Organizations[i].organ,org) == 0) {
				addHostOfOrgan(Organizations[i].h_list);
				return;
			}
		}
	}
}

addHostOfOrgan(hlist)
HostList *hlist;
{
	char buf[FNLEN];
	FILE *f;

	if (hlist->nexthost)
		addHostOfOrgan(hlist->nexthost);
	sprintf(buf,"%s/whod.%s",SPOOLDIR,hlist->hostname);
	if (f = fopen(buf,"r")) {
		fclose(f);
		strcpy(HostBuffer[hostn],hlist->hostname);
		HostNames[HostNum++] = HostBuffer[hostn];
		hostn++;
	}
}
	

addHost(hostfile)
char	*hostfile;
{	int	i,j;
	char	*hf;

	strcpy(HostBuffer[hostn],hostfile);
	hf = HostBuffer[hostn];
	hostn++;
	for (i = 0; i < HostNum; i++) {
		if (strcmp(HostNames[i],hf) > 0) {
			for (j = HostNum; j >= i; j--)
				HostNames[j+1] = HostNames[j];
			HostNames[i] = hf;
			HostNum++;
			return;
		}
	}
	HostNames[HostNum++] = hf;
}

readOrgan(org)
char *org;
{	FILE	*f;
	static
	char	str[NAMELEN];
	short	n,i;
	char	c,*p;
	HostList	*hl;

	if ((f = fopen(org,"r")) == NULL) {
		MyOrgan = "others";
		return;
	}
	MyOrgan = NULL;
	for(;;) {
		n = 0;
		if ((c = getc(f)) == '#') {
			while (getc(f) != '\n');
			continue;
		}
		else
			ungetc(c,f);
		if (fscanf(f,"%s:",str),feof(f)) {
			return;
		}
		for (p = &str[strlen(str)-1];
		       *p == ':' || *p == ' ' || *p == '\t' || *p == '\n';
		       p--);
			*(p+1)='\0';
		Organizations[OrganNum].organ = allocStr(str);
		if (MyOrgan == NULL)
			MyOrgan = Organizations[OrganNum].organ;
		for (;;) {
			n++;
			i = 0;
			skip(f);
			while ((c = getc(f)) != ' ' && c != '\t' && c != '\n')
				str[i++] = c;
			str[i] = '\0';
			hl = (HostList*)malloc(sizeof(HostList));
			hl->hostname = allocStr(str);
			hl->nexthost = Organizations[OrganNum].h_list;
			Organizations[OrganNum].h_list = hl;
			
			if (c == '\n') {
				c = getc(f);
				if (c != ' ' && c != '\t' && c != '\n') {
					ungetc(c,f);
					break;
				}
			}
		}
		Organizations[OrganNum].hostNum = n;
		OrganNum++;
	}
}

skip(f)
FILE	*f;
{	char	c;

	while ((c = getc(f)) == ' ' || c == '\t' || c == '\n');
	ungetc(c,f);
}

char *allocStr(s)
char	*s;
{	char	*a;

	a = malloc(strlen(s)+1);
	strcpy(a,s);
	return a;
}

inOrgan(host,org)
char	*host,*org;
{	int		i;
	HostList	*hl;

	for (i = 0; i < OrganNum; i++) {
		if (strcmp(Organizations[i].organ,org) == 0) {
			for (hl = Organizations[i].h_list;
				hl != NIL;
				hl = hl->nexthost) {
				if (strcmp(hl->hostname,host) == 0) {
					return 1;
				}
			}
			return 0;
		}
	}
	return 0;
}

inAnyOrgan(host)
char	*host;
{	int		i;
	HostList	*hl;

	for (i = 0; i < OrganNum; i++) {
		for (hl = Organizations[i].h_list;
		     hl != NIL;
		     hl = hl->nexthost) {
			if (strcmp(hl->hostname,host) == 0) {
				return 1;
			}
		}
	}
	return 0;
}

outOfOrgan(host)
char	*host;
{	int		i;
	HostList	*hl;

	for (i = 0; i < OrganNum; i++) {
		for (hl = Organizations[i].h_list; hl != NIL;
			hl = hl->nexthost) {
			if (strcmp(hl->hostname,host) == 0)
				return 0;
		}
	}
	return 1;
}


syntax highlighted by Code2HTML, v. 0.9.1