
/*
 *	Function	ProgArgsWB
 *	Programmer	N.d'Alterio
 *	Date		11/09/95
 *
 *  Synopsis:	Parse SpoolWatch arguments if started from WB, the
 *		arguments structure it returns should be freed using
 *		FreeProgArgs.
 *
 *  Arguments:  **argv				The standard main(argv)
 *
 *  Returns:	*swa				Pointer to filled in arguments
 *						structure.
 *
 *		NULL				If error.
 *
 *  Variables:	**tooltypes			Array for tooltypes strings;
 *		 *type				Temp string; 
 *		 *wbmsg				Message received on startup
 *						from WB;
 *		 *wbarg				WB program arguments
 *		 *dobj				Info obtained from icon;
 *		 *swa				Processed arguments structure.
 *  
 *  Functions:	AllocMem			Allocate memory (EXEC)
 *		FreeMem				Free memory (EXEC)
 *		OpenLibrary			Opens library (EXEC)	
 *		CloseLibrary			Close library (EXEC)
 *		GetDiskObject			Reads icon file (ICON)
 *		FreeDiskObject			Frees mem from GetDiskObject (ICON)
 *		FindToolType			Locates a tooltype (ICON)
 *		SWError				Outputs error message (SW)
 *		strlen				Length of string (ANSI)
 *		strcpy				Copy a string to another (ANSI)
 *		atoi				Ascii to integer (ANSI)
 *
 *  $Id: ProgArgsWB.c 1.6 1995/09/24 18:29:03 daltern Exp $
 *
 */

#include "SpoolWatch.h"

extern struct Library *IconBase;

struct SWArgs *ProgArgsWB( char *argv[] )

{
 
  char **tooltypes;

  char *type;

  struct WBStartup  *wbmsg;
  struct WBArg      *wbarg;
  struct DiskObject *dobj;

  struct SWArgs     *swa;

  if ( swa = (struct SWArgs *)AllocMem( sizeof( struct SWArgs ), MEMF_ANY | MEMF_CLEAR ) ) {

  	wbmsg = (struct WBStartup *)argv;
  	wbarg = wbmsg->sm_ArgList;

  	if ( IconBase = OpenLibrary( "icon.library", 37 ) ) {

/*
 *   Get the icon and read tooltypes.
 */

		if ( dobj = GetDiskObject( wbarg->wa_Name ) ) {

			tooltypes = (char **)dobj->do_ToolTypes;

			if ( type = (char *)FindToolType( tooltypes, "MAILDIR" ) ) {
				swa->mail_spooldir = (char *)AllocMem( (ULONG)(strlen( type ) * sizeof( char )), MEMF_ANY );
 				strcpy( swa->mail_spooldir, type );
			} else {
				swa->mail_spooldir = (char *)AllocMem( (ULONG)(strlen( MAIL_SPOOLDIR ) * sizeof( char )), MEMF_ANY );
				swa->mail_spooldir = MAIL_SPOOLDIR;
			}   /* end if mail spool */

			if ( type = (char *)FindToolType( tooltypes, "NEWSDIR" ) ) {
				swa->news_spooldir = (char *)AllocMem( (ULONG)(strlen( type ) * sizeof( char )), MEMF_ANY );
 				strcpy( swa->news_spooldir, type );
			} else {
				swa->news_spooldir = (char *)AllocMem( (ULONG)(strlen( NEWS_SPOOLDIR ) * sizeof( char )), MEMF_ANY );
				swa->news_spooldir = NEWS_SPOOLDIR;
			}   /* end if news spool */

			if ( type = (char *)FindToolType( tooltypes, "DELAY" ) ) {
				swa->delay = atoi( type );
			} else {
				swa->delay = UPD_DELAY;
			}   /* end if update delay */

			if ( type = (char *)FindToolType( tooltypes, "LEFT" ) ) {
				swa->win_left = atoi( type );
			} else {
				swa->win_left = WIN_LEFT;
			}   /* end if win left */

			if ( type = (char *)FindToolType( tooltypes, "TOP" ) ) {
				swa->win_top = atoi( type );
			} else {
				swa->win_top = WIN_TOP;
			}   /* end if win top */

			FreeDiskObject( dobj );

		} else {

			SWError( swa, NULL, "Could not find program icon" );
			FreeMem( swa, sizeof( struct SWArgs ) );
			swa = NULL;

		}   /* end if GetDiskObject */

		CloseLibrary( IconBase );

  	} else {

		SWError( swa, NULL, "Could to open icon library >v37" );
		FreeMem( swa, sizeof( struct SWArgs ) );
		swa = NULL;

  	}   /* end if OpenLibrary */

  }   /* end if swa, swa = NULL if alloc fails */

  return swa;

}   /* end function ProgArgsWB */

/*========================================================================*
		      END FUNCTION ProgArgsWB
 *========================================================================*/