/***************************************************************
Sit.c - Scritto da Stephen Vermeulen - Fissa il tipo di un'icona

         Il formato e': sit nomefile [tipo] [drawer]

Il comando lavora sul file "nomefile.info" modificando il tipo
e risalvandolo sotto lo stesso vecchio nome. I tipi sono:

1 --------------> DISK
2 --------------> DRAWER (Directory)
3 --------------> TOOL
4 --------------> PROJECT
5 --------------> GARBAGE
6 --------------> DEVICE (CHE COOOOSA ?????????!!!!!!!)
7 --------------> KICK

Traduzione ed adattamento per Aztec C V3.6 di Luigi R. Callegari
Programma di Pubblico Dominio. Non alterare le scritte. FTE Srl
****************************************************************/

#include <intuition/intuition.h>
#include <workbench/workbench.h>
#include <stdio.h>
#include <functions.h>

#define NO_ICONS       4

extern ULONG IconBase;

/************************************************************
 Qui apriamo semplicemente le librerie che ci occorrono
************************************************************/

short OpenLibs()
{
  short flags; /* per memorizzare librerie inapribili */

  flags = 0;
  IconBase = (ULONG) OpenLibrary("icon.library", 0L);
  if (!IconBase) flags |= NO_ICONS;
  return(flags);
}

void CloseLibs(flags)
short flags;
{
  if (!(flags & NO_ICONS))     CloseLibrary(IconBase);
}

void main(argc, argv)
short argc;
char *argv[];
{
  short lib_flags, icon_type;
  struct DiskObject *dobj, *drawer_obj;
  struct DrawerData *dd_temp;

  if (argc >= 3)
  {
    lib_flags  = OpenLibs();
    if (!lib_flags)
    {
      if (dobj = GetDiskObject(argv[1]))
      {
        sscanf(argv[2], "%d", &icon_type);
        if (icon_type < 1) icon_type = 1;
        if (icon_type > 7) icon_type = 7;
        if ((icon_type == 1) || (icon_type == 2) || (icon_type == 5))
        {

/* Qui si e' richiesto di rendere l'icona di tipo drawer, disk o trashcan,
quindi verifichiamo che l'originale sia proprio di questo tipo */

           if ((dobj->do_Type == 1) || (dobj->do_Type == 2) || (dobj->do_Type == 5))
           {
             /** ok a procedere **/
             dobj->do_Type = icon_type;
             PutDiskObject(argv[1], dobj);
           }
           else
           {
/* Dobbiamo leggere dei dati extra da una semplice drawer, disk o 
trashcan per potere modificare il file informativo appropriatamente */

             if (argc == 4)
             {
               /** corregge numero di parametri **/

               if (drawer_obj = GetDiskObject(argv[3]))
               {
                 if ((drawer_obj->do_Type == 1) ||
                     (drawer_obj->do_Type == 2) ||
                     (drawer_obj->do_Type == 5))
                 {
                   /** se e' il tipo di icona corretta **/

                   dd_temp = dobj->do_DrawerData;
                   dobj->do_DrawerData = drawer_obj->do_DrawerData;
                   dobj->do_Type = icon_type;
                   PutDiskObject(argv[1], dobj);
                   dobj->do_DrawerData = dd_temp;
                 }
                 else
                   printf("%s Deve essere DRAWER, DISK, o TRASHCAN\n", argv[3]);
                 FreeDiskObject(drawer_obj);
               }
             }
             else
               printf("Bisogna specificare anche una icona di drawer!\n");
           }
        }
        else
        {
	/* Se l'originale era di tipo DRAWER, eseguiamo il dump */

           dd_temp = dobj->do_DrawerData;
           dobj->do_DrawerData = NULL;
           dobj->do_Type = icon_type;
           PutDiskObject(argv[1], dobj);
           dobj->do_DrawerData = dd_temp;
        }
        FreeDiskObject(dobj);
      }
    }
    CloseLibs(lib_flags);
  }
  else if (argc == 2)
  {
    lib_flags  = OpenLibs();
    if (!lib_flags)
    {
      if (dobj = GetDiskObject(argv[1]))
      {
        printf("Tipo %d ", dobj->do_Type);
        switch(dobj->do_Type)
        {
          case 1:
            printf("WBDISK\n");
            break;
          case 2:
            printf("WBDRAWER\n");
            break;
          case 3:
            printf("WBTOOL\n");
            break;
          case 4:
            printf("WBPROJECT\n");
            break;
          case 5:
            printf("WBGARBAGE\n");
            break;
          case 6:
            printf("WBDEVICE\n");
            break;
          case 7:
            printf("WBKICK\n");
            break;
          deafult:
            printf("TIPO SCONOSCIUTO!\n");
        }
        FreeDiskObject(dobj);
      }
    }
    CloseLibs(lib_flags);
  }
  else
  {
    printf("Sintassi: sit icon [newtype] [drawer|disk|trashcan]\n");
    printf("newtype:  1 = disk     2 = drawer   3 = tool\n");
    printf("          4 = project  5 = garbage  6 = device  7 = kick\n");
    printf("Versione 1.10i\n");
    printf("Scritto da:      Stephen Vermeulen\n");
    printf("                 3635 Utah Dr. N.W.,\n");
    printf("                 Calgary, Alberta,\n");
    printf("                 CANADA, T2N 4A6\n");
    printf("\nMandate un contributo libero od un analisi dei bug!\n");
    printf("Software distribuito da EnigmA Amiga Disk\n");
  }
}

/******************************* FINE DEL FILE *************************/
