/* Tool.c - a quick way to set tool types
 *
 *  Ver - 1.0a - Steven Allen McClain  Jun-25-88
 */

    #define PROGRAM "Tool for Changing Icon ToolTypes From CLI"
    #define VERSION "Version 1.0e Aug-9-88 Steven Allen McClain"

    #include <workbench/workbench.h>
    #include <workbench/icon.h>

    #define NIL 0L
    #define MAX_TOOLS 40
    #define NAME_SIZE 50

    #undef TRUE
    #undef FALSE
    #define TRUE 1
    #define FALSE 0

  void *IconBase=NIL, *OpenLibrary(); 

    int not_quiet= TRUE;
    int first_time_printed= TRUE;

    int changed= FALSE;
    int running= TRUE;

main(argc, argv)
  int argc; char *argv[];
{
    struct WBObject *wbobject;
    char *tools[MAX_TOOLS];
    int arg;
    int count= 0, found;
    char *ptr, *ptr2;
    char *new_tool;
    char filename[NAME_SIZE];

    if (argc == 1 || (argc == 2 && *argv[1] == '?'))
    {
      First_Time_Printed();
      printf("\
Usage: Tool <InfoFile> .............. (to list tools, default, and stacksize)\n\
    or      <InfoFile> <-q> <ToolType1> <ToolType2> ...... (to set new tools)\n\
\n\
    or      <InfoFile> <-q> -s<New_StackSize> ........... (to set stack size)\n\
    or      <InfoFile> <-q> -d<New_Default> ....... (to set new default tool)\n\
    or      <InfoFile> <-q> -w<ToolWindow> ......... (to set new tool window)\n\
\n\
    or      <InfoFile> <-q> -x<New_Xoffset> ........... (to set new x offset)\n\
    or      <InfoFile> <-q> -y<New_Yoffset> ........... (to set new y offset)\n\
    or      <InfoFile> <-q> -u<x|y> ....................... (to 'unsnapshot')\n\
    or      <InfoFile> <-q> -r ......................... (to reset tooltypes)\n\
\n");
      exit(1);
    }

    if ( (IconBase = OpenLibrary("icon.library", 0L)) == NIL) 
    {
      First_Time_Printed();
      printf("[4mCan't Open icon.library[0m\n\n");
      exit (2);
    }

    count = NAME_SIZE;  ptr= argv[1];  ptr2= filename; 
    while (*ptr && *ptr != '.' && --count)
    {
      *ptr2++ = *ptr++;
    }
    *ptr2 = 0;
    count = 0;
    
    if ( (wbobject = GetWBObject(filename)) == NIL )
    {
      First_Time_Printed();
      printf("[4mCan't Open `%s.info'[0m\n\n", filename);
      exit (3);
    }

    arg = 1;    /* look at first - should be info file name */

    if (argc == 2)    /* Just List info */
    {
      First_Time_Printed();
      printf("Info file is\t`%s.info'\nDefault Tool is `%s'\nStackSize is\t`%ld'\n",
                  filename, wbobject->wo_DefaultTool, wbobject->wo_StackSize);

      printf("ToolWindow is\t`%s'\n", wbobject->wo_ToolWindow);

      if (wbobject->wo_CurrentX == NO_ICON_POSITION)
        printf("Current X is \tANYWHERE\n", wbobject->wo_CurrentX);
      else
        printf("Current X is \t`%ld'\n", wbobject->wo_CurrentX);

      if (wbobject->wo_CurrentY == NO_ICON_POSITION)
	printf("Current Y is \tANYWHERE\n", wbobject->wo_CurrentY);
      else
	printf("Current Y is \t`%ld'\n", wbobject->wo_CurrentY);

/*
      printf("Name X is \t`%d'\nName Y is \t`%d'\n",
                  wbobject->wo_NameYOffset, wbobject->wo_NameYOffset);
*/
      count = 0;
      while(ptr= wbobject->wo_ToolTypes[count])
        printf("\n  ToolType %d is\t`%s'", ++count, ptr);

      if (not_quiet)
        printf("\n\n");
    }
    else
    {
      int tt=0;
      int size= 0;

      ++arg;    /* arg number 2 */

      /* Copy tooltypes to local array */
      while( ptr= wbobject->wo_ToolTypes[tt++])
          tools[count++]= ptr;

      while ( running && arg < argc && count < MAX_TOOLS )
      {
        ptr = argv[arg++];

        if ( *ptr == '-' )
        {
          switch ( toupper(*++ptr) )
          {
            case 'S' :  /* Set new stack */
            {
              Set_LongNumber(++ptr, &wbobject->wo_StackSize, "StackSize");
              break;
            }
            case 'D' :    /* Set new Default tool */
            {
              Set_String(++ptr, &wbobject->wo_DefaultTool, "Default Tool");
              break;
            }
            case 'W' :    /* Set new tool window */
            {
              Set_String(++ptr, &wbobject->wo_ToolWindow, "Tool Window");
              break;
            }
            case 'X' :  /* Set new x offset */
            {
              Set_LongNumber(++ptr, &wbobject->wo_CurrentX, "Current X");
              break;
            }
            case 'Y' :  /* Set new x offset */
            {
              Set_LongNumber(++ptr, &wbobject->wo_CurrentY, "Current Y");
              break;
            }
            case 'U' :  /* unsnapshot */
            {
              int setx=FALSE, sety=FALSE, both= TRUE;

              while (*++ptr)
              {
                if (toupper(*ptr) == 'X')
                  setx= TRUE;
                else if (toupper(*ptr) == 'Y')
                  sety= TRUE;
                both= FALSE;
              }

              if (!setx && !sety && !both)
              {
                not_quiet= TRUE;
                First_Time_Printed();
                printf("[4mOnly `X' and or `Y' after -U[0m\n");
                running = FALSE;
                changed = FALSE;
              }
              else
              {
                if (setx || both)
                  Set_LongNumber("-2147483648", &wbobject->wo_CurrentX, "Current X");

                if (sety || both)
                  Set_LongNumber("-2147483648", &wbobject->wo_CurrentY, "Current Y");
              }

              break;
            }
            case 'N' :  /* Set new Name x offset */
            {
              Set_Number(++ptr, &wbobject->wo_NameXOffset, "Name X");
              break;
            }
            case 'M' :  /* Set new Name x offset */
            {
              Set_Number(++ptr, &wbobject->wo_NameYOffset, "Name Y");
              break;
            }
	    case 'R':   /* Reset tool type array */
	    {
	      count = 0;
	      tools[count]= NIL;
              changed = TRUE;

	      if (not_quiet)
	      {
	         First_Time_Printed();
		printf("  ToolType array has been cleared!\n");
	      }
	    }
            case 'Q':
            {
                not_quiet= FALSE;
                break;
            }
            default:
            {
              not_quiet = TRUE;
              First_Time_Printed();
              printf("[4m\nUnknown switch: `%c'\n\n[0m", *ptr);
              running= FALSE;
              changed = FALSE;
            }
          }
          continue;
        }

        if ( size = Extract_Name(ptr) )
        {
	    new_tool = ptr;
	    tt = 0;
	    found= FALSE;
            First_Time_Printed();

	    if ( strpos(ptr, '=') )
	    {
                while( ptr= tools[tt])
                {
                  if (strncmp(ptr, new_tool, size) == 0)
		  {
		    if (strcmp(ptr, new_tool))
		    {
                      if (not_quiet)
                        printf("ToolType Was  \t`%s'\n", ptr);

                      tools[tt]= new_tool;

                      if (not_quiet)
                        printf("ToolType set to\t`%s'\n\n", new_tool);

                      changed= TRUE;
		    }
                    found=TRUE;
                    break;
		  }
		  ++tt;
		}

		if (!found)
		{
                    if (not_quiet)
                      printf("Added new tool\t`%s'\n", new_tool);

		    tools[count++] = new_tool;
                    changed= TRUE;
		}
	    }
	    else
	    {
                while( ptr= tools[tt])
                {
                  if (strncmp(ptr, new_tool, size) == 0)
		  {
                    if (not_quiet)
                      printf("Deleting tool \t`%s'\n", ptr);

		    found=TRUE;
                    changed= TRUE;
		  }
		  if (found)
                    tools[tt]= tools[tt+1];

		  ++tt;
		}
	    }
        }
      }

      if (count >= MAX_TOOLS)
      {
        not_quiet= TRUE;
        First_Time_Printed();
        printf("Only 40 tools eh!\n");
        changed = FALSE;
      }
      else
      {
        tools[count]= NIL;

        if (count > 0)
        {
          wbobject->wo_ToolTypes= tools;
        }

        if (!changed)
        {
           if (not_quiet)
             printf("Nothing Changed.\n");
        }
        else if ( !PutWBObject(filename, wbobject) )
        {
           First_Time_Printed();
           printf("[4mCan't Save `%s.info'[0m\n\n", filename);
           exit (3);
        }
      }
    }
    FreeWBObject(wbobject);

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

    if (not_quiet)
      printf("Done.\n\n");

    exit(0);
}

