/*
 *  This is a short program which will wait until a particular window
 *  is opened, or until a certain amount of time has passed.
 *
 *  Specify -m at the end to wait until the window has finished setting
 *  its menu structure.
 *
 *  Usage : until <window> <seconds> [ <-m> ]
 *
 *  NB: Window name is case sensitive; for multi-word names, only the first
 *      is checked, so if name is "My Window" use "My" when invoking "until".
 */

/*
 *  Use of this program? Anytime you run one application that opens windows,
 *  and then want to start another, but not until the first has opened its
 *  window (and optionally constructed a menulist). Could also be used to
 *  reduce disk grinding (hey, this is a neat idea I just thought of!):
 *
 *      run Scribble!       ;fine and dandy
 *      until Scribble! 20  ;wait till loaded, or 20 secs if load fails
 *      run Pagesetter      ;I'll imagine I have lots of ram
 *
 *  This works well as a companion to ADD, so you can construct script files
 *  that run a program like Scribble or Pagesetter, wait until they have
 *  finished loading, and then do an addbuffers and add some keyboard
 *  shortcuts:
 *
 *      run df1:vt100       ;start it up
 *      until VT100 20 -m   ;wait until it loads, gets ser: device, and
 *                          ;constructs menu
 *      addbuffers df0: 20
 *      add VT100 1 1 C     ;create keyboard shortcuts
 *      etc.
 *
 *             Copyright ((c)) 1987, John Russell. Unlimited distribution.
 */

#include <intuition/intuition.h>

struct IntuitionBase *IntuitionBase;  /* actually has a use! */
struct Window *findwindow();

main(argc, argv)
int argc;
char *argv[];
{
    struct Window *open;
    int seconds,x;

    if (!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0L))) {
        puts("No intuition library.\n");
        exit(0);
    }

    if ((argc < 3) || (argc > 4)) {      /* instructions */
        puts("Usage: until <window> <seconds> [ <-m> ]\n");
        CloseLibrary(IntuitionBase);
        exit(0);
    }

    seconds = atoi(argv[2]);

    for (x=0; x<2*seconds; x++) {   /* check every 1/2 second */
        if (open = findwindow(argv[1])) {   /* if window open yet */

            /* now is menu bar set (or doesn't user care)? */
            if (open->MenuStrip || (argc==3) || compare(argv[3],"-m")) {
                goto end;
            }
        }
        Delay(25);  /* half a second or so */
    }

    puts("Timeout - window did not open.");
    end:
    CloseLibrary(IntuitionBase);
}

struct Window *findwindow(title)
char *title;
{
    struct Screen *Screen;
    struct Window *Window;

    Forbid();   /* just to be safe, disable interrupts */

    /* now loop through all screens/windows */
    for (Screen=IntuitionBase->FirstScreen; Screen; Screen=Screen->NextScreen) {

        for (Window=Screen->FirstWindow; Window; Window=Window->NextWindow) {

            /* if found, return window pointer so we can check menu status */
            if (!compare(Window->Title,title)) {
                Permit();   /* what happens if I don't do this, hm? */
                return(Window);
            }
        }
    }
    Permit();
    return(0L);
}

compare(string1,string2)  /* compare strings that end with nulls or spaces */
char *string1,*string2;
{
    while ((*string1 != '\0') && (*string1 != ' ')) {
        if (*string1 != *string2)
            return(1);
        string1++;
        string2++;
    }
    return(0);  /* return reverse of intuitive value, as strcmp does */
}


