/*
 *  WSETSYSREQUEST  A companion utility to wIconify that allows you to
 *                  change the screen where system requesters open
 *                  (designed for use with CLI's that are openned on
 *                  a screen other than the WB screen).
 *
 *  Copyright 1990 by Davide P. Cervone, all rights reserved.
 *  You may use this code, provided this copyright notice is kept intact.
 */

#define INTUITION_PREFERENCES_H             /* don't need 'em */
#include <intuition/intuitionbase.h>
#include <libraries/dosextens.h>

#define USAGE   "wSetSysRequest [process [screen]]"

static char *program = "wSetSysRequest";
static char *version = "v1.2";
static char *copyright =
   "Copyright (C) 1990 by Davide P. Cervone, all rights reserved.";


#define INTUITION_REV   0L
extern struct IntuitionBase *IntuitionBase;
extern struct IntuitionBase *OpenLibrary();
extern struct Process *FindTask();
extern APTR wBackDropOf();


#define TOUPPER(c)      (((c)>='a'&&(c)<='z')?(c)-'a'+'A':c)


/*
 *  PrefixMatch()
 *
 *  Case-insensitive prefix match.  NULL pointers and empty strings
 *  only match themselves, and are not considered prefixes to any string.
 *
 *  Return <0 if the first is smaller than the second,
 *         =0 if the first is a prefix of the second,
 *         >1 if the first is larger than the second.
 */

int PrefixMatch(s1,s2)
char *s1,*s2;
{
   int match = -1;
   
   if (s1 && *s1)
   {
      if (s2)
      {
         while (TOUPPER(*s1) == TOUPPER(*s2) && *s2 != 0) s1++,s2++;
         if (*s1 == 0) match = 0; else match = *s1 - *s2;
      } else match = 1;
   } else if (s2 == NULL || *s2 == 0) match = 0;
   return(match);
}


/*
 *  *FindScreen()
 *
 *  Lock IntuitionBase so nothing happens while we look.
 *  If there is a name to look for,
 *    Start at the first screen, and look for a screen whose title
 *      matches the given name.  If none, return NULL.
 *  Otherwise, use the active screen.
 *  Unlock Intuition.
 *  Return the screen pointer found, if any.
 */

static struct Screen *FindScreen(ScreenName)
char *ScreenName;
{
   struct Screen *theScreen;
   long ILock, LockIBase();

   ILock = LockIBase(0L);
   if (ScreenName)
   {
      theScreen = IntuitionBase->FirstScreen;
      while (theScreen && PrefixMatch(ScreenName,theScreen->Title))
         theScreen = theScreen->NextScreen;
   } else {
      theScreen = IntuitionBase->ActiveScreen;
   }
   UnlockIBase(ILock);
   return(theScreen);
}


/*
 *  Print()
 *
 *  Find the standard AmigaDOS output file and write the output string
 *  to the output file.  This is intended as a substitute for printf()
 *  when no formatting is required, and when you want a small executable.
 */

static void Print(s)
char *s;
{
   ULONG OutFile;
   extern ULONG Output();
   
   OutFile = Output();
   if (OutFile && s) Write(OutFile,s,strlen(s));
}


/*
 *  Error()
 *
 *  Print the character string, and up to two optional strings, then
 *  go to a new output line, close Intuition, and exit with an error.
 */

static void Error(s,x1,x2)
char *s,*x1,*x2;
{
   Print(s);
   if (x1) Print(x1);
   if (x2) Print(x2);
   Print("\n");
   if (IntuitionBase) CloseLibrary(IntuitionBase);
   _exit(10L);
}


/*
 *  main()
 *
 *  If there are too many arguments, give the Usage message.
 *  If there is at least one argument, make it the process name.
 *  If there are two arguements, make the second the screen name.
 *  Find the specified process (ourself, if ProcName is NULL).
 *  If found, then
 *    Open Intuition.
 *    Find the specified screen (the active one if NULL)
 *    If wIcoinfy is running, set the process window pointer to the
 *      backdrop window of the specified screen; system requests will
 *      appear on the screen where this window is found.
 *    Otherwise show an error message.
 *    Close Intuition.
 *  Otherwise show an error message.
 */

void main(argc,argv)
int argc;
char *argv[];
{
   char *ProcName = NULL;
   char *ScreenName = NULL;
   struct Process *theProcess;
   struct Screen *theScreen;

   if (argc > 3) Error("Usage:  ",USAGE,NULL);

   if (argc > 1 && argv[1]) ProcName = argv[1];
   if (argc > 2 && argv[2]) ScreenName = argv[2];

   theProcess = FindTask(ProcName);
   if (theProcess)
   {
      IntuitionBase = OpenLibrary("intuition.library",INTUITION_REV);
      if (IntuitionBase)
      {
         theScreen = FindScreen(ScreenName);
         if (theScreen)
         {
            if (wIconifyActive())
              theProcess->pr_WindowPtr = wBackDropOf(theScreen);
             else
              Error("wIconify not running or incompatible version",NULL,NULL);
         } else Error("Can't find screen '",ScreenName,"'");
         CloseLibrary(IntuitionBase);
      } else Error("Can't Open Intuition Library!",NULL,NULL);
   } else Error("Can't Find Task '",ProcName,"'");
}
