/* SPOOL.c - queue print file program */

/*
**	Date written: 04/23/86
**	Author: Tim Holloway
**		Compuserve: 73026,2026
**		Bix: tholloway
**		Fido node: 112/1 (Casa Mi Amiga).
**
**	As featured in Ami Project Magazine.
**
**
**	Version: 1.0
**
**	Copyright (C) 1986, by Tim Holloway.  This program may be
**	freely distributed for non-commercial use only.  Use for commercial
**	purposes without the express permission of the author is a violation
**	of copyright law.
**
**	Description:
**	   This program accepts as input one or more file names.
**	These names are passed via a message port to the SPOOLER program,
**	which prints the files.
**
**	Change History: none.
**	Usage: See below.
*/

/* Basic Amiga system definitions */

#include <exec/types.h>
#include <intuition/intuition.h>
#include <libraries/dos.h>

/* more mundane definitions */

#include "stdio.h"
#include "spool.h"

/* define login and logout stuff in the following #include: */
#include "login.cx"


usage()
{
	printf ("SPOOLing program v1.0 by Tim Holloway\n");
	printf ("Usage: SPOOL filename1 [filename2 [filename3...]]\n");
}

SPOOLmsg packet = {{NULL, NULL, 0}, '?', ""};

error (msg)
char *msg;
{
	fprintf (stderr, "%s\n", msg);
}


main (argc, argv)
int argc;
char *argv[];
{
   int i;
   PORTPAIR ports; 

   if ((argc < 2) || (argv[1][1] == '?'))
   {
      usage();
      exit(10);
   }

   if (NOT LogInPort (SPOOL_ME, &ports, &packet))
   {
      error ("SPOOLER program is inactive\n");
      exit(15);
   }

/* printf ("message port for %s is at %lx\n", SPOOL_ME, ports[OUTPORT]); */

   for (i = 1; i < argc; i++)
   {
      if (argv[i][0] == '-')	/* command to SPOOLER */
	strcpy (packet.filename, argv[i]);
      else
      {
	ExpandPath (argv[i], packet.filename);
	if (packet.filename[0] == NUL)
	{
		printf ("ERROR: file \"%s\" could not be found!\n", argv[i]);
		continue;
	}
      }

      packet.minfo.mn_ReplyPort = ports[INPORT];	/* return address */

/* printf ("send/w reply port at %lx\n", packet.minfo.mn_ReplyPort); */

      packet.minfo.mn_Length = strlen(packet.filename)+1;

/* printf ("===Send to spool...\n"); */

      PutMsg (ports[OUTPORT], &packet);
      WaitPort(ports[INPORT]);
      (VOID) GetMsg (ports[INPORT]);
      if (packet.log_status == LOG_OUT) break;	/* ouch! forced off! */
   }
   LogOutPort (&ports, &packet);
}
