
#include <exec/types.h>
#include <clib/exec_protos.h>
#include <stdio.h>
#include <string.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <proto/pipeline.h>

#include <stdarg.h>

void	main			( int, char ** ) ;
void	MyWrite 		( char * ) ;
int	MyRead 			( char *, int ) ;
int	DoGauge 		( void ) ;
void	UpperCase		( char * ) ;
void	shutdown 		( void ) ;
void	StripNl 		( char * ) ;
int	MyDisplayFile 		( char * ) ;

ULONG			 LineNumber;
char			 Line[80];

struct	IntuitionBase	*IntuitionBase;
struct	Library			*PipelineBase;

struct SharedData *gl;

void
main ( int argc, char **argv )
{
	char		Buffer[80];
	BOOL		NotFinished = TRUE;

	if ( argc == 2 )
	{
		if ( IntuitionBase = (struct IntuitionBase *) OpenLibrary ( "intuition.library", 37 ) ) 
		{
			if ( PipelineBase = OpenLibrary ( "pipeline.library", 0 ) )
			{
				if ( gl = GetShared ( ) ) 
				{
					/* place shutdown hook */
					onexit ( shutdown ) ;
					if ( gl->SysOp )
					{
						LineNumber = atol ( argv[1] ) ;
						sprintf ( Line, "CHATPORT.%ld", LineNumber ) ;
	
						if ( MyDisplayFile ( "CallSysop/MainMenu" ) )
						{
							while ( NotFinished )
							{
								ClearMem ( Buffer, 80 ) ;
								MyRead ( Buffer, 2 ) ;
								UpperCase ( Buffer ) ;
								if ( ( strchr ( Buffer, 'C' ) ) || ( ! strlen ( Buffer ) ) )
								{
									while ( NotFinished )
									{
										if ( MyDisplayFile ( "CallSysop/Reason"  ) )
										{
											ClearMem ( Buffer, 25 ) ;
											MyRead ( Buffer, 25 ) ;
											if ( ! strlen ( Buffer ) )
											{
												NotFinished = FALSE;
											}
											else
											if ( ( strlen ( Buffer ) ) <= 5 )
											{
												if ( ! MyDisplayFile ( "CallSysop/BetterReason" ) )
												{
													Printf ( "\nCan't Find File \"CallSysop/BetterReason\"!" ) ;
												}
											}
											else
											{
												if ( MyDisplayFile ( "CallSysop/BeforeGauge" ) )
												{
													if ( DoGauge ( ) )
													{
														if ( ! MyDisplayFile ( "CallSysop/CantFindSysop" ) )
														{
															Printf ( "\nCan't Find File \"CallSysop/CantFindSysop\"!", 0 ) ;
															Delay ( 100 ) ;
														}
														else
															MyRead ( Buffer, 1 ) ;
													}
													NotFinished = FALSE;
												}
												else
												{
													Printf ( "\nCan't Find File \"CallSysop/BeforeGauge\"!" ) ;
													Delay ( 100 ) ;
													NotFinished = FALSE;
												}
											}
										}
										else
										{
											Printf ( "\nCan't Find File \"CallSysop/Reason\"!" ) ;
											Delay ( 100 ) ;
											NotFinished = FALSE;
										}
									}
								}
								else
								if ( strchr ( Buffer, 'Q' ) )
								{
									NotFinished = FALSE;
								}
							}
						}
						else
						{
							Printf ( "\nCould not Open Text File \"CallSysop/MainMenu.Txt\"!\n" ) ;
							Delay ( 100 ) ;
						}
					}
					else
					{
						if ( ! MyDisplayFile ( "CallSysop/SysopNotIn" ) )
						{
							Printf ( "\nCan't Find File \"CallSysop/SysopNotIn\"!", 0 ) ;
							Delay ( 100 ) ;
						}
						MyRead ( Buffer, 1 ) ;
					}
				}
				else
				{
					Printf ( "\nPipeline is not running!\n" ) ;
					Delay ( 100 ) ;
				}
			}
			else
			{
				Printf ( "\nCould not Open the pipeline.library!\n" ) ;
				Delay ( 100 ) ;
			}
		}
		else
		{
			Printf ( "\nCallSysop and Pipeline require Workbench 2.04 (v37) or greater!\n" ) ;
			Delay ( 100 ) ;
		}
	}
	else
	{
		Delay ( 100 ) ;
		Printf ( "\nUsage: CallSysop <LineNumber>\n" ) ;
	}
	exit ( ) ;
}

int
DoGauge ( void )
{
	int					 i;
	FILE					*fh;
	char					 Buffer[256];
	int					 Result = 1;

	sprintf ( Buffer, "%sCallSysop/GaugeLevels", gl->Paths->Text ) ;
	if ( fh = fopen ( Buffer, "r" ) )
	{	
		for ( i = 1 ; i <= 10 ; i++ )
		{
			if ( ! FindPort ( Line ) )
			{
				if ( fgets ( Buffer, 256, fh ) )
				{
					DisplayBeep ( NULL ) ;
					StripNl ( Buffer ) ;
					MyWrite ( Buffer ) ;
					Delay ( 50 ) ;
				}
				else
				{
					fclose ( fh ) ;
					return( 1 ) ;
				}
			}
			else
			{
				Result = 0;
				break;
			}
		}
		fclose ( fh ) ;
	}
	else
		Printf ( "\nCan not open file \"%s\".\n", Buffer ) ;

	return ( Result ) ;
}

void
MyWrite ( char *string )
{
	Write ( Output(), string, strlen ( string ) ) ;
}

int
MyRead ( char *buf, int maxx )
{
	int		lp;

	for ( lp = 0 ; *buf != 10 && *buf != 13 && lp < maxx ; )
	{
		Read ( Input ( ), buf, 1 ) ;

		/* Control-C */
		if ( *buf == 3 )
		{
			exit( );
		}

		if ( *buf == '\b' )
		{
			if ( lp > 0 )
			{
				MyWrite ( "\b \b" ) ;
				buf -- ;
				lp -- ;
			} 
		}
		else
		if ( *buf != 10 && *buf != 13 )
		{
			*(buf + 1 ) = '\0' ;
			MyWrite ( buf ) ;
			lp ++ ;
			buf ++ ;
		}
	}
	*( buf ) = '\0' ;
	return ( lp ) ;
}

void
UpperCase ( char *string )
{
	char *cp = string;

	while ( *cp != '\0' )
	{
		if ( islower ( *cp ) )
			*cp = toupper ( *cp ) ;
		cp++;
	}
}

void
shutdown ( void )
{
	CloseLibrary ( (struct Library *) IntuitionBase ) ;
	CloseLibrary ( PipelineBase ) ;
}

void
StripNl ( char *buffer )
{
	char *cp = buffer;

	while ( *cp != '\0' )
	{
		if ( *cp == '\n' || *cp == '\r' )
			*cp = '\0';
		cp++;
	}
}

int
MyDisplayFile ( char *FileName )
{
	BPTR	fh;
	char	Buffer[256];

	if ( fh = OpenTextFile ( LineNumber, FileName ) )
	{
		while ( FGets ( fh, Buffer, 256 ) )
		{
			MyWrite ( Buffer ) ;
		}
		Close ( fh ) ;
		return ( TRUE ) ;
	}
	return ( FALSE ) ;
}