#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:  WMAX x y [window [screen]]\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 x,y;
   struct Window *theWindow;
   struct Screen *theScreen;

   if (argc < 3 || argc > 5) ShowUsage();
   GetInt(&x,argv[1]);
   GetInt(&y,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];
   
   IntuitionBase = OpenLibrary("intuition.library",INTUITION_REV);
   if (IntuitionBase)
   {
      Forbid();
      theScreen = FindScreen(ScreenName);
      theWindow = FindWindow(WindowName,theScreen);
      Permit();

      printf("\nWindow '%s' on Screen '%s'\n",
         theWindow->Title,theWindow->WScreen->Title);
      printf("   Old Max:  (%d,%d)    Old Min:  (%d,%d)\n",
         theWindow->MaxWidth,theWindow->MaxHeight,
         theWindow->MinWidth,theWindow->MinHeight);
      if (x || y)
      {
         Forbid();
         theWindow->MaxWidth  = x;
         theWindow->MaxHeight = y;
         Permit();
         printf("   New Max:  (%d,%d)\n\n",
            theWindow->MaxWidth,theWindow->MaxHeight);
      }
      CloseLibrary(IntuitionBase);
   }
}
