/* File : Icon.c
 *
 * $Project:         Bindnames
 *
 * $Module Id:         
 * $Original Author:   Daryl Hegyi and Robert Hardy
 * $Date Started:      Mon Aug  8 1994
 *
 * $Revision: 1.5 $
 *
 * $State: Exp $
 *
 * $Locker:  $
 *
 * $Log: Icon.c-v $
 * Revision 1.5  1995/11/06  02:39:25  Bob
 * OOPs.  Closed IconBase before FreeDiskObject().
 *
 * Revision 1.4  1995/11/05  06:03:54  Bob
 * Cleanup and optimize;
 *
 * Revision 1.3  1995/11/05  05:36:25  Bob
 * Debugging,  code was expecting a project icon.
 * Fixed to handle both.
 *
 * Revision 1.2  1995/11/05  04:54:27  Bob
 * Opps! Was not freeing DiskObject.
 *
 * Revision 1.1  1995/08/08  02:28:02  Bob
 * Initial revision
 * 
 *
 *
 */


#include <exec/types.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <workbench/workbench.h>
#include <workbench/startup.h>
#include <proto/all.h>

#include "BindNames.h"

#define dbg(FLAGS, args) 				\
{												\
	if ( ((FLAGS) & DBG_Flags) == FLAGS )	\
	{											\
		printf("\r[33m%s: %d <%s>[0m ", __FILE__, __LINE__,__FUNC__); \
		printf args;							\
	}											\
}											 /**/

#define LDT_Gen_Icon 	1
private long DBG_Flags = 0;

static struct FileLock  *Old_Lock = NIL,
						*Program_Lock = NIL,
						*Datafile_Lock = NIL;
static short IconBase_Loaded = 0;

struct DiskObject *Disk_Obj = NIL;

static short get_arg_list(ARG_LIST *arg_list, char *objname, short num_to_init)
{
	char **ToolTypes;
	char *tool;
	ARG_LIST *argptr;
	short count = 0;
	short argnum = 0;

	dbg(LDT_Gen_Icon, ("Obj name is: '%s'\r\n", objname));

	if ( (argptr = arg_list) )
	{
		if ( (Disk_Obj = GetDiskObject(objname)) )
		{
			ToolTypes= Disk_Obj->do_ToolTypes;

			while ( (argptr->arg_type) != NIL )
			{
				dbg(LDT_Gen_Icon, ("arg #%d [%s]", argnum+1, argptr->arg_type));

				++argnum;	/* count it */

				if (  ( tool = FindToolType( ToolTypes, argptr->arg_type ) )  )
				{
					argptr->arg_value = tool;
					++count;
				}
				else if ( argnum <= num_to_init )
				{
					argptr-> arg_value = objname;
					++count;
				}

				++argptr;	/* point to next one */
			}
		}
	}

	return count;
}

short Icon_Launch( int argc, char **argv,
            		ARG_LIST *program_args, short num_to_init )
{
	struct WBArg *wbarg;
	struct WBStartup *wb = (struct WBStartup *) argv;
	short result = FALSE;

			/* started from workbench */
	if (argc == 0 && wb )
	{
		if ( (IconBase = OpenLibrary("icon.library", 0L)) )
	    {
			/* should point to program (tooltype) name */
	   	    wbarg = wb->sm_ArgList;

		    if ( wbarg )
			{
				Program_Lock = (struct FileLock *) wbarg->wa_Lock;

				if (wb->sm_NumArgs > 1 && (++wbarg)->wa_Name)
		    	{
					/* change to the file's directory */
			    	Datafile_Lock = (struct FileLock *)wbarg->wa_Lock;

					if (Datafile_Lock)
						Old_Lock = (struct FileLock *)CurrentDir(
							(BPTR)Datafile_Lock);
					else
						Old_Lock = (struct FileLock *)CurrentDir(
								(BPTR)Program_Lock);

		        }
		        else
					Old_Lock = (struct FileLock *)CurrentDir(
							(BPTR)Program_Lock);

				dbg(LDT_Gen_Icon, ("Icon Name is '%s'\r\n", wbarg->wa_Name));

				/* set desired argument values to point to the icon name */
				if (program_args)
				{
					get_arg_list(program_args, wbarg->wa_Name, num_to_init);
				}

	    	    result = TRUE;
		    }
		}
	}

	return result;
}

void Icon_Finish(void)
{

	if (Old_Lock)
		Old_Lock = (struct FileLock *)CurrentDir((BPTR)Old_Lock);

	if (Disk_Obj)
	{
		FreeDiskObject(Disk_Obj);
		Disk_Obj = NIL;
	}

	if (IconBase)
	{
		CloseLibrary(IconBase);
		IconBase = NIL;
	}

	Program_Lock = Datafile_Lock = NIL;
}

