/********************************************/
/* This module will find all files on the   */
/* system and print an md5sum for each one  */
/* out to a file...not the lsat.out file.   */
/* In this way the output can be diffed     */
/* against an older one. If this module was */
/* already run, the old outfile will be     */
/* renamed to .old. I may put in a switch   */
/* to automagically diff the output....     */
/* This will make it easier for someone to  */
/* cp the md5sum file off the machine or    */
/* to archive it.			    */
/* 					    */
/* See lsatmain.c for more details.         */
/********************************************/
 
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

int checkmd5(filename, distribution, verbose, html)
const char *const filename;
int verbose;
int distribution;
int html;
{
    const char * header   =NULL;
    const char * tempfile =NULL;
    const char * outfile2 =NULL;
    const char * shellcode=NULL;
    const char *outputfile = "lsatmd5.out"; 
    FILE *tempptr;
  
    if (verbose >= 0)
    {
        printf(" Running checkmd5 module...\n");
    }

    header = "Check lsatmd5.out for output of checkmd5.\nIf this is a subsequent run, old one is called lsatmd5.out.old\n";
    if ((dostuff(0, filename, 0, header, html)) < 0)
    {
        /* something went wrong */
        perror(" Creation of list failed.\n");
        return(-1);
    }


    /* ok, make a temp file for holding the output */
    tempfile="/tmp/lsat1.lsat";
 
    /* lets get a list of files and md5(sum) them... */
    /* denote again... under Mac OSX its md5 and not md5sum... */
    if (distribution == 5)
    {
	shellcode = "find / -type f -print0 2>/dev/null |xargs -0 md5  2>/dev/null >> /tmp/lsat1.lsat";
    }
    else
    {
        shellcode = "find / -type f -exec md5sum \\{\\} \\; 2>/dev/null >> /tmp/lsat1.lsat";
    }

    if ((dostuff(tempfile, 0, shellcode, 0, html)) < 0)
    {
        /* something went wrong */
        perror(" Creation of list failed.\n");
        return(-1);
    }
    
    /* ok, now all of these find outputs are in the tempfile */
    /* now we print that out to another .out file...         */

    outfile2 = "lsatmd5.out";
    
    /* check if output exists, if it does, make a       */
    /* backup and rm the orig. named one.               */
    tempptr = fopen(outputfile, "r");
    if (tempptr != 0)
    {
        /* output file exists */
        /* make a backup copy */
        char *newname = NULL;
        if ((newname = (char *) calloc(2, sizeof(outputfile))) == NULL)
        {
            perror("Can not allocate temp memory in checkmd5.\n");
            perror("Strange. Bailing...\n");
            exit(-1);
        }
        fclose(tempptr);
        /* add .old onto the old outfile name */
        sprintf(newname, "%s.old", outputfile);

        /* gnu, but should be ok... */
        /* no failure check? eh, its ok...   */
        rename(outputfile, newname);

        /* free up calloc'd mem */
        free(newname);
    }

    /* now that we have a backup, remove the outfile */
    /* don't care about failure here...              */
    remove(outputfile);

    /* ok, now that we have a backup of the orig file*/
    /* write the new one out to disk...		     */

    if ((dostuff(tempfile, outfile2, 0, 0, html)) < 0)
    {
        /* something went wrong */
        perror(" Creation of list failed.\n");
        return(-1);
    }
 

    if (verbose > 0)
    {
        printf(" Finished in checkmd5 module.\n");
    }

    return(0);
}

