/*
 *  WICONSCREEN     A utility that works with WICONIFY to open a new
 *                  wIconify screen of the desired resolution and depth.
 *
 *  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/intuition.h>
#include "wIcon.h"

#define USAGE\
   "wIconScreen [depth] [LORES] [HIRES] [INTERLACE] [HAM]\n           \
                [[NO]ACTIVATE] [[NO]BEHIND]"

static char *program = "wIconScreen";
static char *version = "v1.3";
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 Screen *wNewScreen();

static UWORD Depth = 2;         /* The number of bitplanes for the screen */
static UWORD Modes = HIRES;     /* The desired resolution and type */
static UWORD Activate;          /* Activate the wIconify window? */
static UWORD Behind;            /* Open the screen behind the others? */

#define ARGMATCH(s)     (stricmp(*argv,s) == 0)


/*
 *  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));
}


/*
 *  ParseArguments()
 *
 *  While there are more arguments to view,
 *    Check if it equals something we are looking for, and if so,
 *      set or unset the proper flag.
 *    If it equals a number, then this is the depth, so check if the
 *      given depth is legal.
 *    If no match is found, the parameter is unknown, so warn the user.
 *  Return TRUE if all arguments matched, FALSE otherwise.
 */

static int ParseArguments(argc,argv)
int argc;
char **argv;
{
   int status = TRUE;
   ULONG depth;
   
   while (--argc)
   {
      argv++;
      if (ARGMATCH("HIRES"))      Modes |= HIRES; else
      if (ARGMATCH("LORES"))      Modes &= ~HIRES; else
      if (ARGMATCH("INTERLACE"))  Modes |= LACE; else
      if (ARGMATCH("HAM"))        Modes |= HAM; else
      if (ARGMATCH("ACTIVATE"))   Activate = TRUE; else
      if (ARGMATCH("NOACTIVATE")) Activate = FALSE; else
      if (ARGMATCH("BEHIND"))     Behind = TRUE; else
      if (ARGMATCH("NOBEHIND"))   Behind = FALSE; else
      if (sscanf(*argv,"%lu",&depth) == 1)
      {
         if (depth < 1) depth = 1;
         else if (depth > 5) depth = 5;
         Depth = depth;
      } else {
         Print("Unrecognized flag '");
         Print(*argv);
         Print("'\n");
         status = FALSE;
      }
   }
   return(status);
}


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

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


/*
 *  main()
 *
 *  First parse the argument list.
 *  If all are OK, then
 *    If wIconify is running,
 *      Open the Intuition library.
 *      If succcessful,
 *        Ask wIconify to open a new screen.
 *        If one is openned,
 *          If we are supposed to activate it, do so.
 *          If it should be behind, send it there.
 *        Otherwise, say that one can't be openned.
 *        Close Intuition.
 *      Otherwise say that Intuition can't be openned.
 *    Otherwise say that wIconify is not running.
 *  Otherwise, give the Usage message.
 */

void main(argc,argv)
int argc;
char **argv;
{
   struct Screen *theScreen;

   if (ParseArguments(argc,argv))
   {
      if (wIconifyActive())
      {
         IntuitionBase = OpenLibrary("intuition.library",INTUITION_REV);
         if (IntuitionBase)
         {
            theScreen = wNewScreen(Depth,Modes);
            if (theScreen)
            {
               if (Activate && theScreen->FirstWindow)
                   ActivateWindow(theScreen->FirstWindow);
               if (Behind) ScreenToBack(theScreen);
            } else Error("Can't open new Iconify screen",NULL);
            CloseLibrary(IntuitionBase);
         } else Error("Can't Open Intuition Library!",NULL);
      } else Error("wIconify not running or incompatible version",NULL);
   } else Error("Usage:  ",USAGE);
}
