/* fx. Send a command to SYSOP via AREXX and store the resulting output
   into a file.
*/
#include "rexxhostbase.h"
#include <stdio.h>

struct Library *RexxSysBase;
struct RexxHostBase  *RexxHostBase;

struct RexxHost      *rexx_host;
long rexxbit = 0L;
char *portcmd = "GO VE5VA-FX";
/* Try to open the rexxhost.library. */
open_rexx()
{
   if((RexxSysBase = OpenLibrary((UBYTE *)RXSNAME, 0L)) == NULL) {
      printf("couldn't open rexxsupport.library.\n");
      return(1);
   }
      /* set up a public port for rexx to talk to us later */
   if(!(rexx_host = CreateRexxHost((STRPTR)&portcmd[3])))   {
      printf("Can't set up public rexx port for port %s\n",&portcmd[3]);
      CloseLibrary((struct Library *)RexxHostBase);
      return(1);
   }
   rexxbit = (1L << rexx_host->rh_Port.mp_SigBit);
   return(0);
}
close_rexx()
{
   struct RexxMsg *rexxmessage;  /* incoming rexx messages */
   STRPTR Arg;

/* Drain any messages for us */
   while(rexxmessage = GetRexxMsg(rexx_host,0L)) {
      if(Arg = GetRexxCommand(rexxmessage)) {
         ReplyRexxCommand(rexxmessage,0L,0L,0L);
      }
      else {
         FreeRexxCommand(rexxmessage);
      }
   }
   if(rexx_host)
      rexx_host = DeleteRexxHost(rexx_host);
   if(RexxSysBase)CloseLibrary((struct Library *)RexxSysBase) ;
   RexxSysBase = NULL ;

}
char cmdstr[100];
char *sysop_port = "CBBS-1";
FILE *fdo;
main(argc,argv)
int argc;
char *argv[];
{
   struct RexxMsg *rexxmessage;  /* incoming rexx messages */
   STRPTR Arg;
   char c;

   while(argv[1][0] == '-') {
      c = tolower(argv[1][1]);
      switch(c) {
      case 'p':
         c = argv[1][2];
         if(!isdigit(c) || (c == '0')) {
            printf("SYSOP Port must be a non-zero digit\n");
            exit(1);
         }
         sysop_port[5] = c;
         break;
      }
      argc--;
      argv++;
   }
   if(argc < 3) {
      printf("Missing argument(s). Send a message to SYSOP via AREXX\n");
      printf("fx [-pD] filename C-BBS command\n");
      exit(10);
   }
   if(!FindPort((UBYTE *)sysop_port)) {
      printf("Can't find SYSOP Port %s\n",sysop_port);
      exit(19);
   }
   if(open_rexx()) {
      printf("Can't open rexxhostlib\n");
      exit(10);
   }
   if((fdo = fopen(argv[1],"w")) == NULL) {
      printf("Can't open %s\n",argv[1]);
      exit(10);
   }
   if(SendRexxMsg((STRPTR)sysop_port,0L,(STRPTR)portcmd,0L)) {
      printf("SendRexxMsg - GO failed");
      fclose(fdo);
      exit(10);
   }
   /* Remove the first SYSOP> prompt */
   Wait(rexxbit);
   rexxmessage = GetRexxMsg(rexx_host,0L);
   ReplyRexxCommand(rexxmessage,0L,0L,0L);
   /* Now put together the command and send it to SYSOP */
   argc--;
   argv++;
   while(1) {
      strcat(cmdstr,argv[1]);
      argc--;
      argv++;
      if(argc == 0)break;
      strcat(cmdstr," ");
   }
   if(SendRexxMsg((STRPTR)sysop_port,0L,(STRPTR)cmdstr,0L)) {
      printf("SendRexxMsg - command failed");
      fclose(fdo);
      exit(10);
   }
   while(1) {
      Wait(rexxbit);
      while(rexxmessage = GetRexxMsg(rexx_host,0L)) {
         if(Arg = GetRexxCommand(rexxmessage)) {
            if(!strncmp((char *)Arg,"SYSOP>",6)) {
               ReplyRexxCommand(rexxmessage,0L,0L,0L);
               goto done;
            }
            fprintf(fdo,"%s\n",Arg);
            ReplyRexxCommand(rexxmessage,0L,0L,0L);
         }
         else {
            FreeRexxCommand(rexxmessage);
         }
      }
   }
done:
   fclose(fdo);
   SendRexxMsg((STRPTR)sysop_port,0L,(STRPTR)"b",0L);
   close_rexx();
}
