/* FL_Panel_Init.c */

/* This file will parse the initialization file for FL_Panel */
/* Uses: FL_Panel_Init.h */

/*
   When this file completes, There will be specified values for the <file_name> <seed> and <points>.
*/

/* 
*/
#include <exec/types.h>
#include <pragmas/exec_pragmas.h>
#include <pragmas/icon_pragmas.h>
#include <pragmas/utility_pragmas.h>
#include <libraries/dos.h>
#include <workbench/workbench.h>
#include <workbench/startup.h>
#include <clib/alib_protos.h>
#include <clib/exec_protos.h>
#include <clib/dos_Protos.h>
#include <clib/icon_protos.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <dos.h>

/* #include <stream.h> */
#include "FL_Panel_GlobExt.h"
#include "FL_Panel_Init.h"
/* 
*/
struct Library *IconBase = NULL;
struct DiskObject *dobj;
static LONG olddir = -1;
       SHORT i;
static BOOL FromWb;

static BOOL False = 0;
static BOOL True  = !FALSE;
static char **ToolArray;
static BOOL InitComplete = FALSE;
/* 
*/
void message (UBYTE *TheMessage )
{
  if ( !FromWb ) printf ( "%s\n", TheMessage );
  return;
}

void cleanup ( void )
{
 if ( IconBase ) CloseLibrary(IconBase);
}

void FL_Panel_Init_cleanexit (UBYTE *TheMessage, LONG ErrorCode )
{
  if ( *TheMessage ) message ( TheMessage );
  cleanup ();
  InitComplete = ( ErrorCode == RETURN_FAIL );
  return;
}
/* 
*/
void FlPanelFindToolTypes ( char* ToolTypeNames[], char *ToolType2Find )
{
  int   NextToolType = FirstToolType;

  for ( ++NextToolType; NextToolType < LastToolType; NextToolType ++ )
      { /* All Possible Tooltypes */
        if ( !strncmp ( ToolType2Find, ToolTypeNames [ NextToolType ], strlen ( ToolTypeNames [ NextToolType ] ) ) )
           { /* Found a tool type */
             switch ( toupper ( ToolTypeNames [ NextToolType ] [ 0 ] ) )
             {
               case 'F' : /* Process FileNameTT           */
                          {
                            for ( ; (*ToolType2Find); ++ToolType2Find )
                            {
                               if ( (*ToolType2Find) == '=' ) break; /* Look for an = so we can process number */
                            }
                            if ( '=' == *ToolType2Find )
                               { /* we found start of the file name */
                                 strncpy ( UsersFileName, ++ToolType2Find, 149 );
                               }
                               else
                               { /* set up the default */
                                 strncpy ( UsersFileName, DefaultFileName, 149 );
                               } /* endif */
                            /* This is where we set the string gadget */
                          }
                          break;
               case 'X' : /* Process X Points            */
                          {
                            for ( ; (*ToolType2Find); ++ToolType2Find )
                            {
                               if ( (*ToolType2Find) == '=' ) break; /* Look for an = so we can process number */
                            }
                            if ( '=' == *ToolType2Find )
                               { /* we found start of the Points */
                                 XPointCardinality = atoi ( ++ToolType2Find );
                               }
                               else
                               { /* set up the default */
                                 XPointCardinality = DefaultXPoints;
                               } /* endif */
                          }
                          break;
               case 'Y' : /* Process Y Points             */
                          {
                            for ( ; (*ToolType2Find); ++ToolType2Find )
                            {
                               if ( (*ToolType2Find) == '=' ) break; /* Look for an = so we can process number */
                            }
                            if ( '=' == *ToolType2Find )
                               { /* we found start of the Points */
                                 YPointCardinality = atoi ( ++ToolType2Find );
                               }
                               else
                               { /* set up the default */
                                 YPointCardinality = DefaultYPoints;
                               } /* endif */
                          }
                          break;
               case 'S' : /* Process Seed                 */
                          {
                            /* Look for an = so we can process number */
                            for ( ; (*ToolType2Find); ++ToolType2Find ) if ( (*ToolType2Find) == '=' ) break;

                            if ( '=' == *ToolType2Find )
                                    { Seed = atoi ( ++ToolType2Find   ); /* Found start seed value   */ }
                               else { Seed = DefaultSeed;                /* Set up the default       */ }
                          }
                          break;
               default  : /* Process extraneous tool type */
                          break;
             } /* end of switch */
           } /* endif */
      } /* end of for */
}
/* 
*/
/***********************************************************************/
/*                                                                     */
/* FUNCTION: FL_Panel_Init                                             */
/* PURPOSE : To parse the tooltypes (if any) and set up the default    */
/*              values of SEED, POINTS, and FileName                   */
/* RETURNS : BOOL TRUE if a DISKOBJ was found and processed,           */
/*                otherwise it returns a BOOL FALSE.                   */
/*                                                                     */
/***********************************************************************/

BOOL FL_Panel_Init ( int argc, char ** argv, struct WBStartup *WBenchMsg )
{
  int i;
  FromWb = ( argc == 0 ) ? True : False;

  for ( i = 0; i < 150; ++i ) UsersFileName [ i ] = '\0'; /* Make sure we start with null stiring. */
  strncpy ( UsersFileName, DefaultFileName, 149 );
  XPointCardinality = DefaultXPoints;
  YPointCardinality = DefaultYPoints;
  Seed = DefaultSeed;

  if ( FromWb )
  { /* Then someone has clicked on an icon, look for tools types. */
    argc = _WBArgc;
    argv = _WBArgv;

    if ( ! ( IconBase = OpenLibrary ( "icon.library", 33UL ) ) )
      FL_Panel_Init_cleanexit ( "Can't open icon.library\n", RETURN_FAIL );

    for ( i = 0; i < argc; i++ )
     { /* Process the tool types associated with this icon */
       FlPanelFindToolTypes ( ToolTypeNames, argv [ i ] );
       InitComplete = TRUE;
     }

  }
  else
   InitComplete = TRUE; /* We are started from CLI */
  cleanup ();
  return ( InitComplete );
}
