/****************************************/
/* This is the checkpkgs 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 checkpkgs(version, kernel, distribution, filename, verbose)
char version[50];
char kernel[50];
int distribution;
const char *const filename;
int verbose;
{

	/* note if distribution = 1, redhat */
	/* if distribution = 2, debian */
	char *templistptr;	/* ptr to templist file */
	char line[80];		/* array for a line	*/
        char temp1[80];		/* temp char 		*/
        int returnval;		/* int for return value */
        int i=0;		/* counter variable	*/
	int submatch=0;		/* match flag		*/
	int fileval=0;		/* return val from mkstemp */

	/* list of rpms to check? 		*/
	/* NOT a comprehensive list yet.	*/
	/* just making sure this works...	*/
	char rpmlist[14][15] = {"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 checkpkgs 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, "Installed packages found that are non-essential:\n");
	fprintf(outfile, "\n");
	
	if (verbose == 1)
	{
		printf("Generating list of rpms...\n");
	}

	
	/* create a temp file for storage */
        fileval = mkstemp("/tmp/templist.txt");
        if (fileval < -1)
        {
                printf("Unable to create tempfile!?\n");
                printf("Problem was in module: checkpkgs\n");
                return(-1);
        }

	/* if distro = redhat, use rpm */
	if (distribution == 1)
	{
		if ((system("rpm -qa >> /tmp/templist.txt")) < 0)
		{
			/* rhut-rho...something bad happened */
			perror("Problem writing to /tmp?!?\n");
			perror("Problem was in module: checkpkgs.\n");
			return(-1);
		}
	}
	
	/* if distro = debian, use dpkg */
	if (distribution == 2)
	{
		if ((system("dpkg -l |awk -F" " 'length($2) > 0 {print $2}' 1>/dev/null 2>/dev/null >> /tmp/templist.txt")) < 0)
		{
			/* rhut-rho...something bad happened */
                        perror("Problem writing to /tmp?!?\n");
			perror("Problem was in module: chkpkgs.\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");
		perror("Error was in module: checkpkgs.\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");
				perror("Failure in module: checkpkgs.\n");
				return(-1);		
			}
				for (i=0; i<12; i++)
				{
					if (strstr(temp1, rpmlist[i]) != NULL)
					{
						/* substring matched */
						/* set flag	     */
						submatch = 1;
						/* print match out   */
						fprintf(outfile, "%s\n", temp1);
					}
				}
		}
	}
	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");
		perror("Problem was in module: checkpkgs.\n");
		return(-1);
	}
	

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