	/***************************************************************
	 *                    CLIsizer v1.0  03/89                     *
	 *                    by Torsten Jürgeleit                     *
	 *                   for Manx Aztec C v3.6a                    *
	 * with   : cc clisizer.c                                      *
	 *          ln clisizer.o detach.o -lc                         *
	 * Syntax : clisizer xoffset yoffset width height [batch file] *
	 *          endcli                                             *
	 *     or : clisizer [batch file]                              *
	 *          endcli                                             *
	 ***************************************************************/

	/* Includes */

#include <exec/exec.h>
#include <libraries/dos.h>
#include <graphics/gfxbase.h>
#include <intuition/intuition.h>
#define INTUITIONPRIVATE
#include <intuition/intuitionbase.h>
#include <functions.h>

	/* Defines */

#define MAX_RETRIES		100	/* num of retries for CloseWorkbench() -> needs EndCLI before */
#define DOS_COMMAND_LEN		200

	/* Declarations for DETACH (segment list splitting) -> EndCLI possible */

LONG _stack        = 0x1000;
LONG _priority     = 0;
LONG _BackGroundIO = 0;
BYTE *_procname    = "CLIsizer1.0";

	/* Globals */

struct GfxBase		*GfxBase;
struct IntuitionBase	*IntuitionBase;

UBYTE dos_command[DOS_COMMAND_LEN];

	/* Dummy functions - not used from c.lib */

VOID _wb_parse() {}

	/* Main */

   VOID
main(argc, argv)
   USHORT argc;
   UBYTE  *argv[];
{
   struct Preferences	*prefs;
   struct FileHandle	*file_handle;
   USHORT xoffset, yoffset, width, height, i;
   UBYTE  *file_name = NULL;
   ULONG  ilock;
   BOOL   success = FALSE;

   if (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0L)) {
      if (IntuitionBase = (struct IntuitionBase *)
				     OpenLibrary("intuition.library", 0L)) {
	 if (prefs = (struct Preferences *)AllocMem((ULONG)
		    sizeof(struct Preferences), MEMF_PUBLIC | MEMF_CLEAR)) {
	    GetPrefs(prefs, (ULONG)sizeof(struct Preferences));
	    if (file_handle = Open("NIL:", MODE_NEWFILE)) {

	       /* needs 0, 1, 4 or 5 command line arguments */

	       if (argc > 2 && argc < 5 || argc > 6) {
		  DisplayBeep(NULL);
	       } else {
		  if (argc >= 5) {

		     /* get new dimensions */

		     xoffset = atoi(argv[1]);
		     yoffset = atoi(argv[2]);
		     width   = atoi(argv[3]);
		     height  = atoi(argv[4]);

		     /* get script file name */

		     if (argc == 6) {
			file_name = argv[5];
		     }
		  } else {

		     /* get default dimensions */

		     xoffset   = 0;
		     yoffset   = 0;
		     width     = GfxBase->NormalDisplayColumns;
		     height    = GfxBase->NormalDisplayRows;

		     /* get script file name */

		     if (argc == 2) {
			file_name = argv[1];
		     }
		  }
		  sprintf(&dos_command[0],
			       "NewCLI <nil: >nil: con:0/0/%d/%d/NewCLI %s",
/*
			       "NewWSH <nil: >nil: con:0/0/%d/%d/NewWSH %s",
*/
						  width, height, file_name);

		  /* wait for EndCLI to satisfy CloseWorkBench() */

		  for (i = 0; i < MAX_RETRIES && success == FALSE; i++) {
		     if ((success = CloseWorkBench()) == TRUE) {

			/* change Workbench and CLI dimensions */

			prefs->ViewXOffset      = xoffset;
			prefs->ViewYOffset      = yoffset;
			prefs->ColumnSizeChange = width -
					      GfxBase->NormalDisplayColumns;
			prefs->RowSizeChange    = height -
						 GfxBase->NormalDisplayRows;
			SetPrefs(prefs, (ULONG)sizeof(struct Preferences),
								     FALSE);
			ilock = LockIBase(0L);
			IntuitionBase->MaxDisplayWidth  = width;
			IntuitionBase->MaxDisplayHeight = 2 * height;
			IntuitionBase->MaxDisplayRow    = 2 * height - 1;
			UnlockIBase(ilock);
			OpenWorkBench();

			/* execute NewCLI command */

			Execute(&dos_command[0], file_handle, file_handle);
		     } else {
			Delay(20L);
		     }
		  }
		  if (success == FALSE) {
		     DisplayBeep(NULL);
		  }
	       }
	       Close(file_handle);
	    }
	    FreeMem(prefs, (ULONG) sizeof(struct Preferences));
	 }
	 CloseLibrary(IntuitionBase);
      }
      CloseLibrary(GfxBase);
   }
}