Extract_Name(tooltype)
  char *tooltype;
{
    register int count =0;
    register char *ptr= tooltype;
    register char ch;

    while ( (ch = toupper(*ptr)) && (ch != '='))
    {
      *ptr++ = ch;
      ++count;
    }

    return count;
}

First_Time_Printed()
{
    if (first_time_printed && not_quiet)
    {
      printf("\n%s\n%s\n\n", PROGRAM, VERSION);
      first_time_printed=FALSE;
    }
}

ucstrneq(str1, str2)
    char *str1, *str2;
{
    while (1)
    {
      if (!*str1 && !*str2)
          return TRUE;

      if ( toupper(*str1++) != toupper(*str2++) )
          return FALSE;
    }
}


/* Set_LongNumber - parse a long number out of a string and place results
 *                  at destination
 *        
 *        Depends on globals: not_quiet
 *          sets changed global.
 */
Set_LongNumber(Src_StrPtr, Dest_LongPtr, Desc_StrPtr)
    char *Src_StrPtr;    /* Pointer to source string */
    long *Dest_LongPtr;  /* Pointer to destination */
    char *Desc_StrPtr;   /* Pointer to description string */
{
      long number;

      if (not_quiet)
         First_Time_Printed();

      if ( (*Src_StrPtr) && sscanf(Src_StrPtr, "%ld", &number) 
                   && (number != *Dest_LongPtr) )
      {
         if (not_quiet)
            if (*Dest_LongPtr == NO_ICON_POSITION)
              printf("%s Was      \tNOT FIXED\n", Desc_StrPtr, *Dest_LongPtr);
            else
              printf("%s Was      \t`%ld'\n", Desc_StrPtr, *Dest_LongPtr);

         *Dest_LongPtr = number;
         changed= TRUE;

         if (not_quiet)
            if (*Dest_LongPtr == NO_ICON_POSITION)
              printf("%s Set To    \tANYWHERE\n\n", Desc_StrPtr, *Dest_LongPtr);
            else
              printf("%s Set To    \t`%ld'\n\n", Desc_StrPtr, *Dest_LongPtr);
       }
       else if (not_quiet)
            if (*Dest_LongPtr == NO_ICON_POSITION)
              printf("..%s is    \tANYWHERE\n", Desc_StrPtr, *Dest_LongPtr);
            else
              printf("..%s is    \t`%ld'\n", Desc_StrPtr, *Dest_LongPtr);
}

