#include <stdlib.h>

/*#include <mrt.h>*/
#include <irrauth.h>

/*JW take out
#include <irrauth.h>
#include "irr_check.h"
#include "irr_notify.h"
#include "hdr_comm.h"
*/

FILE *dfile;
extern trace_t *default_trace;
extern config_info_t ci;

void call_pipeline (trace_t *tr, FILE *fin, 
		    int null_notifications, int dump_stdout) {
  char outfn[256], outfn2[256], logfn[256];
  int ret_code, ack_lock = -1, lock_fp = NULL;
  char pid_string[20];
  FILE *trans_fp = NULL;

  sprintf (pid_string, "PID%d", (int) getpid ());
  strcat (pid_string, "\n");

  /* open the transaction log  */
  if (ci.log_dir != NULL) {
    sprintf (logfn, "%s/trans.log", ci.log_dir);
    if ((ack_lock = lock_file (&lock_fp, logfn)) != 0)
      trace (NORM, default_trace, "Error opening %s: %s\n",
	     logfn, strerror(errno));
    if ((trans_fp = fopen (logfn, "a")) != NULL) {
      fputs (TRANS_SEPERATOR, trans_fp);
      fputs (pid_string, trans_fp);
    }
    else
      trace (NORM, default_trace, "Could not open transaction log: %s\n", 
	     logfn);
  }
  else
    trace (TR_TRACE, default_trace, 
	   "No object submission log directory configured!\n");

  /* Decode PGP stuff, and add any auth cookies necessary. PGP needs to
     come first, because it's sensitive to any changes in the file. */
  trace (TR_TRACE, tr, "Calling pgpdecodefile ()...\n");
  if (pgpdecodefile (fin, outfn, trans_fp, ci.pgp_dir, tr))
    exit(0);
  trace (TR_TRACE, tr, "PGP output filename is %s\n", outfn);
  fclose (fin);

  if (trans_fp != NULL)
    fclose (trans_fp);

  if (ack_lock == 0)
    unlock_file (lock_fp);
  else if (lock_fp != NULL)/*JW ? if (ack_lock != 1)*/
    close (lock_fp);

  /* Open the ack log.  We need to lock the next section as the DB is
   * queried and updated.
   */
  if (ci.log_dir != NULL) {
    sprintf (logfn, "%s/ack.log", ci.log_dir);
    if ((ack_lock = lock_file (&lock_fp, logfn)) != 0)
      trace (NORM, tr, "Error opening %s: %s\n", 
	     logfn, strerror(errno));
    if ((trans_fp = fopen (logfn, "a")) != NULL) {
      fputs (TRANS_SEPERATOR, trans_fp);
      fputs (pid_string, trans_fp);
    }
  }


  /* if (verbose) fprintf (tmp, "calling addmailcookies ()...\n");*/
  /* check mail headers, adding cookies while removing the mail headers
   * from the stream; also preserve any PGP cookies already existing.
   * 'ret_code' is 0..n, ie, the number of non-null lines read.  This way
   * we can detect empty submissions.
   */
  if ((ret_code = addmailcookies(tr, dump_stdout, outfn, outfn2)) < 0)	exit(0);
  trace (TR_TRACE, tr, "ADDMAILCOOKIES() output filename is %s\n", 
	 outfn2);
  unlink(outfn);

  if (ret_code > 0) {
    /* syntax checking: irr_check */
    dfile = stderr;
    dfile = fopen ("/dev/null", "w");
    trace (TR_TRACE, tr, "Calling irr_check ()...\n");    
    if (callsyntaxchk (tr, outfn2, outfn, (char *) tmpfntmpl) < 0) {
      trace (NORM, tr, "** FAILED IRRCHECK **, problem opening up files\n");
    }
    trace (TR_TRACE, tr, "irr_check output filename is %s\n", outfn);
    unlink (outfn2);

    /* auth checking: irr_auth */
    dfile = stdout;
    trace (TR_TRACE, tr, "Calling irr_auth()...\n");
    if (auth_check (tr, outfn, outfn2, ci.irrd_host, 
		    ci.irrd_port, ci.super_passwd) < 0) {
      trace (NORM, tr, "** FAILED AUTHCHECK **\n");
    }
    trace (TR_TRACE, tr, "Auth check output filename is %s\n", 
	   outfn2);
    unlink (outfn);
  }

  
  /* notification: irr_notify */
  trace (TR_TRACE, tr, "Calling irr_notify()...\n"); 
  dfile = stderr;
  callnotify (tr, outfn2, ret_code, null_notifications, dump_stdout,
	      ci.irrd_host, ci.irrd_port, ci.db_admin, trans_fp);
  unlink (outfn2);
  trace (TR_TRACE, tr, "All done!\n");

  if (trans_fp != NULL)
    fclose (trans_fp);

  /* See if we need to roll the logs */
  if (ack_lock == 0) {
    log_roll ("%s/ack.log", ci.log_dir, 1);
    unlock_file (lock_fp);
  } /* lock file opened but a lock error occured; see lock_file () */
  else if (ack_lock != 1) 
    close (lock_fp);
  
  log_roll ("%s/trans.log", ci.log_dir, 0);

}
