/* Application - HPDI32 - Linux */
/* $Revision: 1 $ */
/* $Date: 10/27/07 4:35p $ */

#include "util_host.h"

#include "util_label.h"

#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <sys/utsname.h>



/*****************************************************************************
*
*	Function:	_print_str
*
*	Purpose:
*
*		Print a string with compacted white space.
*
*	Arguments:
*
*		str		The string to process and print.
*
*		eol		Any end of line sequence to print.
*
*	Returned:
*
*		>= 0	The number of errors seen.
*
*****************************************************************************/

static void _print_str(const char* str, const char* eol)
{
	for (;;)
	{
		// Skip any leading white space.

		for (; isspace(str[0]);)
			str++;

		// Compact any white space that follows.

		if (str[0] == 0)
			break;

		for (;str[0];)
		{
			if (isspace(str[0]))
			{
				printf(" ");
				break;
			}

			printf("%c", str[0]);
			str++;
		}
	}

	if (eol)
		printf("%s", eol);
}



/*****************************************************************************
*
*	Function:	id_host
*
*	Purpose:
*
*		Identify the operating system.
*
*	Arguments:
*
*		None.
*
*	Returned:
*
*		>= 0	The number of errors seen.
*
*****************************************************************************/

int id_host(void)
{
	char			buf[1024];
	int				end;
	FILE*			file;
	int				i;
	int				index;
	char*			psz;
	char			tmp[1024];
	struct utsname	uts;

	memset(&uts, 0, sizeof(uts));
	uname(&uts);
	label_text("Host");
	printf("\n");
	label_level_inc();

	label_text("Operating System");
	printf("%s\n", uts.sysname);
	label_text("Kernel");
	printf("%s\n", uts.release);
	label_text("Processor");
	printf("%s\n", uts.machine);
	label_level_inc();

	file	= fopen("/proc/cpuinfo", "r");

	if (file)
	{
		for (;;)
		{
			end	= feof(file);

			if (end)
				break;

			psz	= fgets(buf, sizeof(buf), file);
			i	= sscanf(buf, "processor : %d", &index);

			if (i == 1)
			{
				label_level_dec();
				sprintf(buf, "Processor #%d", index);
				label_text(buf);
				printf("\n");
				label_level_inc();
				continue;
			}

			i	= sscanf(buf, "model name : %s", tmp);

			if (i == 1)
			{
				label_text("CPU Model");
				psz	= strchr(buf, ':') + 2;
				_print_str(psz, "\n");
				continue;
			}

			i	= sscanf(buf, "cpu MHz : %s", tmp);

			if (i == 1)
			{
				label_text("CPU Speed");
				printf("%s MHz\n", tmp);
				continue;
			}
		}

		fclose(file);
	}

	label_level_dec();
	label_level_dec();
	return(0);
}



/*****************************************************************************
*
*	Function:	host_issue_os
*
*	Purpose:
*
*		See if the OS might affect operation.
*
*	Arguments:
*
*		None.
*
*	Returned:
*
*		0		All went well.
*		> 0		The number of errors encountered.
*
*****************************************************************************/

int host_issue_os(void)
{
	return(0);
}


