/*
* Copyright (C) 2001-2008, CompHEP Collaboration
* Copyright (C) 1997, Alexander Pukhov 
* ------------------------------------------------------
* $Id$
*
* $Log$
*/

#include "service2/include/chep_limits.h"
#include "service2/include/getmem.h"
#include "service2/include/unix_utils.h"
#include "service2/include/tptcmac.h"
#include "service2/include/syst.h"
#include "service2/include/s_files.h"
#include "chep_crt/include/chep_crt.h"

#include "physics.h"
#include "procvar.h"
#include "out_service.h"
#include "saveres.h"
#include "out_form.h"

static void 
writeparameters (int nsub)
{
  int k;
  int first;
  char ch;

  writeF ("\n");
  first = TRUE;
  ch = ' ';
  writeF ("Symbols\n");

  for (k = 1; k <= nmodelvar; k++)
    {
      if (vararr[k].used && !modelvars[k].func)
	{
	  writeF (" %c%s\n", ch, vararr[k].alias);
	  ch = ',';
	}
    }
  writeF (" ;\n");
}


static void 
writefunctions (int nsub)
{
  int k;
  char s[STRSIZ];

  writeF ("\n");
  for (k = 1; k <= nmodelvar; k++)
    {
      if (vararr[k].used && modelvars[k].func)
	{
	  sscanf (modelvars[k].func, "%[^|]", s);
	  trim (s);
	  writeF ("id %s=%s;\n", vararr[k].alias, s);
	}
    }
}


static void 
startForm (FILE * fsrc, int nsub, int *prtclNum, int ncalc)
{
  char fname[STRSIZ];

  outputLanguage = 'F';
  initvararray (fsrc, nsub, outputLanguage);
  sprintf (fname, "%sresults%cusr%d.frm", pathtouser, f_slash, nsub);
  outFileOpen (fname);

  writeF ("\n#-\nCFunction Sqrt;\n#procedure userWork(nnn)\n");

  writefunctions (nsub);

  emitconvlow (prtclNum);

  writeF ("#if 'nnn' == %d\n.end\n#endif\n#endprocedure\n", ncalc);
  outFileClose ();

  sprintf (fname, "%sresults%csum%d.frm", pathtouser, f_slash, nsub);
  outFileOpen (fname);
  writeLabel ('*');
  writeF ("vector p1,p2,p3,p4,p5,p6;\n");
  writeparameters (nsub);
  writeF ("CFunction den;\n");
  writeF ("Symbol factor;\n");
  writeF ("#include usr%d.frm\n", nsub);
}


static void 
diagramForm (FILE * fsrc, vcsect * vcs, catrec * cr)
{
  int i;

  writeF ("\n*Diagrama number %d-%d;\n", cr->nsub_, cr->ndiagr_);

  if (vcs != NULL)
    DiagramToOutFile (vcs, 0, '*');

  fseek (fsrc, cr->factpos, SEEK_SET);
  readvardef (fsrc);
  writeF ("Local FACTOR%d = (", cr->ndiagr_);
  rewritepolynom (fsrc);
  writeF (")/(");
  rewritepolynom (fsrc);
  writeF (");\n");
  clearvardef ();
  writeF ("Local ANS%d = factor*(", cr->ndiagr_);

  fseek (fsrc, cr->rnumpos, SEEK_SET);
  readvardef (fsrc);
  rewritepolynom (fsrc);
  writeF (")");
  clearvardef ();

  fseek (fsrc, cr->denompos, SEEK_SET);
  readDenominators (fsrc);

  for (i = 0; i < denrno; i++)
    {
      char momStr[20];

      momentToString (denom[i].momStr, momStr);
      writeF ("\n *den(%s,%s,%d)", momStr,
	      vararr[denom[i].mass].alias, -denom[i].power);
    }
  writeF (";\n");
  writeF ("\n#call userWork{%d}\n", cr->ndiagr_);
}

static void 
endForm (int *prtclNum)
{
  writeF ("*\n");
  outFileClose ();
}


void 
makeFormOutput (void)
{
  makeOutput (startForm, diagramForm, endForm);
}
