#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:  WMOVE dx dy [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 dx,dy;
   struct Window *theWindow;
   struct Screen *theScreen;

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

      if (dx < -(theWindow->LeftEdge)) dx = -(theWindow->LeftEdge);
      if (dy < -(theWindow->TopEdge))  dy = -(theWindow->TopEdge);
      
      if (theWindow->LeftEdge + dx + theWindow->Width > theScreen->Width)
         dx = theScreen->Width - theWindow->Width - theWindow->LeftEdge;
      if (theWindow->TopEdge + dy + theWindow->Height > theScreen->Height)
         dy = theScreen->Height - theWindow->Height - theWindow->TopEdge;

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