/****************************************/
/* This is the checkrpms module for the */
/* lsatmain.c program. It checks the    */
/* currently installed rpms against a   */
/* (somewhat small) list of unwanted 	*/
/* rpms. It then prints out this list   */
/* to the outfile.			*/
/*					*/
/* Note: perhaps a checkdeb should be   */
/* written also. Or a checkpkg for 	*/
/* Solaris. Hrm, Ill look at it.	*/
/*					*/
/* see lsatmain.c for more details	*/
/****************************************/

#include "lsatheader.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int checkrpms(version, kernel, filename, verbose)
char version[50];
char kernel[50];
const char *const filename;
int verbose;
{

	char *templistptr;	/* ptr to templist file */
        char *rpmlistptr;	/* ptr to rpmlist array */
	char line[80];		/* array for a line	*/
        char temp1[40];		/* temp char 		*/
        char temp2[40];		/* temp char		*/
        int returnval;		/* int for return value */
        int i=0;		/* counter variable	*/
	int submatch=0;		/* match flag		*/

	/* list of rpms to check? 		*/
	/* NOT a comprehensive list yet.	*/
	/* just making sure this works...	*/
	char rpmlist[] = "identd lpd linuxconf netfs portmap routed rstatd rusersd rwalld rwhod sendmail ypbind yppasswdd ypserv";

	FILE *outfile;		/* output file pointer 	*/
	FILE *templist;		/* file pointer to templist file */

	if (verbose == 1)
	{
		printf("Starting checkrpms module...\n");
	}


	outfile=fopen(filename, "a");
	if (outfile == NULL)
	{
		perror("Can't open outfile!\n");
		perror("Check perms or something...\n");
		return(-1);
	}

	/* print some header info to the outfile...  */
	fprintf(outfile, "\n");
	fprintf(outfile, "****************************************\n");
	fprintf(outfile, "rpm packages found that are non-essential:\n");
	fprintf(outfile, "\n");
	
	if (verbose == 1)
	{
		printf("Generating list of rpms...\n");
	}


	if ((system("rpm -qa >> /tmp/templist.txt")) < 0)
	{
		/* rhut-rho...something bad happened */
		perror("Problem writing to /tmp?!?\n");
		return(-1);
	}
	
	templist=fopen("/tmp/templist.txt", "r");
	if (templist == NULL)
	{
		perror("Can't open /tmp/templist.txt!!\n");
		perror("Check perms or something...\n");
		return(-1);
	}

	if (verbose == 1)
	{
		printf("Checking rpm list...\n");
	}


	/* ok, for each entry in rpmlist we want to  */
	/* check if that exitst in templist...       */

	/* to speed things up, we only read through  */
	/* templist once, for each line in it        */
	/* we compare to all in the rpmlist...       */

	while (fgets(line, sizeof(line), templist) != NULL)
	{
		templistptr = strtok(line, ", \n");
		if (templistptr != NULL)
		{
			if (sscanf(templistptr, "%s", temp1) < 1)
			{
				perror("sscanf failed.\n");
				return(-1);		
			}
			rpmlistptr = strtok(rpmlist, ", \n");
			if (rpmlistptr != NULL)
			{
				for (i=0; i<12; i++)
				{
					if (sscanf(rpmlistptr, "%s", temp2) <1)
					{
						perror("sscanf failed.\n");
						return(-1);
					}
					if (strstr(temp1, temp2) != NULL)
					{
						/* substring matched */
						/* set flag	     */
						submatch = 1;
						/* print match out   */
						fprintf(outfile, "%s\n", temp1);
					}
					rpmlistptr = strtok(NULL, ", \n");
					if (rpmlistptr == NULL)
					{
						;
					}
				}
			}
		}
		rpmlistptr=0;
	}
	if (submatch == 1)
	{
		fprintf(outfile, "Please consider removing the above packages.\n");
		fprintf(outfile, "****************************************\n");
	}
	if (submatch == 0)
	{
		fprintf(outfile, "No installed rpms matched my list of non-essentials. Good.\n");
	}
	fprintf(outfile, "****************************************\n");
	fclose(outfile);
	fclose(templist);	
	if ((system("rm -f /tmp/templist.txt")) < 0)
	{
		perror("Promblem rm'ing tempfile...\n");
		return(-1);
	}
	

	if (verbose == 1)
	{
		printf("Finished in checkrpms module.\n");
	}
	return(0);
}
