#include <exec/types.h>
#include <intuition/intuitionbase.h>
#include <intuition/screens.h>
#include <proto/intuition.h>
#include <proto/exec.h>

#define INTUITION_REV   0L
extern struct IntuitionBase *IntuitionBase;
extern struct IntuitionBase *OpenLibrary();


static void ShowUsage()
{
   printf("Usage:  WSIZE dx dy [window [screen [IGNOREMAX]]\n");
   exit(10L);
}


static void GetInt(i,s)
long *i;
char *s;
{
   if (sscanf(s,"%ld",i) != 1) ShowUsage();
}

static struct Screen *FindScreen(ScreenName)
char *ScreenName;
{
   struct Screen *theScreen;

   if (ScreenName && ScreenName[0])
   {
      theScreen = IntuitionBase->FirstScreen;
      while (theScreen && (theScreen->Title == NULL ||
             stricmp(theScreen->Title,ScreenName) != 0))
                theScreen = theScreen->NextScreen;
   } else {
      theScreen = IntuitionBase->ActiveScreen;
   }

   if (theScreen == NULL)
   {
      Permit();
      printf("Can't find screen '%s'\n",ScreenName);
      exit(10L);
   }
   return(theScreen);
}


static struct Window *FindWindow(WindowName,theScreen)
char *WindowName;
struct Screen *theScreen;
{
   struct Window *theWindow;

   if (WindowName && WindowName[0])
   {
      theWindow = theScreen->FirstWindow;
      while (theWindow && (theWindow->Title == NULL ||
             stricmp(theWindow->Title,WindowName) != 0))
                theWindow = theWindow->NextWindow;
   } else {
      theWindow = IntuitionBase->ActiveWindow;
   }

   if (theWindow == NULL)
   {
      Permit();
      printf("Can't find window '%s' on screen '%s'\n",
         WindowName,theScreen->Title);
      exit(10L);
   }
   Permit();
   return(theWindow);
}


void main(argc,argv)
int argc;
char *argv[];
{
   char *WindowName = NULL;
   char *ScreenName = NULL;
   long dx,dy;
   struct Window *theWindow;
   struct Screen *theScreen;
   long UseMax = TRUE;

   if (argc < 3 || argc > 6) ShowUsage();
   GetInt(&dx,argv[1]);
   GetInt(&dy,argv[2]);
   if (argc > 3 && argv[3] && argv[3][0] != '\0') WindowName = argv[3];
   if (argc > 4 && argv[4] && argv[4][0] != '\0') ScreenName = argv[4];
   if (argc > 5) UseMax = FALSE;
   
   IntuitionBase = OpenLibrary("intuition.library",INTUITION_REV);
   if (IntuitionBase)
   {
      Forbid();
      theScreen = FindScreen(ScreenName);
      theWindow = FindWindow(WindowName,theScreen);
      Permit();

      if (theWindow->LeftEdge + theWindow->Width + dx > theScreen->Width)
         dx = theScreen->Width - theWindow->LeftEdge - theWindow->Width;
      if (theWindow->Width + dx < theWindow->MinWidth)
         dx = theWindow->MinWidth - theWindow->Width;
      if (theWindow->Width + dx > theWindow->MaxWidth && UseMax)
         dx = theWindow->MaxWidth - theWindow->Width;

      if (theWindow->TopEdge + theWindow->Height + dy > theScreen->Height)
         dy = theScreen->Height - theWindow->TopEdge - theWindow->Height;
      if (theWindow->Height + dy < theWindow->MinHeight)
         dy = theWindow->MinHeight - theWindow->Height;
      if (theWindow->Height + dy > theWindow->MaxHeight && UseMax)
         dy = theWindow->MaxHeight - theWindow->Height;

      printf("\nWindow '%s' on Screen '%s'\n",
         theWindow->Title,theWindow->WScreen->Title);
      printf("   Old size:  (%d,%d)\n",theWindow->Width,theWindow->Height);
      if (dx || dy)
      {
         printf("   New size:  (%d,%d)\n   Changed:   (%d,%d)\n\n",
            theWindow->Width+dx,theWindow->Height+dy,dx,dy);
         SizeWindow(theWindow,dx,dy);
      }
      CloseLibrary(IntuitionBase);
   }
}
