/*
 *	Name:				sendit.c
 *
 * Description:	
 *		This program demonstrates how to "act like a mailer" for the purpose
 *		of sending files to remote sites.
 *
 * Copyright:		1993 by David Jones.
 *
 * Distribution:
 *		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		David Jones
 *				675 Mass Ave							6730 Tooney Drive
 *				Cambridge, MA							Orleans, Ontario
 *				02139										K1C 6R4
 *				USA										Canada
 *
 *	Usenet:	gnu@prep.ai.mit.edu					dej@qpoint.ocunix.on.ca
 *	Fidonet:												1:163/109.8
 *
 *		$Log: $
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include "xferq.h"
#include "xferq_pragmas.h"


struct Library *XferqBase;

char *WorkStates[] = {
	"NO_WORK",
	"LOCKED_WORK",
	"UNLOCKED_WORK",
	"PENDING_WORK"
};


int main(int argc, char *argv[])
{
struct NetAddress *addr=NULL;
int state;
char *name, *asname;
long pri, flags, status;
int choice;
struct WorkNode *wn;

	if (!(XferqBase = OpenLibrary("xferq.library", 1l))) exit(100);
	
        if(argc>1) addr = XfqGetAddressTags(argv[1], NULL,
			XQ_Mandatory, XQADDR_3D | XQADDR_MACHINE,
			XQ_Optional, XQADDR_FIDO | XQADDR_MACHINE,
			TAG_DONE);
	if (!addr) {
		printf("Error parsing address on command line.\n");
		CloseLibrary(XferqBase);
		exit(10);
	}	
	while (TRUE) {
		state = XfqAnyWorkTags(addr,TAG_DONE);
		if (state<4) printf("There is currently %s.\n", WorkStates[state]);
		else printf("There is currently Unknown.\n");
		if (state == NO_WORK) {
			break;
		}
		if (state == PENDING_WORK || state == LOCKED_WORK) {
			Delay(100);
			continue;
		}
		
		wn = XfqGetWorkTags(addr,TAG_DONE);
		if (wn) {
			XfqExamObjectTags(wn,
				XQ_Name, &name,
				XQ_AsName, &asname,
				XQ_Pri, &pri,
				XQ_Status, &status,
				XQ_Flags, &flags,
				TAG_DONE);
			printf("\nCurrent item:\n");
			printf("  name = '%s'\n", name);
			if (asname) {
				printf("  asname = '%s'\n", asname);
			}
			printf("  priority = %d\n", pri);
			printf("  flags = %d\n", flags);
			printf("  status = %d\n\n", status);
			printf("Options:\n1. Send it successfully.\n");
			printf("2. Don't send it.\n\n");
			do {
				choice = 0;
				printf("Choice: ");
				fflush(stdout);
				scanf("%d", &choice);
			} while (choice < 1 || choice > 2);
			if (choice == 1) {
				wn = XfqModifyObjectTags(wn,
					XQ_Status, XQ_SENT,
					TAG_DONE);
				XfqRemoveWork(wn);
				XfqDropObject(wn);
			}
			else {
				wn = XfqModifyObjectTags(wn,
					XQ_Status, XQ_UNSENT,
					TAG_DONE);
				XfqUnlockWork(wn);
			}
		}
		else {
			printf("There is no more work that can be sent at this time.\n");
		}
	}
	XfqDropObject(addr);
	CloseLibrary(XferqBase);
}
