#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/lowlevel.h>
#include <exec/memory.h>
#include <devices/serial.h>
#include <exec/io.h>

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>

/* En msg struktur som vi får fra/gir til MAX */
struct DoorMsg {
  struct   Message Door_Msg;
  short    command;
  short    data;
  char     string[80];
  short    carrier;
};

struct DoorMsg *doormsg,*garbage;
struct MsgPort *MyPort,*replyport;

struct Task *mytask; /* Pointer to this task so we can name it */

int err;
BOOL hetwittedus=FALSE;

/*
 * end() -  Notifies Paragon that we are done and ready to continue with
 *		the BBS.  ALWAYS use this to exit from your program, or
 *		the BBS will go to never-never land.
 */

void end(void)
{
	doormsg->command = 20;
	PutMsg(MyPort,(struct Message *)doormsg);
	WaitPort(replyport);
	GetMsg(replyport);
	FreeMem(doormsg,(long)sizeof(struct DoorMsg));
	DeletePort(replyport);
	exit(0);
}

/*
 * getsvar(typ,mstring) - Gets certain string variables from Paragon.
 * "mstring" is a pointer to a string to dump the string into.  "typ" tells
 * it what you want: 1=Name, 2=Password, 3=Address, 4=City, 5=State
 * 6=Postal code, 7=Door pathname, 8=Default BBS pathname.
 */

void getsvar(int typ,char *mstring)
{
	doormsg->data = typ;
	doormsg->command = 14;
	PutMsg(MyPort,(struct Message *)doormsg);
	WaitPort(replyport);
	strcpy(mstring,doormsg->string);
	GetMsg(replyport);
  if(doormsg->carrier) hetwittedus=TRUE;
}

/*
 * getivar(typ) - Gets certain integer variables from Paragon.
 * typ can be: 1 access level, 2 expert mode, 3 net credits, 4 number of calls,
 * 5 calls to system, 6 graphics mode, 7 minutes remaining, 8 screen width,
 * 9 screen height
 */

int getivar(int typ)
{
int i;

	doormsg->data = typ;
	doormsg->command = 13;
	PutMsg(MyPort,(struct Message *)doormsg);
	WaitPort(replyport);
  i=doormsg->data;
	GetMsg(replyport);
  if(doormsg->carrier) hetwittedus=TRUE;
  return(i);
}

/*
 * sendmessage(mstring,nl) - Sends a message to the local window and to
 * the modem (if applicable).  "mstring" is the output string you want to
 * send, "nl" is and integer which is 1 if you want it to send the
 * c/r+l/f combination, or 0 if not.
 */

void sendmessage(char *mstring,int nl)
{
	doormsg->data = nl;
	doormsg->command = 1;
	strcpy(doormsg->string,mstring);
	PutMsg(MyPort,(struct Message *)doormsg);
	WaitPort(replyport);
	GetMsg(replyport);
  if(doormsg->carrier) hetwittedus=TRUE;
}

void msg(char *mstring)
{
  if(strlen(mstring)<80)
  {
  	doormsg->data = 0;
  	doormsg->command = 1;
  	strcpy(doormsg->string,mstring);
  	PutMsg(MyPort,(struct Message *)doormsg);
  	WaitPort(replyport);
  	GetMsg(replyport);
    if(doormsg->carrier) hetwittedus=TRUE;
  }
}

/*
 * hotkey(mstring,ostring) - outputs the string "mstring" and waits
 * for one key which it will place in element [0] of "ostring".
 * "mstring" may be a null string, in which case it will only wait for
 * the key, and not output any prompt.
 */

void hotkey(char *mstring,char *ostring)
{
	strcpy(doormsg->string,mstring);
	doormsg->command = 8;
	PutMsg(MyPort,(struct Message *)doormsg);
	WaitPort(replyport);
	GetMsg(replyport);
  if(doormsg->carrier) hetwittedus=TRUE;
	strcpy(ostring,doormsg->string);
}

/*
 * prompt(mstring,ostring,len) - outputs the string "mstring" and inputs
 * a string which is placed in ostring.  'len' is the maximum number of
 * characters which will be accepted.
 */

void prompt(char *mstring,char *ostring,int len)
{
	strcpy(doormsg->string,mstring);
	doormsg->data=len;
	doormsg->command = 6;
	PutMsg(MyPort,(struct Message *)doormsg);
	WaitPort(replyport);
	GetMsg(replyport);
  if(doormsg->carrier) hetwittedus=TRUE;
	strcpy(ostring,doormsg->string);
}


/* showfile(mstring) shows the text file which mstring is the path to.
   It handles ^C aborting, and ^S/^Q pausing. */

void showfile(char *mstring)
{
	strcpy(doormsg->string,mstring);
	doormsg->command = 10;
	PutMsg(MyPort,(struct Message *)doormsg);
	WaitPort(replyport);
	GetMsg(replyport);
  if(doormsg->carrier) hetwittedus=TRUE;
}


/* enter a menu function to execute 1-33 */
void domenu(int menu,int extra,char *filename)
 {
	strcpy(doormsg->string,filename);
	doormsg->command = menu+100;
	doormsg->data	 = extra;
	PutMsg(MyPort,(struct Message *)doormsg);
	WaitPort(replyport);
	GetMsg(replyport);
  if(doormsg->carrier) hetwittedus=TRUE;
 }


/* change user information 1-15 */
void changeuserint(int number,long val)
 {
	long *ptr;

	ptr = (long *)&doormsg->string;	/* We need a long value not a ptr */
	*ptr = val;
	
	doormsg->command = 200;
	doormsg->data    = number;
	PutMsg(MyPort,(struct Message *)doormsg);
	WaitPort(replyport);
	GetMsg(replyport);
  if(doormsg->carrier) hetwittedus=TRUE;
 }

void main(int argc,char *argv[])
{
char line_number;
char buffer[100];

  /* Find tlf linie nummer */
  line_number=argv[argc-1][0];
	/* Create the door reply messageport */
	sprintf(buffer,"DoorReply%c",line_number);
	replyport=(struct MsgPort *)CreatePort(buffer,0L);
	doormsg=(struct DoorMsg *)AllocMem((long)sizeof(struct DoorMsg),MEMF_PUBLIC|MEMF_CLEAR);
	if(doormsg==0)
	{
		return;
	}
	doormsg->Door_Msg.mn_Node.ln_Type = NT_MESSAGE;
	doormsg->Door_Msg.mn_ReplyPort = replyport;
	doormsg->Door_Msg.mn_Length = (UWORD)sizeof(struct DoorMsg);
	/* Locate the correct DoorControl port based on the line number */
	sprintf(buffer,"DoorControl%c",line_number);
  Forbid();
 	MyPort=(struct MsgPort *)FindPort(buffer);
  Permit();
 	if(MyPort==0L) 
	{
		FreeMem(doormsg,(long)sizeof(*doormsg));
		return;
	}
  /* Skriv */
  domenu(14,0,argv[1]);
  hotkey(NULL,"   ");
  /* Exit */
	end();
}

