/* *****                                                            *****
** *****           Program created by Ken Farinsky, 1986.           *****
** *****                                                            *****
**
** All material contained herein may be used in any way desired.
**                    >>>>> i.e.  PUBLIC DOMAIN. <<<<<
** Not limited to any single bulletin board - can be freely transferred.
**
** Original distribution through:
**     Slipped Disk, Inc.
**     Madison Heights,  MI  48071
**     (313) 583-9803
**
** Distributed in the hopes of increasing the level of understanding of the
** Amiga personal computer.  Even the best computer can only succeed with
** the proper software support.
*/
#include <exec/types.h>
#include <intuition/intuition.h>
#include <functions.h>
#include <libraries/dos.h>
#include <exec/memory.h>
#include <workbench/startup.h>
#include "view.h"

extern struct NewWindow		NewWin ;
struct Remember		*menu_memory = NULL ;

/*----------------------------------------------------------------------
** main().
** this is sort of a generic startup routine.  it can be used for most
** applications.  you know, open the libraries, open the window, set the
** menu, open a font, run the program.  When the program finishes, clear
** the menu, free any memory, close the font, close the window, and 
** close the library.  nothing to it.
**
** note that the program does different things depending on its origin.
** CLI programs use run_cli(), otherwise (workbench) use run_nocli().
*/
main( argc, argv)
int		argc ;
char	**argv ;
{
short			library_open() ;
void			library_close() ;
void			run_cli() ;
void			run_nocli() ;
UBYTE		   *file_request() ;
struct Window  *Win ;

extern struct Menu			menu_header ;
extern struct TextFont	   *textfont ;
extern struct TextAttr		textattr ;

textattr.ta_Name = (STRPTR)"topaz.font" ;
textattr.ta_YSize = 8 ;
textattr.ta_Style = 0 ;
textattr.ta_Flags = 0 ;

if ( SUCCESS == library_open())
	{
	NewWin.Title = (UBYTE *)"view" ;
	if ( NULL != ( Win = OpenWindow( &NewWin)))
		{
		SetMenuStrip( Win, &menu_header) ;
		if ( NULL != ( textfont = OpenFont( &textattr)))
			SetFont( Win->RPort, textfont) ;

		if (argc == 0)
			run_nocli( argv) ;
		else
			run_cli( argc, argv) ;
		ClearMenuStrip( Win) ;

		/* free all menu memory (i.e. used for font list) */
		FreeRemember(&menu_memory, TRUE) ;
		CloseFont( textfont) ;
		CloseWindow( Win) ;
		}
	library_close() ;
	}
}

/*----------------------------------------------------------------------
** run no cli - startup from the workbench.
**
** for each file in the argument list, change to the new directory,
** view the file, and change back to the original directory.
*/
void run_nocli( WBSptr, Win)
struct WBStartup	*WBSptr ;
struct Window		*Win ;
{
void			 do_view() ;
UBYTE			 filename[40] ;
struct WBArg	*arg ;
short			 argc ;
struct Lock		*olddir ;

argc = ((struct WBStartup *)(WBSptr))->sm_NumArgs ;
arg  = ((struct WBStartup *)(WBSptr))->sm_ArgList ;

if ( argc == 1)
	do_view( "", Win) ;
else
	while ( 1 < argc--)
		{
		arg++ ;
		if ( NULL != arg->wa_Lock)
			{
			olddir = CurrentDir(arg->wa_Lock) ;
			strncpy( filename, (char *)arg->wa_Name, 30) ;
			do_view( filename, Win) ;
			CurrentDir( olddir) ;
			}
		}
}

/*----------------------------------------------------------------------
** run CLI - from the CLI.
**
** for each file in the command line, view the file.
*/
void run_cli( argc, argv, Win)
int					  argc ;
UBYTE				**argv ;
struct Window		 *Win ;
{
void	do_view() ;
UBYTE	filename[40] ;

if ( argc == 1)
	do_view( "", Win) ;
else
	while ( 1 < argc--)
		{
		strncpy( filename, *++argv, 30) ;
		do_view( filename, Win) ;
		}
}

/*----------------------------------------------------------------------
** do view.  view the file.  note that the user may switch between many
** files in each iteration of this routine (using the file requester...).
*/
void do_view( filename, Win)
UBYTE				*filename ;
struct Window		*Win ;
{
UBYTE			   **data ;
struct Remember		*memory_key ;
UBYTE				 win_title[100] ;
UBYTE			    *null_file[2] ;

UBYTE  *process_window() ;
void	read_datafile() ;
SHORT	count_records() ;

null_file[0] = (UBYTE *)"" ;
null_file[1] = NULL ;

/* if there is no file name, then get one. */
if ( *filename == '\0')
	filename = file_request( Win) ;

/* while the name is still valid (only quit when it is null).
** note that returning here causes the program to switch to the
** next filename in the argument list.
*/
while ( *filename != '\0')
	{
	memory_key = NULL ;

	/* read the data for the file into memory. set up a title */
	read_datafile( filename, &data, &memory_key) ;
	sprintf( win_title, "view \"%s\" - %d records",
				filename, count_records(data)) ;

	/* if there is no data, then title==ERROR */
	if ( data == NULL)
		{
		sprintf( win_title, "ERROR OPENING FILE \"%s\" - 0 records",filename) ;
		data = null_file ;
		}

	/* set the title and process the window. */
	SetWindowTitles( Win, win_title, -1L) ;
	filename = process_window( Win, data, &menu_memory) ;

	/* free any data allocated for this file. */
	FreeRemember(&memory_key, TRUE) ;
	}
}
