
/* Append.c     Append files better than join.
 *
 * V1.0A - Jul-10-88 - Steven A. McClain
 *
 */

/*   #define DEBUG 1  /**/

   #include <stdio.h>

   #define TRUE 1
   #define FALSE 0

   FILE *base_dest;
   FILE *append_file;

   int not_quiet = TRUE;
   int search = FALSE;
   char *search_keyword;
   char *base_name= 0;

main(argc,argv)
  int argc;  char*argv[];
{
	int arg= 1;

	if (argc == 0)
		exit(0);

	if (argc == 1 || *argv[1] == '?' )
	{
		printf("\
\n\
Append <base_file> [-q] [-s[x] <keyword>] <append_file> ... [-t <text> ...]\n\n");
		exit(0);
	}

	while (arg <argc)
	{
		char *ptr= argv[arg];

		#ifdef DEBUG
			printf(".");
		#endif

		if (*ptr == '-')
		{
			#ifdef DEBUG
				printf("Got a '-'\n");
			#endif

			switch( toupper(*++ptr) )
			{
				case 'Q':
				{
				   not_quiet= FALSE;
				}
				break;

				case 'S':
				{
				   printf("Can't search yet!!");
				}
				break;

				case 'T':
				{
					if (!base_name)
					{
					    printf("Must have Base File!");
						exit(10);
					}
					if (!base_dest)
					{
						if ( !(base_dest = fopen(base_name, "a")) )
						{
						    printf("Error Can't open base file %s", base_dest);
							exit(10);
						}
					}
					while (++arg < argc)
					{
						#ifdef DEBUG
							printf("+");
						#endif
						fputs(argv[arg], base_dest);
						if (arg < argc-1)
							fputs(" ", base_dest);
					}
					fputs("\n", base_dest);
				}
				break;
			}
		}
		else if (*ptr)   /* Should be filename */
		{
			#ifdef DEBUG
				printf("Got a filename\n");
			#endif
			if (!base_name)
			   base_name= ptr;
			else
			{
				static char buffer[1024];

				if (!base_dest)
				{
					if ( !(base_dest = fopen(base_name, "a")) )
					{
					    printf("Error Can't open base file %s", base_dest);
						exit(10);
					}
				}

				if ( !(append_file = fopen(ptr, "r")) )
				{
				    printf("Error Can't open base file %s", ptr);
					exit(10);
				}

				/* Time to append the file */
				while (fgets(buffer, 1024, append_file))
				{
					#ifdef DEBUG
						printf(":");
					#endif
					fputs(buffer, base_dest); 
				}
				fclose(append_file);
			}
		}
		++arg;
	}
	if (base_dest)
		fclose(base_dest);
}
/* Thats it */
