
/**********
 *
 * uujoin - join all related uuencoded files for decoding. All related files
 *          must start with the same filename ending with .1, .2, .3, .n.
 *
 *          Usage : uujoin filename (no extension)
 *
 *          It will search for consecutively numbered files and join them to
 *          filename argv[1].uuj.
 *
 *
 * Related Files                    Relationship
 * -------------------------------------------------------------------------
 *
 * -------------------------------------------------------------------------
 *
 * File History:
 *
 * Date        Author               Version     Comments
 * -------------------------------------------------------------------------
 * 10-Jun-89   Keith Elbertson         1.0P     Original Version
 * -------------------------------------------------------------------------
 *
 * Known Bug List:
 * -------------------------------------------------------------------------
 *
 * -------------------------------------------------------------------------
 *
 * Copyright (c) 1989 Keith Elbertson, All Rights Reserved.
 *
 **********/


#include <stdio.h>

#define MAXPATH 128
#define STRING_BUFFER_SIZE 512

void
main(argc, argv)
int argc;
char **argv;
{
   FILE *infile, *outfile;
   char outfilename[MAXPATH] = "";          /* room for filename and path */
   char infilename[MAXPATH]  = "";
   char stringbuffer[STRING_BUFFER_SIZE] = "";
   int  extnum = 1;                    /* extension number */


   puts("Copyright 1989, Keith Elbertson, All Rights Reserved.");

   if ( argc != 2) {

      puts("Usage: UUJOIN <filename> /* no extension */");
      exit();
   }

   strcat (outfilename, argv[1]);      /* copy path and name to buffer */
   strcat (outfilename, ".UUJ");       /* append .UUJ to it. */

   printf("Sending output to %s\n", outfilename);
   fflush(stdout);

   if (outfile = fopen(outfilename, "w")){

      while( found_input( infilename, argv[1], extnum++)){

         register int found_BEGIN = 0;

         printf("Working on %s\n" ,infilename);
         fflush(stdout);

         if ( infile = fopen( infilename, "r")){

            while ( fgets ( stringbuffer, STRING_BUFFER_SIZE, infile )){

               if (found_BEGIN) {

                  if(!(strncmp(stringbuffer, "END", 3))) break;
                  else fputs( stringbuffer, outfile);
               }
               if(!(strncmp(stringbuffer, "BEGIN" , 5))) found_BEGIN++;
            }
            if( ! found_BEGIN ){

               printf("Didn't find BEGIN in %s\n" , infilename);
            }
            fclose(infile);
         }
      }
      fclose(outfile);
   }
}

int
found_input(inname,outname,ext)
char *inname, *outname;
int ext;
{
   sprintf(inname, "%s.%d", outname, ext);

   return ( ! (access(inname, 4))); /* access returns 0 if accessable */
}
