#include <exec/exec.h>
#include <dos/dos.h>
#include <stdio.h>
#include <stdlib.h>
#include "work:romconf/doorheader.h"

#define AWAIT_LOGON	0
#define SYSOP_LOGON	1
#define LOCAL_LOGON	2
#define REMOTE_LOGON	3
#define GOODBYE_LOGON	4
#define SUSPEND_LOGON	5
int Whence_The_Logon;
void EnterShell(void);
int mynode;
char temp[200];
#define gu getuserstring
#define sm sendmessage
#define pm prompt
void CloseAux(BPTR tfh);
BPTR OpenAux(void);
void end(void);
void LastCommand(void);
void sr(char *s);
main(int argc,char *argv[])
{
   register int i;
   if(argc!=2)
   {
      printf("AUXSHELL version 1.0 written by Joseph Hodge\n");
      printf("\n");
      exit(0);
   }
   Register(argv[1][0]-'0');
   Whence_The_Logon=GetInfo(BB_LOGONTYPE);
   mynode=argv[1][0]-'0';
   EnterShell();
   ShutDown();
   end();
}
void EnterShell(void)
{
 BPTR fh;

 if(fh=OpenAux()) {
 	//sm("[0;0H[J",0);
	Execute("execute s:REMOTE-startup",fh,0);
	CloseAux(fh);
 }
}


BPTR OpenAux(void)
{
 char str[100];
 BPTR tfh;

 if(Whence_The_Logon==REMOTE_LOGON) {
	    sprintf(str,"AUX%u:",mynode);
	} else
	    sprintf(str,"CNN:S*/0/1/%d/%d/LocalAux",GetInfo(BB_SCRWIDTH),GetInfo(BB_SCRHEIGHT)-1);

 if(!(tfh=Open(str,MODE_OLDFILE))) {
	    sm("",1);sm("The ",0);
	    sm(str,0);
	    sm(" handler can't be opened!",1);
	}
 return(tfh);
}

void CloseAux(BPTR tfh)
{
 Close(tfh);
}

void end(void)
{
  exit(0);
}
void LastCommand(void)
{
  sm("",1);
}
void sr(char *s)
{
  register int i;
  i=strlen(s)-1;
  while(i>-1)
  {
    if(*(s+i)<=32) *(s+i)='\0'; else break;
    i--;
  }
}