/* Set_Number - parse a number out of a string and place results
 *                  at destination
 *        
 *        Depends on globals: not_quiet
 *          sets changed global.
 */
Set_Number(Src_StrPtr, Dest_IntPtr, Desc_StrPtr)
    char *Src_StrPtr;    /* Pointer to source string */
    int *Dest_IntPtr;  /* Pointer to destination */
    char *Desc_StrPtr;   /* Pointer to description string */
{
      int number;

      if (not_quiet)
         First_Time_Printed();

      if ( (*Src_StrPtr) && sscanf(Src_StrPtr, "%d", &number) 
                   && (number != *Dest_IntPtr) )
      {
         if (not_quiet)
            printf("%s Was      \t`%d'\n", Desc_StrPtr, *Dest_IntPtr);

         *Dest_IntPtr = number;
         changed= TRUE;

         if (not_quiet)
            printf("%s Set To    \t`%d'\n\n", Desc_StrPtr, *Dest_IntPtr);
       }
       else if (not_quiet)
            printf("..%s is    \t`%d'\n", Desc_StrPtr, *Dest_IntPtr);
}

/* Set_String - 
 *        
 *        Depends on globals: not_quiet
 *          sets changed global.
 */
Set_String(Src_StrPtr, Dest_StrPtrPtr, Desc_StrPtr)
    char *Src_StrPtr;    /* Pointer to source string */
    char **Dest_StrPtrPtr;  /* Pointer to destination */
    char *Desc_StrPtr;   /* Pointer to description string */
{
      if (not_quiet)
         First_Time_Printed();

      if ( (*Src_StrPtr) && !ucstrneq(Src_StrPtr, *Dest_StrPtrPtr) )
      {
         if (not_quiet)
            printf("%s Was      \t`%s'\n", Desc_StrPtr, *Dest_StrPtrPtr);

         *Dest_StrPtrPtr = Src_StrPtr;
         changed= TRUE;

         if (not_quiet)
            printf("%s Set To    \t`%s'\n\n", Desc_StrPtr, *Dest_StrPtrPtr);
       }
       else if (not_quiet)
            printf("..%s is    \t`%s'\n", Desc_StrPtr, *Dest_StrPtrPtr);
}

int strpos(str, match)
	char *str, match;
{
	int pos = 0;
	char *ptr;

	ptr = str;
	while(*ptr)
	{
		if (*ptr == match)
		{
			pos = ptr-str;
			break;
		}
		++ptr;
	}
	return pos;
}

