/*
 *  WOPENON     A companion utility to wIconify that allows you to
 *              specify where windows that are destined for the
 *              Workbench screen should be openned.
 *
 *  Copyright 1990 by Davide P. Cervone, all rights reserved.
 *  You may use this code, provided this copyright notice is kept intact.
 */


#include <exec/types.h>
#include "wIcon.h"

#define USAGE\
   "wOpenOn [ACTIVE_SCREEN | CURRENT_WB | REAL_WB] [[NO]RESIZE]"

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


#define ACTIVESCRN      1
#define CURRENTWB       2
#define REALWB          3
#define NOCHANGE        99

static int ScreenType = NOCHANGE;
static int Resize     = NOCHANGE;


#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,
 *    Move to the next one
 *    If it matches something we're looking for, then set the proper variable.
 *    Otherwise, report an error.
 *  Return TRUE if all parameters were OK and at least one was given,
 *    and return FALSE otherwise.
 */

static int ParseArguments(argc,argv)
int argc;
char **argv;
{
   int status = TRUE;
   
   while (--argc)
   {
      argv++;
      if (ARGMATCH("ACTIVE_SCREEN")) ScreenType = ACTIVESCRN; else
      if (ARGMATCH("CURRENT_WB"))    ScreenType = CURRENTWB; else
      if (ARGMATCH("REAL_WB"))       ScreenType = REALWB; else
      if (ARGMATCH("RESIZE"))        Resize     = TRUE; else
      if (ARGMATCH("NORESIZE"))      Resize     = FALSE;
      else
      {
         Print("Unrecognized flag '");
         Print(*argv);
         Print("'\n");
         status = FALSE;
      }
   }
   if (ScreenType == NOCHANGE && Resize == NOCHANGE) status = FALSE;
   return(status);
}


/*
 *  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)
char *s,*x1;
{
   Print(s);
   if (x1) Print(x1);
   Print("\n");
   _exit(10L);
}


/*
 *  main()
 *
 *  If all the arguments are OK,
 *    If wIconify is running, set the OpenOn menus appropriately,
 *    Otherwise give an error.
 *  Otherwise show the Usage message.
 */

void main(argc,argv)
int argc;
char **argv;
{
   if (ParseArguments(argc,argv))
   {
      if (wIconifyActive()) wSetOpenOn(ScreenType,Resize);
        else Error("wIconify not running or incompatible version",NULL);
   } else Error("Usage:  ",USAGE);
}
