/********************************************************************************/
/********************************************************************************/
/***                          COM  Version 4.0                                ***/
/***                    An ARexx utility written by                           ***/
/***                           Andreas Jung                                   ***/
/***                         Klosterstrasse 21                                ***/
/***                          D-6602 Dudweiler                                ***/
/***                    Federal Republic Of Germany                           ***/
/***                                                                          ***/
/***   Copyright (c) 1990 Andreas Jung.  This program may be distributed      ***/
/***   on non-commercial base.  This program may not be distibuted on         ***/
/***   commercial base without my permission.                                 ***/
/***   It seems that this programs works well. But no guarantees of any       ***/
/***   kind are given for total reliabilty !                                  ***/
/***   Please send any bug reports to adress above.                           ***/
/***   Warning: This program needs the Arp.Library (version 39.1 or higher)   ***/
/********************************************************************************/
/********************************************************************************/

#include <functions.h>
#include <exec/types.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <string.h>
#include <libraries/arpbase.h>
#include <arp.h>

BPTR fp;
struct ArpBase *ArpBase;

void ComExit(int faultcode);
void _wb_parse(void) {};

void main(int argc,char **argv)
{
	char st[255];
	int i;

	ArpBase=(struct ArpBase *) OpenLibrary("arp.library",0);
	if (ArpBase == NULL) exit(20);

	if (argc == 1)
	{
		Printf("Usage: Com <Hostname> <Argument 1> <Argument 2> ...\n");
		ComExit(5);
	}

	if (argc == 2 && argv[1][0]=='?')
	{
		Printf("Usage: Com <Hostname> <Argument 1> <Argument 2> ...\n");
		Printf("Com (Version 4.0) sends the given arguments to a running REXX-Host\n"); 
		Printf("Written in 1989/90 by Andreas Jung, Klosterstraße 21\n");
		Printf("                      D-6602 Dudweiler (West Germany)\n");
		ComExit(5);
	}
	
	if (argc == 2 && argv[1][0]!='?')
	{
		Printf("Type 'com ?' for help\n");
		ComExit(5);
	}


	fp=Open("ram:rexx-file.rexx",MODE_NEWFILE);
	if (fp==NULL)
	{
		Printf("Can't create new Rexx-File !\n");
		ComExit(5);
	}
	
	FPrintf(fp,"/* Command 2.0 Written by Andreas Jung */\n");
	FPrintf(fp,"options results\n");
	FPrintf(fp,"options failat 5\n");
	FPrintf(fp,"signal on error\n");

	strcpy(st,"address ");
	for (i=1;i<argc;i++)
	{	
		strcat(st," '");
		strcat(st,argv[i]);
		strcat(st,"' ");
	}
	strcat(st,"\n");
	FPrintf(fp,st);

	FPrintf(fp,"erg=result\n");
	FPrintf(fp,"if erg='RESULT' then say 'No results returned'\n");
	FPrintf(fp,"				else say 'Result: ' erg\n");
	FPrintf(fp,"exit\n");
	FPrintf(fp,"ERROR:\n");
	FPrintf(fp,"say 'Error during receiving data, please verify system !'\n");
	FPrintf(fp,"exit 5\n");
	
	Close(fp);

	i=Execute("rx ram:rexx-file.rexx",0,0);
	if (i == NULL)
	{
		Printf("Can't execute REXX-command file !\n");
		ComExit(5);
	}

	i=DeleteFile("ram:rexx-file.rexx");
	if (i == NULL)
	{
		Printf("Can't delete REXX-command file !\n");
		ComExit(5);
	}
	ComExit(0);
}

void ComExit(int faultcode)
{
	if (ArpBase != NULL) CloseLibrary(ArpBase);
	exit(faultcode);
}