/* SetAlternate.c */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* (c) Copyright 1986 John A. Toebes, VIII   All rights reserved         */
/*     120-H Northington Place,   Cary NC 27511   (919) 469-4210         */
/*  This program may be used and modified for any purpose so long as     */
/*  this copyright notice remains with the code and the program is not   */
/*  sold and no charge is made for its use.                              */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <stdio.h>
#include <exec/types.h>
#include <workbench/workbench.h>
#include <workbench/icon.h>
#include <workbench/startup.h>

int IconBase;
extern struct WBStartup *WBenchMsg;

main(argc,argv)
int argc;
char *argv[];
{
	struct DiskObject *diskobja, *diskobj, *GetDiskObject();
	APTR saveimage;
	USHORT saveflags;

	/* make sure we ran from CLI */
	if (argc == 0) exit(1);

	/* make sure they specified two icons */
	if (argc != 3)
		{
		printf("Usage: %s <icon> <icona>\n", argv[0]);
		exit(2);
		}

	/* make the icon library available to us */
	if ( (IconBase = OpenLibrary( ICONNAME, 1)) == NULL)
		exit(2);

	/* read the icon to modify from disk */
	if ( (diskobj = GetDiskObject(argv[1])) == NULL)
		{
		printf("Cannot read icon for %s\n", argv[1]);
		CloseLibrary( IconBase );
		exit(3);
		}

	/* read the second icon to get its image */
	if ( (diskobja = GetDiskObject(argv[2])) == NULL)
		{
		printf("Cannot read icon for %s\n", argv[1]);
		FreeDiskObject(diskobj);
		CloseLibrary( IconBase );
		exit(4);
		}

	/* remember what the original icon looked like */
	/* we will restore it before we do a FreeDiskObject on it */
	/* this way the system won't get confused as to which memory it */
	/* allocated for the structure */
	saveimage = diskobj->do_Gadget.SelectRender;
	saveflags = diskobj->do_Gadget.Flags;

	/* point the alternate image of the modified icon to that of the */
	/* second icon */
	diskobj->do_Gadget.SelectRender = diskobja->do_Gadget.GadgetRender;

	/* modify the flags so that it used the alternate image */
	diskobj->do_Gadget.Flags = (saveflags & ~GADGHIGHBITS) | GADGHIMAGE;

	/* now write the fixed icon to disk */
	if (!PutDiskObject( argv[1], diskobj))
		printf("Cannot write new icon - code %d\n", IoErr() );


	/* restore the original icon to what we read in */
	diskobj->do_Gadget.SelectRender = saveimage;
	diskobj->do_Gadget.Flags = saveflags;

	/* and send it and the other object away */
	FreeDiskObject( diskobj );
	FreeDiskObject( diskobja );
	CloseLibrary( IconBase );
}
