/* IconTool 1.1 by Dominic Clifton */

#include <exec/exec.h>
#include <dos/dos.h>
#include <workbench/workbench.h>
#include <workbench/startup.h>   /* This has WBStartup and WBArg structs    */
#include <clib/exec_protos.h>
#include <clib/icon_protos.h>
#include <clib/dos_protos.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

long __stack=16384;

char *versionstring="$VER: IconTool 1.1 (28.6.1999)";

struct Library *IconBase;
struct DiskObject *AnIcon=NULL;

char *usagetemplate="FILENAME/M,UNSNAPSHOT/S,INFO/S,QUIET/S,TYPE/K";

struct MyArgs
{
  STRPTR *FileName;
  LONG UnSnapshot;
  LONG Info;
  LONG Quiet;
  STRPTR Type;
};

#define ICONTYPES 8

char *IconTypeName[ICONTYPES]={"DISK","DRAWER","TOOL","PROJECT","GARBAGE","DEVICE","KICK","APPICON"};

void Usage( void )
{
  printf("IconTool V1.1 by Dominic Clifton\n"
         "%s\n\n"
         "Set TYPE to one of the following:\n"
         "DISK,DRAWER,TOOL,PROJECT,GARBAGE,DEVICE,KICK or APPICON\n\n",usagetemplate);
}

void SaveIcon(struct DiskObject *AnIcon,char *FileName,LONG Quiet)
{
  if (PutDiskObject(FileName,AnIcon))
  {
    if (!Quiet) printf("Wrote icon (%s)\n",FileName);
  }                         
  else
  {
    if (!Quiet) printf("Unable to write icon (%s)\n",FileName);
  }
}

int main(int argc, char **argv)
{

  char *FileName;

  int FileCount = 0,Loop;
  struct RDArgs *myrdargs;
  struct MyArgs args;

  UBYTE NewType = 0;

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

    memset(&args,0,sizeof(struct MyArgs));

    if (myrdargs = ReadArgs(usagetemplate,(LONG *) &args,NULL))
    {

      // Match the type specified to one from our list

      if (args.Type)
      {
        for (Loop = 0; Loop < ICONTYPES; Loop ++)
        {

          if (stricmp(args.Type,IconTypeName[Loop]) == 0)
          {
            // Set the ACTUAL type that WorkBench uses. (see include:workbench/workbench.h)

            NewType = Loop + 1;
          }
        }
      }

      // Check paramaters

      if (!(args.UnSnapshot || args.Info || (args.Type && NewType)))
      {
        Usage();

        if (args.Type && !NewType)
        {
          puts("Please specify a valid TYPE");
        }
        else
        {
          puts("Specify either UNSHAPSHOT, INFO or TYPE");
        }

      }
      else
      {
        if (!args.FileName)
        {
          Usage();
          puts("Please specify a file name (without the .info)");
        }
        else
        {
          while (args.FileName[FileCount++]); /* find the last item, FileCount will tell you how many "FileName" items + 1 there were */

          FileCount--; /* skip back past the array terminator */

          for ( Loop = 0 ; Loop < FileCount ; Loop ++ )
          {
            FileName = args.FileName[Loop];


            if (AnIcon = GetDiskObject( FileName ))
            {
              if (!args.Quiet) printf("Got icon (%s)\n",FileName);

              if (args.Info)
              {
                printf("Filename: %s.info\n",FileName);
                
                printf("Type is currently %s\n",(AnIcon->do_Type >0 && AnIcon->do_Type <ICONTYPES) ? IconTypeName[AnIcon->do_Type - 1] : "Unknown!");

                if (AnIcon->do_CurrentX == 0x80000000)
                {    
                  puts("Icon is un-snapshotted");
                }
                else
                {
                  printf("Snapshot position: X = %ld Y = %ld\n",AnIcon->do_CurrentX,AnIcon->do_CurrentY);
                }
                
                printf("Icon Image Info: X=%ld Y=%ld W=%ld H=%ld\n",AnIcon->do_Gadget.LeftEdge,AnIcon->do_Gadget.TopEdge,AnIcon->do_Gadget.Width,AnIcon->do_Gadget.Height);
              }

              if (NewType)
              {
                if (AnIcon->do_Type == NewType)
                {
                  printf("Icon Type is already set to %s\n",(AnIcon->do_Type >0 && AnIcon->do_Type <ICONTYPES) ? IconTypeName[AnIcon->do_Type - 1] : "Unknown!");
                }
                else
                {
                  AnIcon->do_Type = NewType;

                  if (!args.Quiet) printf("Type is now %s\n",IconTypeName[NewType - 1]);

                  if (!args.UnSnapshot) // so we don't write the .info file out twice!
                  {
                    SaveIcon(AnIcon,FileName,args.Quiet);
                  }
                }
              }


              if (args.UnSnapshot)
              {
                AnIcon->do_CurrentX = AnIcon->do_CurrentY = 0x80000000;

								SaveIcon(AnIcon,FileName,args.Quiet);
              }

              FreeDiskObject(AnIcon);
            }
            else
            {
              if (!args.Quiet) printf("Failed to open icon (%s)\n",FileName);
            }
          } // end of for() loop

          if (!args.Quiet) printf("Done\n");
        }
      }
    }
    CloseLibrary(IconBase);
  }
}


