//
//	ManPageWindow Class
//
//	Harald Schlangmann, 19.7.93
//

#undef TEST

#import "ManPageWindow.h"
#import "ManOpenApp.h"
#import "WhereAmI.h"

@implementation ManPageWindow

- printManPage:sender {

	[theText printPSCode:sender];
	
	return self;
}

- close {

	//	Notify Application of Window's close (workaround deadlock)

	[NXApp perform:@selector(manPageClosed:) with:self
		afterDelay:1 cancelPrevious:NO];

	return [super close];
}

- abortLoad {

	NXRunAlertPanel(NULL,"Cannot open Manual Page "
		"for ``%s''...",NULL,NULL,NULL,[titleString stringValue]);
	return [self close];
}

- makeKeyAndOrderFront:sender {

	[super makeKeyAndOrderFront:sender];
	
	return [self makeFirstResponder:theText];
}

- free {

	if( theFile )
		NXZoneFree(NXDefaultMallocZone(),theFile);
	return [super free];
}

- loadManPage:(const char *)name {

	char		command[2*FILENAME_MAX];
	FILE		*pipe;
	
#ifndef TEST
	NXStream	*aStream;
#endif

	char		*cptr;
	BOOL		hasCat = NO;
	
	//	Store filename

	if( theFile )
		NXZoneFree(NXDefaultMallocZone(),theFile);
	theFile = NXCopyStringBuffer(name);
	
	//	Prepare window
	
	[theText setText:""];
	
	//	Prepare short Title
	
	cptr = strrchr(theFile,'/');
	
	if( cptr ) {
	
		//	Check whether catX exists
	
		if( cptr-theFile>4&&strncmp(cptr-4,"man",3)==0 ) {
		
			struct stat statMan;
			struct stat statCat;
			
			if( stat(theFile,&statMan)==0 ) {
				cptr[-4] = 'c';
				cptr[-2] = 't';
				if( stat(theFile,&statCat)==0 ) {
					if( statCat.st_mtime
						<statMan.st_mtime ) {
						cptr[-4] = 'c';
						cptr[-2] = 't';
					} else
						hasCat = YES;
				} else {
					cptr[-4] = 'm';
					cptr[-2] = 'n';
				}
			}
		}
		
		cptr++;
	} else
		cptr = theFile;

	strcpy(command,cptr);
	
	cptr = strrchr(command,'.');
	if( cptr )
		*cptr = '\0';
	
	strcat(command," Manual Page");
	
	[titleString setStringValue:command];
	
	//	Loading title
	
	if( hasCat )
		sprintf(command,"Loading %s...",theFile);
	else
		sprintf(command,"Reformating and loading %s...",theFile);
	[self setTitle:command];
	
	//	Order front window now
	
	[self makeKeyAndOrderFront:self];
	
	//	Create command
	
	if( hasCat ) {
		char 	buffer[FILENAME_MAX];
		sprintf(command,"/bin/cat %s | %s",theFile,
			[NXApp whereIs:"cat2rtf" path:buffer]);
		cptr = strrchr(theFile,'/');
		cptr[-4] = 'm';
		cptr[-2] = 'n';
	} else {
		char 	buffer[FILENAME_MAX];
		sprintf(command,[NXApp nroffCmd],theFile);
		strcat(command,"| ");
		strcat(command,[NXApp whereIs:"cat2rtf" path:buffer]);
	}
	
	//	Execute command and prepare NXStream for reading
	
	pipe = popen(command,"r");
	if( !pipe )
		return [self abortLoad];

#ifndef TEST
	aStream = NXOpenFile(fileno(pipe),NX_READONLY);
	if( !aStream ) {
		pclose(pipe);
		return [self abortLoad];
	}

	//	Read text
	
	[[theText readRichText:aStream] sizeToFit];
#else
	{
		int	ch = fgetc(pipe);
		
		while( ch!=EOF ) {
			fputc(ch,stderr);
			ch = fgetc(pipe);
		}
	}

#endif
	[self setTitleAsFilename:theFile];
	
	//	Close NXStream and file descriptor

#ifndef TEST
	NXClose(aStream);
#endif

	pclose(pipe);
	
	[self setMiniwindowIcon:"ManOpen"];

	return self;
}

- (const char *)getFile {

	return theFile;
}

@end
