/*  Chaos:		    The Chess HAppening Organisation System	V5.1a
    Copyright (C)   1993    Jochen Wiedmann

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


    $RCSfile: OutSK.c,v $
    $Revision: 1.1 $
    $Date: 1993/08/19 14:06:41 $

    This file contains those output-functions that create TeX-source.
    (I didn't find a better solution.)

    Computer:	Amiga 1200		    Compiler:	Dice 2.07.54 (3.0)

    Author:	Jochen Wiedmann
		Am Eisteich 9
	  72555 Metzingen
		Tel. 07123 / 14881
		Internet: wiedmann@mailserv.zdv.uni-tuebingen.de
*/


#ifndef CHAOS_H
#include "chaos.h"
#endif
#ifndef CLIB_DOS_PROTOS_H
#include <clib/dos_protos.h>
#endif
#ifndef __STDIO_H
#include <stdio.h>
#endif

#ifdef AZTEC_C
#ifndef PRAGMAS_DOS_LIB_H
#include <pragmas/dos_lib.h>
#endif
#endif


void OutSpielerKarten(void)

  { struct Teilnehmer *t;
    struct Game *g;
    char *filename;
    FILE *fh;
    int i, j;
    char punktes[20];
    int numrunden;

    if (NumRunden < 11)
      { numrunden = (11 < NumRunden+4) ? NumRunden+4 : 11;
      }
    else
      { numrunden = NumRunden+2;
      }
    if ((numrunden %2) == 0)
      { numrunden++;
      }

    if ((filename = FileRequest(TRUE, "#?.tex",
				(char *) GetChaosString(MSG_TEXFILE_SELECTION)))
		  ==  NULL)
      { return;
      }

    if ((fh = fopen(filename, "w"))  ==  NULL)
      { ShowError((char *) GetChaosString(MSG_NO_WRITE_FILE), filename,
		  IoErr());
	return;
      }

    fprintf(fh, "\\documentstyle[german,a4,12pt]{article}\n");
    fprintf(fh, "\\renewcommand{\\arraystretch}{2.0}\n");
    fprintf(fh, "\\parindent0em\n\n");
    fprintf(fh, "\\begin{document}\n");

    for (t = (struct Teilnehmer *) Teilnehmerliste.lh_Head, i = 0;
	 t->Tn_Node.ln_Succ != NULL;
	 t = (struct Teilnehmer *) t->Tn_Node.ln_Succ)
      { t->Nr = ++i;
      }

    for (t = (struct Teilnehmer *) Teilnehmerliste.lh_Head;
	 t->Tn_Node.ln_Succ != NULL;
	 t = (struct Teilnehmer *) t->Tn_Node.ln_Succ)
      { fprintf(fh, "\\begin{tabular}{|l*{%d}{|p{0.6cm}}|}\\hline\n", numrunden);
	fprintf(fh, "Nr. %d&\\multicolumn{%d}{|l|}{%s}\\\\\\hline\n", t->Nr,
		numrunden, t->Name);

	fprintf(fh, (char *) GetChaosString(MSG_WHITE_OUTPUT));
	for (j = 0, g = t->First_Game;  j < numrunden;  j++)
	  { if (g != NULL)
	      { if (g->Flags & GMFLAGSF_WEISS)
		  { fprintf(fh, "&%d", g->Gegner->Nr);
		  }
		else
		  { fprintf(fh, (g->Flags & GMFLAGSF_FREILOS) ? "&F" : "&");
		  }
		g = g->Next;
	      }
	    else
	      { fprintf(fh, "&");
	      }
	  }
	fprintf(fh, "\\\\\\hline\n");

	fprintf(fh, (char *) GetChaosString(MSG_BLACK_OUTPUT));
	for (j = 0, g = t->First_Game;  j < numrunden;  j++)
	  { if (g != NULL)
	      { if ((g->Flags & (GMFLAGSF_WEISS|GMFLAGSF_FREILOS))  ==  0)
		  { fprintf(fh, "&%d", g->Gegner->Nr);
		  }
		else
		  { fprintf(fh, (g->Flags & GMFLAGSF_FREILOS) ? "&F" : "&");
		  }
		g = g->Next;
	      }
	    else
	      { fprintf(fh,"&");
	      }
	  }
	fprintf(fh, "\\\\\\hline\n");

	fprintf(fh, (char *) GetChaosString(MSG_POINTS_OUTPUT));
	for (j = 0, i = 0, g = t->First_Game;  j < numrunden;  j++)
	  { if (g != NULL)
	      { i += g->Ergebnis;
		g = g->Next;
		punktetoa(punktes, (long) i);
		fprintf (fh, "&%s", punktes);
	      }
	    else
	      { fprintf(fh, "&");
	      }
	  }
	fprintf(fh, "\\\\\\hline\n");

	fprintf(fh, "\\end{tabular}\n\\vspace{0.5cm}\n\n");
      }
    fprintf(fh, "\\end{document}\n");
    fclose(fh);
  }


