/*
** FunLiner/C
** by Kelly Cochran
** based off of Fun.rexx by Aaron Smith
** Last updated: Sun Nov  2 18:22:52 1997
*/

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

const char *version = "\0$VER: FunLiner-C 1.3 "__AMIGADATE__;

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

struct CPort           *cport;
struct MainPort        *myp;		/* Pointer to CNet port--ALL info!	*/
struct PortData        *z;
char	                 **bm;
struct Library         *CNetBase  = NULL;
struct Library         *CNetCBase = NULL;
struct CNetCContext    *context;
struct SignalSemaphore *SEM;


void main( int argc, char **argv )
{

/* Custom Declares */

   char  *uucp;
   short accnum;
   char  funids [ 10 ];
   char  highs  [ 10 ];
   char  buf1   [ 64 ] = "";
   char  buf2   [ 64 ] = "";
   char  buf3   [ 64 ] = "";
   char  date   [ 64 ] = "";
   char  date2  [ 32 ] = "";
   char  hours  [ 4  ] = "";
   int   funid;
   int   high;
   int   hour;
   long  t;

   struct tm *time_str;

   BPTR fin, fout, fhi;

/* End of Declares */

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

   if(!(CNetCBase = OpenLibrary("cnetc.library", 4)))
   {
      Printf("Unable to open cnetc.library!\n");
      exit(0);
   }

   if(!(context = CNC_InitContext(argv[1])))
   {
      CloseLibrary(CNetCBase);
      exit(1);
   }

	z    = context->z;
	myp  = context->myp;
	bm   = z->bm;

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

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

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

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

   if (fin = Open(buf1,MODE_OLDFILE))
   {
      FGets(fin,funids,sizeof(funids));
      Close(fin);
   }

   strcat(buf2,FUNDIR"Hi");

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

   high = atoi(highs);

   if (!high)
   {
      CNC_PutText("Missing File: "FUNDIR"Hi");
      GetOut();
   }

   funid = atoi(funids);

   if (!funid)
      funid = 1;

   sprintf(z->ABuffer,"n3[3Ac8%d/%d",funid,high);
   CNC_PutA();

   sprintf(funids,"%d",funid);
   strcat(buf3,FUNDIR);
   strcat(buf3,funids);
   CNC_ReadFile(buf3,0);

   time(&t);
   time_str=localtime(&t);
   strftime(date,64,"%A ",time_str);
   strftime(hours,4,"%I",time_str);
   hour = atoi(hours);
   if (!hour)
      hour = 12;
   sprintf(hours,"%d",hour);
   strcat(date,hours);
   strftime(date2,32,"[5m:[0m[1m[44;32m%M%p",time_str);
   strcat(date,date2);

   sprintf(z->ABuffer,"n1[35m[[1m[44;32m%s[35m[0m[35;40m] ",date);
   CNC_PutA();

   funid++;

   if (funid > high)
      funid = 1;

   sprintf(funids,"%d",funid);

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

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


void GetOut( void )
{
   if(CNetCBase)
   {
   	CNC_ShutDown( NULL );
      CNC_DisposeContext();
      CloseLibrary(CNetCBase);
   }

   if(CNetBase)
   {
      CloseLibrary(CNetBase);
   }
	exit(0);
}
