/*
** (c) 1992 Uwe C. Schroeder, Anwendungssysteme , Augsburg, Germany
**
** 
** Permission to use, copy, and modify this software and its
** documentation for non comercial  purpose is hereby granted 
** without fee, provided that
** the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation, and that the name of Uwe C. Schroeder not be used in
** advertising or publicity pertaining to distribution of the software without
** specific, written prior permission.  Uwe Schroeder makes no representations
** about the suitability of this software for any purpose.  It is provided
** "as is" without express or implied warranty.
** This software may not be sold or distributed with commercial products
** ( this includes distribution "for users convenience" ) without prior
** written permission by the author.
**
** UWE SCHROEDER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
** INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
** EVENT SHALL UWE SCHROEDER BE LIABLE FOR ANY SPECIAL, INDIRECT OR
** CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
** DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
** TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
** PERFORMANCE OF THIS SOFTWARE.
**
**
** This is a alpha release, which means, that there are still several
** known bugs.
**
** Please report bugs to:
**
**                     usys@phoenix.abg.sub.org
**
*/


#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>

#include "vfax.h"

int
send_process(fname)
     char *fname;
{
  FILE *g3;
  int exflag = 0;
  Queue qt;
  char qfname[512];
  char cmdbuf[128];
  FILE *f;

  logto(1,"got queue_file %s",fname);
  
  strcpy(qfname,fname);
  strcat(qfname,".qf");
  if((f = fopen(qfname,"r")) == (FILE *)NULL)
    {
      logto(0,"Sorry, couldn't open %s",fname);
      return -1;
    }
  else
    {
      if(parse_queuefile(f,&qt) == 0){
	if(qt.retries < qt.maxtries){
	  logto(1,"Sending file %s to number %s retry %d",
		qt.g3file,qt.phone,qt.retries);
	  if((g3 = fopen(qt.g3file,"r")) == (FILE *)NULL){
	    logto(0,"Sorry, can't open %s for reading",qt.g3file);
	    return -1;
	  }
	  if(init_fax()!=0){
	    logto(0,"Can't sync modem !\n");
	    return -1;
	  }
	  if(modem_dial(qt.phone) == 0){
	    if(send_file(g3)==0){
	      unlink(qt.g3file);
	      mail(&qt,0);
	      return 0;
	    }
	  }
	  qt.retries++;
	  set_retries(qfname,&qt);
	  return -1;
	} else {
	  logto(0,"Not Sending file %s to number %s\n\t 3 retries failed\n",
		qt.g3file,qt.phone);
	  mail(&qt,3);
	  unlink(qt.g3file);
	  unlink(qfname);
	  return -1;
	}
      } else {
	logto(0,"Illegal format of queuefile !");
      }
    }
  return -1;
}


