/* db_tex.c - main-Routinen f�r dbtex; UKE/EDV, Bernd Paradies, 05.11.90 */

/* db2tex.c: db2tex.h  db_tex.c */

#include "db2tex.h"

/* -- importierte Variablen */

IMPORT  char   SpTab[MAXSPALTEN][50];
IMPORT  int    SpLen[MAXSPALTEN], SpNr;

IMPORT  char  *prgname;

IMPORT  int    TxtZeilen, GetRepZeilen, RepZeilen, DefZeilen, InpZeilen;
IMPORT  int    MaxReport, OptQuiet, OptOutput, OptCompile;

IMPORT  BOOL   BeginDocument, EndDocument;
IMPORT  FILE  *InpFile, *DefFile, *ShFile, *stdlog;

/* -- Funktions-Prototypen */

PUBLIC  int    PutTexRep ();        /* wird von GetReport ausgef�hrt */
PRIVATE int    GetTex();            /* bearbeitet die TeX-Datei */
PUBLIC  void   do_tex();            /* main f�r dbtex */

/* ---------------------------------------------------------------------- */

PRIVATE int   GetTex(Nr,Zeile)
       int   Nr;
       char *Zeile;
{
char  Spalte[MAXZEILE];
char  TmpZeile[MAXZEILE];                /* Zeile im printf-Format  */
char *Zptr;                              /* wandert durch Zeile        */
char *Tptr;                              /* zeigt auf den letzten Text */

   if (Nr == 1)                          /* SQL-Zeile auswerten */
     return (GetSQL (Zeile));

   if (strncmp (ENDDOC, Zeile, strlen(ENDDOC)) == 0)
     EndDocument = TRUE;

   if (EndDocument) return (0);          /* alles ab EndDoc. ignorieren */

   if (!BeginDocument)                   /* noch im Definitions-Bereich */
   {
     if (strncmp (BEGINDOC, Zeile, strlen(BEGINDOC)) == 0)
       BeginDocument = TRUE;
     else
     {
       fprintf (DefFile, "%s", Zeile);
       DefZeilen++;
     }
     return (0);
   }

   strcpy (TmpZeile, "");                 /* Initialisierung */
   Zptr = Tptr = Zeile;

   while ( (Zptr=strchr(Zptr, '%')) != NULL)     /* schnelle Vorpr�fung */
     if ( *(++Zptr) == '$' )
     {
       strncat (TmpZeile, Tptr, Zptr - Tptr - 1);     /* kopiere bis % */
       sscanf (++Zptr, "%[^, ]", Spalte);             /* hole Spaltenname */

       if ( ChkName(Spalte) == NULL )
         warning ("Ung�ltiger Spaltenname ignoriert. Zeile", Nr);
       else
       {
         strcat  (TmpZeile, TEXMAC);                  /* Backslash */
         strcat  (TmpZeile, strtoupper(Spalte));      /* alles gro� */
         Zptr += strlen (Spalte);                 /* Spalte �berspringen */
       }
       Tptr = Zptr;                                  /* neuer Textbeginn */
     }

   strcat (TmpZeile, Tptr);                /* kopiere den Rest der Zeile */
   fprintf (InpFile, "%s", TmpZeile);
   InpZeilen++;
#ifdef __TEST__
   printf ("IN : %s",   Zeile);
   printf ("  >: %s\n", TmpZeile);
#endif __TEST__
}

/* ---------------------------------------------------------------------- */

PUBLIC int   PutTexRep(Nr, Zeile)
      int   Nr;
      char *Zeile;
{
char   *Sptr;
REG     ii;

 fprintf (DefFile, TEXLINE);
 fprintf (DefFile, "%% --- Exemplar Nr. %i ---\n", Nr-2);
 fprintf (DefFile, "\\newpage \\setcounter{page}{1}\n");
 Zeile[strlen(Zeile)-1] = 0;
 Sptr = Zeile;
 for (ii=0; ii != SpNr; ii++)
 {
   *(Sptr + SpLen[ii]) = 0;
   fprintf (DefFile, "\\def\\%s{%s \\ }\n", SpTab[ii], delblank(Sptr) );
   Sptr += SpLen[ii] + 3;
 }
 fprintf (DefFile, "\\input {%s}\n", InpName);
 DefZeilen += SpNr + 4;
}

/* ---------------------------------------------------------------------- */

PUBLIC int do_tex(void)
{
char  Command[100];
char  Temp[30];

 DefFile   = _fopen (DefName, "w");
 InpFile   = _fopen (InpName, "w");

 fprintf (DefFile, "%% --- Definitions-Sektion von %s ---\n", TxtName );
 fprintf (DefFile, TEXLINE);
 TxtZeilen = WalkFile(TxtName, GetTex);

 fprintf (DefFile, "%s\n", BEGINDOC);
 RepZeilen = WalkFile(RepName, GetReport);

 if (!EndDocument)
   error ("syntax error in LaTeX file: missing \\end{document}");

 fprintf (DefFile, TEXLINE);
 fprintf (DefFile, "%s\n", ENDDOC);

 fclose (InpFile);
 fclose (DefFile);

 if (OptCompile)             /* RETURN !!! bei -c Option */
   return (0);


 if (!OptQuiet)   printf ("TeX wird gestartet...\n");

 /* -- 1) deutsche Umlaute in dbtex.def f�r LaTeX */
 sprintf (Command, "%s < %s > out.tex", UMLT2TEX, DefName );
 if (system(Command) != 0)  error ("umlt2tex liefert Fehler zur�ck !");

 /* -- 2) deutsche Umlaute in dbtex.inp f�r LaTeX */
 sprintf (Command, "mv %s %s.tmp; %s < %s.tmp > %s",
          InpName, prgname, UMLT2TEX, prgname, InpName);
 if (system(Command) != 0)  error ("umlt2tex liefert Fehler zur�ck !");

 /* -- 3) TeX wird aufgerufen */
 sprintf (Command, "%s out.tex > /dev/null", VIRTEX );
 if (system(Command) != 0)  error ("virtex liefert Fehler zur�ck !");

 /* -- 4) Aufr�umarbeiten */
 sprintf (Command, "rm out.aux out.log out.tex %s.tmp; mv out.dvi %s",
                   prgname, DviName);
 system (Command);

 /* -- 5) Ausgabe (quiet, Dateiname, Drucker oder Bildschirm) */

 if (OptQuiet || OptOutput)
 {
   if (!OptOutput)
     sprintf (Command, "%s %s | lp -d%s", DVITPS, DviName, PRINTER );
   else
     sprintf (Command, "%s %s > %s", DVITTY, DviName, OutName);
   return (system (Command));      /* RETURN !!! */
 }

 printf ("Zwischendateien erfolgreich angelegt !\n");

 while (TRUE)        /* Endlos-Schleife terminiert bei 'e' */
 {
   printf ("\nEnde oder Ausgabe auf den Drucker oder Bildschirm ? [e/d/b]: ");
   switch ( getchar() )
   {
     case 'd' :
          sprintf (Command, "%s %s | lp -d%s", DVITPS, DviName, PRINTER );
          printf ("%s\n", Command);
          system (Command);
          break;
     case 'b' :
          sprintf (Command, "%s %s", DVITTY, DviName );
          printf ("%s\n", Command);
          system (Command);
          break;
     case 'e' : return (0);         /* RETURN !!! */
   }
   getchar();  /* wg. return */
 }
} /* ENDE: do_tex() */

/* ENDE: db_tex.c */