/* uuclean.c ... remove extraneous stuff from captured
   uuencoded files 07 Nov 1995 */

#include <stdio.h>
#define INCHARS 128
#define MAX 6

main(int argc,char *argv[])

{
   FILE *infile;
   FILE *outfile;
   char instring[INCHARS];
   char oldstring[INCHARS];
   char teststr[MAX];
   char testend[MAX];
   char testsize[MAX];
   char oneback[INCHARS];
   char twoback[INCHARS];
   char threeback[INCHARS];
   char current[INCHARS];
   static char beginstr[] =  "begin";
   static char beginc[] = ":";
   static char beging[] = ">";
   static char space[] = " ";
   static char endstr[] = "end";
   static char sizestr[] ="size";
   static char mstring[] = "M";
   static char version[] = "$VER: uuclean 0.4 (10.08.1996)";
   int i,j;
   int start = 1;
   int end = 1;
   int size = 1;
   int write = 0;
   
   if (argc != 3)
   {
      printf ("\n UUCLEAN by rdavis@nyx.cs.du.edu (Robert Davis), version 0.4");
      printf ("\n Usage: %s INPUT OUTPUT \n Where INPUT and OUTPUT are filenames\n"
      ,argv[0]);
      exit(1);
   }  

   if ((infile = fopen(argv[1],"r")) == NULL)
   {
      printf ("\n Cannot open %s\n",argv[1]);
      exit(1);
   }
   
   if (( outfile = fopen(argv[2],"w")) == NULL)
   {
      printf ("\n Unable to open output file %s\n",argv[2]);
      exit(2);
   }
   printf ("\n Working ... CTRL C exits\n");
   
   do   
   {  fgets (instring,INCHARS,infile); /* get a line from the file */
      i = j = strlen(instring);
   
      /* check for a quote indicator */
      if ((*instring == *beginc) || (*instring == *beging))
         {for (i=0;i<strlen(instring);i++)
            (instring[i] = instring[i+1]);}
  
      if (*instring == *space)
         { for (i=0;i<strlen(instring);i++)
            (instring[i] = instring[i+1]); }
            
      if (i != j)
          instring[i] = 0x00;
      else (instring[j] = 0x00);
                 
      /* find   begin  */
      for (i=0;i<5;i++)
         { teststr[i] = instring[i]; }
      teststr[i] = 0x00; /* put null at end of string */ 

      start = (strcmp(teststr,beginstr));  /* true if cmp == 0 */

   } while (start != 0);
   
   /* printf ("\n Found 'begin' %s\n",instring); */
          
      fputs (instring,outfile); /*write the 'begin' line */
      
      write = 1; /* flag */       
   
   while (fgets (instring,INCHARS,infile) != NULL)
   {      
      i = j = strlen(instring);
   
      /* check for a quote indicator */
      if ((*instring == *beginc) || (*instring == *beging))
         {for (i=0;i<strlen(instring);i++)
            (instring[i] = instring[i+1]);}
  
      if (*instring == *space) /* maybe a following space */
         { for (i=0;i<strlen(instring);i++)
            (instring[i] = instring[i+1]); }
            
      if (i != j)
          instring[i] = 0x00;
      else (instring[j] = 0x00);
                       
      strcpy (threeback,twoback);
      strcpy (twoback,oneback);
      strcpy (oneback,current);
      strcpy (current,instring);
                     
      /* look for end */
      for (i=0;i<3;i++)
         { testend[i] = instring[i]; }
      testend[i] = 0x00; /* again add null at end of string */
      
      /* look for size */
      for (i=0;i<4;i++)
         { testsize[i] = instring[i]; }
      testsize[i] = 0x00; /* add null to allow string comparison */
               
      end = (strcmp(endstr,testend));
      size = (strcmp(sizestr,testsize));
                
      if (size == 0)
        { fputs (instring,outfile);  /* writes the 'size xxxx' line */ 
          write = 0;
          break;}
      
      if (end == 0)
        { fputs (twoback,outfile); /* this might be a problem with some files, */
                 /* if the last full line of the file is exactly xx characters */
                            /* so that there is no partial line before the  '' */
          fputs (oneback,outfile);
          fputs (instring,outfile); }
          /* fputs the last lines of the uuencoded file */
        
      /* compare then copy the new string to the old string */
      
      if ((strcmp (oldstring,instring) != 0) &&
          (strcmp (twoback,instring) != 0) &&
          (strcmp (threeback,instring) != 0))
        {         
         if ((*instring == *mstring)&&(write == 1)) /* if first char in line is M */
            {fputs (instring,outfile);
               strcpy (oldstring,instring); }            
        }
   }   
   fclose (outfile);
   fclose (infile);
   printf ("\n Done.\n");
}
