/***************************************************************************

   Program:    MailMerge
   File:       MailMerge.c
   
   Version:    V1.0
   Date:       17.01.91
   Function:   Perform mailmerge with LaTeX
   
   Copyright:  SciTech Software 1991
   Author:     Andrew C. R. Martin
   Address:    SciTech Software
               23, Stag Leys,
               Ashtead,
               Surrey,
               KT21 2TD.
   Phone:      +44 (0372) 275775
   EMail:      UUCP: cbmuk!cbmuka!scitec!amartin
               JANET: andrew@uk.ac.ox.biop
               
****************************************************************************

   This program is not in the public domain, but it may be freely copied
   and distributed for no charge providing this header is included.
   The code may be modified as required, but any modifications must be
   documented so that the person responsible can be identified. If someone
   else breaks this code, I don't want to be blamed for code that does not
   work! The code may not be sold commercially without prior permission from
   the author, although it may be given away free with commercial products,
   providing it is made clear that this program is free and that the source
   code is provided with the program.

****************************************************************************

   Description:
   ============
   A simple mail merge program for using with the letter format of LaTeX.
   The program expect 2 parameters, the first is the letter, the second
   is the file of addresses.

****************************************************************************

   Usage:
   ======
   MailMerge [-c<command>] [-d<driver>] <letter.tex> <addresses.lis>
   
   The letter must contain a:
      \begin{letter}{}
   line and the file of addresses should contain the addresses in the form:
      \begin{letter}{23, Stag Leys,\\Ashtead,\\Surrey,\\KT21 2TD.}
   Each line from the file will be inserted into the letter in turn,
   TeX will be run on the file and dviljp will be run.
   The -c and -d flags allow the default TeX command and dviljp command
   respectively to be replaced.

****************************************************************************

   Revision History:
   =================

***************************************************************************/

/* Define the computer/operating system type */
#define AMIGA 1
/* #define VAX 1 */
/* #define UNIX 1 */

/* Switch on debugging mode (print commands rather than execute them */
/* #define DEBUG 1 */

#include <stdio.h>

/* These define the name of the temporary file used for TeX processing.
   If you alter this, both lines must be changed
*/
#define OUTTEX "MergeLetter.tex"
#define TEXFILE "MergeLetter"

/* Define the default command for LaTeX processing */
#ifdef AMIGA
#define DEF_COMMAND "tex"
#else
#define DEF_COMMAND "latex"
#endif

/* Define the default printer driver */
#define DEF_DRIVER "dviljp"

/* Define buffer lengths */
#define BUFFLEN 255
#define SMALLBUFF 80

main(argc,argv)
int argc;
char **argv;
{
   FILE *letter,
        *address,
        *out;
   char buffer[BUFFLEN+1],
        addrline[BUFFLEN+1],
        command[SMALLBUFF],
        driver[SMALLBUFF],
        ch;

   strcpy(command,DEF_COMMAND);
   strcpy(driver,DEF_DRIVER);
   
   if(argc<3)
   {
      Usage();
      exit(0);
   }
   
   /* Process any switches supplied */
   argc -= 3;
   argv++;
   while(argc)
   {
      if((argv[0][1] == 'c')||(argv[0][1] == 'C'))
         strcpy(command,argv[0]+2);
      else if((argv[0][1] == 'd')||(argv[0][1] == 'D'))
         strcpy(driver,argv[0]+2);
      else
      {
         Usage();
         exit(1);
      }
      argc--;
      argv++;
      
   }
   
   /* Open the input files */
   if((letter=fopen(argv[0],"r"))==NULL)
   {
      printf("Unable to open letter file %s\n",argv[0]);
      exit(1);
   }
   argv++;
   if((address=fopen(argv[0],"r"))==NULL)
   {
      printf("Unable to open address file %s\n",argv[0]);
      exit(1);
   }

   /* While there are lines in the address file, copy the letter file
      into the temporary file, replacing the \begin{letter} line.
   */
   ch = getc(address);
   while(!feof(address))
   {
      ungetc(ch,address);
      /* Get a line from the address file */
      memset(addrline,0,BUFFLEN);
      fgets(addrline,BUFFLEN,address);
      /* Break the loop from returns, etc. at the end of the address file */
      if(strlen(addrline)<14) break;
      
      /* Open the output TeX file */
      if((out=fopen(OUTTEX,"w"))==NULL)
      {
         printf("Unable to open temporary TeX file %s\n",OUTTEX);
         exit(1);
      }
   
      /* Copy the tex file into the output file, substituting the \begin{letter}
         line.
      */
      while(!feof(letter))
      {
         /* Get a line from the letter file */
         memset(buffer,0,BUFFLEN);
         fgets(buffer,BUFFLEN,letter);
         
         /* Check if it's the \begin{letter} line */
         if(!strncmp(buffer,"\\begin{letter}",14))
            fputs(addrline,out);
         else
            fputs(buffer,out);

      }
      
      /* Close the output file and rewind the letter file */
      fclose(out);
      rewind(letter);
      
      /* Now TeX the output file */
      memset(buffer,0,BUFFLEN);
      sprintf(buffer,"%s %s",command,TEXFILE);      
#ifdef DEBUG
      puts(buffer);
#else
      system(buffer);
#endif

      /* Now send the file to the printer */
      memset(buffer,0,BUFFLEN);
      sprintf(buffer,"%s %s",driver,TEXFILE);      
#ifdef DEBUG
      puts(buffer);
#else
      system(buffer);
#endif

      ch = getc(address);
   }

   /* Now clean up by removing the temporary TeX files */
#ifdef VAX
   memset(buffer,0,BUFFLEN);
   sprintf(buffer,"DELETE %s.*;*",TEXFILE);      
#ifdef DEBUG
   puts(buffer);
#else
   system(buffer);
#endif
#endif

#ifdef AMIGA
   memset(buffer,0,BUFFLEN);
   sprintf(buffer,"delete %s#?",TEXFILE);      
#ifdef DEBUG
   puts(buffer);
#else
   system(buffer);
#endif
#endif

#ifdef UNIX
   memset(buffer,0,BUFFLEN);
   sprintf(buffer,"rm %s*",TEXFILE);      
#ifdef DEBUG
   puts(buffer);
#else
   system(buffer);
#endif
#endif

   return(0);
}

/***************************************************************************/
Usage()
{
   printf("Usage: MailMerge [-c<command>] [-d<driver>] <letter.tex> <addresses.lis>\n");
   printf("       -c   Set command (default: tex)\n");
   printf("       -d   Set driver  (default: dviljp)\n");
   return(0);
}
   