void OutKreuztabelle(void)

  { char *filename;
    FILE *fh;
    struct Teilnehmer *t;
    struct Game *g;
    int i, j;
    char punktes[20];

    if ((filename = FileRequest(TRUE, "#?.tex",
				(char *) GetChaosString(MSG_TEXFILE_SELECTION)))
		  ==  NULL)
      { return;
      }

    if ((fh = fopen(filename, "w"))  ==  NULL)
      { ShowError((char *) GetChaosString(MSG_NO_WRITE_FILE), filename,
		  IoErr());
	return;
      }

    fprintf(fh, "\\documentstyle[german,a4");
    if (NumTeilnehmer < 25)
      { fprintf(fh, ",12pt");
      }
    fprintf(fh, "]{article}\n");
    fprintf(fh, "\\newcommand{\\r}{$\\frac{1}{2}$}\n");
    fprintf(fh, "\\renewcommand{\\arraystretch}{2.0}\n");
    fprintf(fh, "\\parindent0em\n\n");
    fprintf(fh, "\\begin{document}\n");
    fprintf(fh, "  \\begin{tabular}{l|*{%d}{p{0.6cm}|}r}\n", NumTeilnehmer);

    fprintf(fh,"    ");
    for (i = 0;  i < NumTeilnehmer;  i++)
      { fprintf(fh, "& %d ", i+1);
      }
    fprintf(fh, "& \\\\\\hline\n");

    for (i = 1;  i <= NumTeilnehmer;  i++)
      { for(t = (struct Teilnehmer *) Teilnehmerliste.lh_Head;
	    t->Nr != i;
	    t = (struct Teilnehmer *) t->Tn_Node.ln_Succ)
	  {
	  }

	fprintf(fh, "    %s", t->Name);
	for (j = 1;  j <= NumTeilnehmer;  j++)
	  { if (j == i)
	      { fprintf(fh, "& x ");
	      }
	    else
	      { g = t->First_Game;
		while(g->Gegner == NULL  ||  g->Gegner->Nr != j)
		  { g = g->Next;
		  }
		switch (g->Ergebnis)
		  { case 2:
		      fprintf(fh, "& 1 ");
		      break;
		    case 1:
		      fprintf(fh, "& \\r");
		      break;
		    case 0:
		      fprintf(fh, "& 0 ");
		      break;
		    default:
		      fprintf(fh, "&   ");
		  }
	      }
	  }
	punktetoa(punktes, (long) t->Punkte);
	fprintf(fh, "& %s", punktes);
	if (i < NumTeilnehmer)
	  { fprintf(fh, "\\\\\\hline");
	  }
	fprintf(fh, "\n");
      }

    fprintf(fh, "  \\end{tabular}\n");
    fprintf(fh, "\\end{document}\n");
    fclose(fh);
  }
