#define DATAFILE  "Fun"
#define FUNDIR    "VR:Fun/Funline/"

/* #define DEBUG */

char version[] = { "$VER: FunEnter-C 1.0 by Trolan (trolan@gnomes.org)" };

/************************************************************************
 *    CNet/3 and CNet/4  C language interface routines and examples     *
 *                                                                      *
 *  copyright © 1996 ZenMetal Software ... this code may be freely      *
 *  distributed to and used by registered CNet owners EXCLUSIVELY.      *
 *  Other distribution is in violation of copyright laws.               *
 ************************************************************************/



/************************************************************************
 *                          Function prototypes                         *
 ************************************************************************/
void		CallHost( UBYTE c );
void		ShutDown( char *spawn );
void		GetOut( void );
void		LoadError( void );


/************************************************************************
 *                           Global Variables                           *
 ************************************************************************/
struct MsgPort         *replyp;	/* Some communication details ...	*/
struct CPort           *cport;
struct CMessage        cmess;
struct MainPort        *myp;		/* Pointer to CNet port--ALL info!	*/
struct PortData        *z;
char	                 **bm;
struct Library         *CNetBase = NULL;
struct SignalSemaphore *SEM;



/************************************************************************
 *                             Main routine                             *
 ************************************************************************/
void main( int argc, char **argv )
{

/* Custom Declares */

   char  *uucp;
   short accnum;
   char  funids [ 64 ] = "";
   char  highs  [ 10 ];
   char  buf1   [ 64 ] = "";
   char  buf2   [ 64 ] = "";
   char  buf3   [ 64 ] = "";
   int   funid = 0;
   int   high;
   short items;

   BPTR fout, fhi;

/* End of Declares */

	Forbid();
	if(argc > 1)
		cport = (struct CPort *)FindPort( argv[1] );
	Permit();

	if( argc<2 || !(cport) ) {
		printf("This is a CNet C program.\n");
		exit(0);
		}

	if( !(replyp = CreatePort( 0,0 )))
		exit(0);

	cmess.cn_Message.mn_ReplyPort   = replyp;
	cmess.cn_Message.mn_Length      = sizeof( struct CMessage );
	cmess.cn_Message.mn_Node.ln_Name= "cstuff";

	// CNet version check
	if( cport->ack != 30 )
   {	                     // change "30" to be compatible with only pre 4.13 systems
		cport->ack = 1;
		LoadError();
   }

//	if( cport->ack != 40 )      // Replace the code above with this
//		if( cport->ack != 30 )   // to support both 3.x and 4.x systems
//			{
//			cport->ack = 1;
//			goto err;
//			}

	cport->ack = 0;

	z    = cport->zp;
	myp  = cport->myp;
	SEM  = myp->SEM;
	bm   = z->bm;

	if( !(CNetBase = OpenLibrary( "cnet.library", 4 )) ) // <- NOTE version 4 - can be changed to 3 to make compatible with older CNet releases!
		LoadError();

/******* *****************************************************************
 *           End of CNet setup - YOUR CUSTOM CODE STARTS HERE           *
 ************************************************************************/

   uucp = z->user1.UUCP;
   accnum = z->id;
   items = z->npitems;

   sprintf(buf1,"Mail:Users/%s/"DATAFILE".%d",uucp,accnum);

   strcat(buf2,FUNDIR"Hi");

   if (fhi = Open(buf2,MODE_OLDFILE))
   {
      FGets(fhi,highs,sizeof(highs));
      Close(fhi);
   }

   high = atoi(highs);

   if ((items > 0) && (z->pitem[1] != NULL))
   {
      strcat(funids,z->pitem[1]);
      if (atoi(funids) < 1)
         funid = 0;
      else
         funid = atoi(funids);
   }

#ifdef DEBUG
   PutText(funids); /* DEBUG */
#endif

   if (!funid)
   {
      PutText("n1caEnter Funliner number to jump tocd: ");
      if (EnterLine(5,64,""))
         strcat(funids,z->InBuffer);
   }

   funid = atoi(funids);

   if (!funids)
      GetOut();

   if ((funid > high) || (funid < 1))
   {
      sprintf(z->ABuffer,"n1caSorry %d is too high.  The highest Funliner is %d.n1",funid,high);
      PutA();
      GetOut();
   }
   
   strcat(buf3,FUNDIR);
   strcat(buf3,funids);

   if (fout = Open(buf1,MODE_NEWFILE))
   {
      FPuts(fout,funids);
      Close(fout);
   }

   sprintf(z->ABuffer,"n1caCurrent Funliner set to %d.n1",funid);
   PutA();

	/* exit back to CNet - always call GetOut() to exit back to CNet! */
	GetOut();
}


/**************************************************************************
 *       Routine called if Load error (wrong CNet version, etc,...        *
 **************************************************************************/
void LoadError( void )
{
	DeletePort( replyp );
	exit(0);
}

/************************************************************************
 *                           Generic EXIT code                          *
 ************************************************************************/
void GetOut( void )
{
	ShutDown( NULL );
	DeletePort( replyp );
	exit(0);
}


/**************************************************************************
 *                         another file to run?                           *
 **************************************************************************/
void ShutDown( char *spawn )
{
	/* spawn = full path/file to run */
	if( spawn )
		strcpy( z->CSpawn, spawn );

	CallHost( 0 );
}


void CallHost( UBYTE c )
{
	cmess.command = c;
	PutMsg  ( (struct MsgPort *)cport, (struct Message *)&cmess );
	WaitPort( replyp );
	GetMsg  ( replyp );
}

void PutText( char *text )
{
	cmess.arg1 = (ULONG)text;	/* text to print		*/
	CallHost( 1 );
}

void PutA( void )
{
	PutText( z->ABuffer );
}


/**************************************************************************
 *    ENTERLINE FLAGS:                                                    *
 *      1: All capitalized                                                *
 *      2: FILENAME.  Don't allow =":; or asterisk                        *
 *      4: Begin with existing z.InBuffer                                 *
 *      8: Chop leading spaces                                            *
 *     16: Force 1st letter of word caps                                  *
 *     32: Force all others lower case                                    *
 *     64: Numeric input only                                             *
 *    128: Print input box (.)                                            *
 *    256: DO allow MCI                                                   *
 *    512: HANDLES/SPECIAL.  Don't allow ^_`{|}~@                         *
 *   1024: Exit for . or / at beginning of line                           *
 *   2048: Exit for backspace at beginning of line                        *
 *   4096: Do not allow OLM's to appear while editing                     *
 *   8192: Allow Chat break in at this prompt. COMMAND PROMPT.            *
 *  16384: Don't allow SPACE, either                                      *
 *  32768: DON'T ALLOW MOVEMENT                                           *
 *  65536: Don't allow forward slash                                      *
 **************************************************************************/
int EnterLine( UBYTE len, ULONG flags, char *prompt )
{
	cmess.arg1 = (ULONG)len;	/* how many chars max to input	*/
	cmess.arg2 = (ULONG)flags;	/* 1=UpperCase			*/
	cmess.arg3 = (ULONG)prompt;	/* text to print before input	*/
	CallHost( 2 );			/* result is in z->InBuffer	*/
	return( (int)strlen( z->InBuffer ));
}
