
/*
 *  INFORM how click-cmd "text" [-x file-to-delete-on-exit]
 *
 *  (C) Copyright 1989-1990 by Matthew Dillon,  All Rights Reserved.
 *
 *  INFORM mail - "You have new mail"
 *
 *  Brings up an unobtrusive window saying that you have new mail and waits
 *  for the user to hit the close box or double click the title bar.  If
 *  the title bar is double clicked
 *
 *  Multiple runs of the same command with the same 'how' field do not cause
 *  further requesters to come up.  Note that sendmail also has a lockout
 *  mechanism by way of a temporary file which is not deleted until
 *  inform exits (the -x option).
 */

#include <exec/types.h>
#include <exec/ports.h>
#include <intuition/intuition.h>

#include <stdio.h>
#include <stdlib.h>
#include "protos.h"
#include "config.h"
#include "version.h"

#define TBFI	0   /*	meaningless */

IDENT(".00");

struct Window	*Win;
struct MsgPort	*Port;

struct IntuitionBase *IntuitionBase;
struct GfxBase	     *GfxBase;

struct NewWindow Nw = {
    320, 0, TBFI, 10, -1, -1,
    CLOSEWINDOW|MOUSEBUTTONS,
    WINDOWCLOSE|BACKDROP|SIMPLE_REFRESH|RMBTRAP|NOCAREREFRESH,
    NULL, NULL, TBFI, NULL, NULL, 0, 0, -1, -1, WBENCHSCREEN
};

static int	newXPos = -1;	    /* MRR, 01-25-91 - user-spec. X coord. */

Local void xexit(int);
Local int WaitWinAction(void);

char *DelFile;

void
main(ac, av)
char *av[];
{
    static char pubname[64];

    {
	short i;
	for (i = 4; i < ac; ++i) {
	    char *ptr = av[i];
	    if (*ptr == '-') {
		ptr += 2;
		switch(ptr[-1]) {
		case 'x':
		    if (*ptr)
			DelFile = ptr;
		    else
			DelFile = av[++i];
		    break;
		case 'p':               /* MRR, 01-25-91 - position <x-coord> */
		    /* Form may be -p<x> or -p <x> */
		    if (! *ptr) ptr = av[++i];
		    newXPos = atoi(ptr); /* MRR - need range checking? */
		}
	    }
	}
    }

    if (ac < 3) {
	fprintf(stderr, "Inform id clickcmd text\n");
	exit(1);
    }
    sprintf(pubname, "Inform.%s", av[1]);
    LockFile("T:Inform");
    if (FindPort(pubname) == NULL)
	Port = CreatePort(pubname, 0);
    UnLockFile("T:Inform");
    if (Port) {
	IntuitionBase = OpenLibrary("intuition.library", 0);
	GfxBase       = OpenLibrary("graphics.library", 0);
	if (IntuitionBase && GfxBase) {
	    Nw.Width = strlen(av[3]) * 8 + 32;
	    Nw.Title = (UBYTE *)av[3];
	    if (newXPos >= 0) Nw.LeftEdge = newXPos; /* MRR, 01-25-91 */
	    if (Win = OpenWindow(&Nw)) {
		ShowTitle(Win->WScreen, 0);
		if (WaitWinAction()) {
		    Execute(av[2], NULL, NULL);
		}
	    }
	}
    }
    if (DelFile)
	remove(DelFile);
    xexit(0);
}

Local int
WaitWinAction()
{
    short click = 0;
    short notdone = 1;
    struct IntuiMessage *im;

    while (notdone) {
	WaitPort(Win->UserPort);
	while (im = (struct IntuiMessage *)GetMsg(Win->UserPort)) {
	    switch(im->Class) {
	    case MOUSEBUTTONS:
		if (im->Code == SELECTDOWN) {
		    click = 1;
		    notdone = 0;
		    break;
		}
		break;
	    case CLOSEWINDOW:
		notdone = 0;
		break;
	    }
	    ReplyMsg((struct Message *)im);
	}
    }
    CloseWindow(Win);
    Win = NULL;
    return((int)click);
}

Local void
xexit(code)
{
    if (Win)
	CloseWindow(Win);
    if (GfxBase)
	CloseLibrary(GfxBase);
    if (IntuitionBase)
	CloseLibrary(IntuitionBase);
    if (Port) {
	Forbid();
	DeletePort(Port);
	Permit();
    }
    exit(code);
}


