;/*
DCC UserInfo.C -o UserInfo -lm
Quit
*/

/*                          _
	UserInfo for AXsh by Guru Gnosis Sahib
	v1.0 - 15-Oct-93 - First working implementation
	v1.1 - 12-Dec-93 - Added xpr.log analysis, moved in questionnaire
					   and recent calls code from another utility
	v1.2 - 26-Dec-93 - Now uses System() instead of Execute()
	v1.3 - 06-Jan-93 - Superuser commands now allowed for assistant,
					   superuser, wizard and system

	Written with DICE C registered, should compile relatively nicely
	with other systems as well, just link in a math library.  For DICE,
	just type "Execute UserInfo.C" and the rest is magic.
*/

#include <stdio.h>
#include <string.h>
#include <libraries/dos.h>
#include <dos/dostags.h>
#include <dos/var.h>

#define TRUE		1
#define FALSE		0
#define NORMAL		0
#define SUPERUSER	1

main(argc,argv)
int argc;
char *argv[];
{
	FILE *input, *input2;
	BPTR in, out;
	char user[80], level[80], first[80], temp[80], answers[80];
	long byte, ulbyte = 0, dlbyte = 0;
	int ulfile = 0, dlfile = 0, tcps = 0, times = 0, ttime = 0;
	int cps, first_ch = TRUE, utype = NORMAL, success, ques, foo;

	if(!(success = GetVar("user", user, 80, GVF_LOCAL_ONLY)))
	{
		puts("UserInfo - couldn't open local variable 'user'");
		exit(5);
	}
	if(!strcmp(user, ""))
	{
		puts("UserInfo - couldn't open local variable 'user'");
		exit(5);
	}
	if(argc > 1)
	{
		if(!(success = GetVar("accesslevel", level, 80, GVF_LOCAL_ONLY)))
		{
			puts("UserInfo - couldn't open local variable 'accesslevel'");
			exit(5);
		}
		if(!stricmp(level, "superuser") || !stricmp(level, "assistant") ||
			!stricmp(level, "wizard") || !stricmp(level, "system"))
		{
			if(!strcmp(argv[1], "?") || !strncmp(argv[1], "-", 1))
			{
				puts("\nUserInfo v1.3 for AXsh by Guru Gnosis Sahib");
				puts("Usage: UserInfo [username]\n");
				exit(0);
			}
			strcpy(user, argv[1]);
			utype = SUPERUSER;
		}
		else
		{
			puts("Access denied!");
			exit(5);
		}
	}
	puts("\nUserInfo v1.3 by Guru Gnosis Sahib\n");
	sprintf(temp, "AXsh:bin/passwd -d %s", user);
	success = System(temp, TAG_END);
	if(success != 0)
	{
		puts("Scan usage.log and xpr.log anyway? (y/N)");
		gets(temp);
		if(stricmp(temp, "y")) exit(0);		/* if not 'y' then exit */
	}
	if(!(input = fopen("AXsh:etc/adm/usage.log", "r")))
	{
		puts("UserInfo - could not open AXsh:etc/adm/usage.log");
		exit(0);
	}
	while(!(feof(input)))
	{
		fscanf(input, "%s", temp);
		if(!(strnicmp(user, temp, strlen(user))))
		{
			fscanf(input, "%s", temp);	/* Date */
			if(first_ch == TRUE)
			{
				first_ch = FALSE;
				strcpy(first, temp);
			}
			fscanf(input, "%s", temp);	/* Login */
			fscanf(input, "%s", temp);	/* Logout */
			fscanf(input, "%s", temp);	/* Time! */
			ttime += atoi(temp);
			times++;
		}
	}
	fclose(input);
	printf("Total login time: %d minutes online\n", ttime);
	printf("Total logins    : %d logins\n", times);
	printf("Av. login length: %d minutes\n", ttime / times);
	printf("First login     : %s\n\n", first);
	if(!(input = fopen("AXsh:etc/adm/xpr.log", "r")))
	{
		puts("UserInfo - could not open AXsh:etc/adm/xpr.log");
		exit(0);
	}
	fgets(temp, 80, input);		/* Header */
	fgets(temp, 80, input);		/* Blank line */
	while(!(feof(input)))
	{
		fscanf(input, "%d %d %d %s", &foo, &byte, &cps, temp);		/* Date */
		if(!strcmp(temp, user) && !feof(input))
		{
			fscanf(input, "%s", temp);
			if(!strcmp(temp, "Rcvd"))
			{
				ulbyte += byte;
				ulfile++;
			}
			else
			{
				dlbyte += byte;
				dlfile++;
			}
			tcps += cps;
		}
		if(!feof(input)) fgets(temp, 80, input); /* Read in the rest */
	}
	fclose(input);
	printf("Bytes downloaded: %-8ld             Files downloaded: %-4d\n",
		dlbyte, dlfile);
	printf("Bytes uploaded  : %-8ld             Files uploaded  : %-4d\n",
		ulbyte, ulfile);
	if(dlfile != 0)
	{
		printf("Byte UL/DL ratio: %2.4f               File UL/DL ratio: %2.4f\n",
			(float) ulbyte / dlbyte, (float) ulfile / dlfile);
	}
	else
		puts("Byte UL/DL ratio: N/A                  File UL/DL ratio: N/A");
	if(dlfile + ulfile != 0)
		printf("Average CPS rate: %d\n\n", tcps / (ulfile + dlfile));
	else
		puts("Average CPS rate: N/A\n");

	if(utype != SUPERUSER) exit(0);
	puts("--- Q to quit, ENTER for more ---");
	gets(temp);
	if(!stricmp(temp, "q")) exit(0);

	if(!(input = fopen("AXsh:etc/rc", "r")))
	{
		puts("UserInfo: could not open AXsh:etc/rc");
		exit(1);
	}
	while(!(feof(input)))
	{
		fgets(temp, 80, input);
		if(!(strnicmp(temp, "%answers:", 9)))
			fscanf(input, "%s", answers);
	}
	fclose(input);

	if(strlen(answers) == 1) strcpy(answers, "AXsh:etc/adm/Answers");
	if(!(input = fopen("AXsh:etc/Questionnaire", "r")))
	{
		puts("UserInfo: could not open AXsh:etc/Questionnaire");
		exit(1);
	}
	if(!(input2 = fopen(answers, "r")))
	{
		printf("UserInfo: could not open %s", answers);
		exit(1);
	}

	puts("Questionnaire answers:\n");
	ques = FALSE;
	while(!(feof(input2)))
	{
		fscanf(input2, "%s", temp);
		if(!(strcmp(temp, "User:")))
		{
			fscanf(input2, "%s", temp);
			if(!(stricmp(temp, user)))
			{
				ques = TRUE;
				fgets(temp, 80, input2);
				while(!(feof(input)))
				{
					fgets(temp, 80, input);
					if (!(feof(input)))
					{
						printf("- %s", temp);
						fgets(temp, 80, input2);
						printf("%s", temp);
					}
				}
			}
		}
	}
	if (ques == FALSE)
		puts("No questionnaire answers found.");
	fclose(input);
	fclose(input2);

	puts("\n--- Q to quit, ENTER for more ---");
	gets(temp);
	if(!stricmp(temp, "q")) exit(0);

	puts("Recent calls:\n");
	ques = FALSE;
	if(!(input = fopen("AXsh:etc/last", "r")))
	{
		puts("UserInfo: could not open AXsh:etc/last");
		exit(0);
	}
	while(!feof(input))
	{
		fgets(temp, 80, input);
		if(!(strnicmp(user, temp, strlen(user))))
		{
			ques = TRUE;
			printf("%s", temp);
		}
	}
	if(ques == FALSE) puts("No recent calls found.");
	fclose(input);
	puts("");
	exit(0);
}
