/*
 * fonz - setfont for program that opens its own screen
 *
 * April 18, 1989
 * A hack to subtitute my own font for Handshake.
 * With a little more work, could be turn into a general purpose
 * setfont for program openning its own screen. I am too lazy to do it.
 *
 * April 19, 1989
 * Let's rename it "fonz".
 * Make necessary changes so that "fonz" can be used in general.
 * Usage: %s [-s font_size] [-w wait_max] font_name screen [window]
 *                     -s font_size: default to 8
 *                     -w wait_max: number of times to go around the wait loop
 *                     font_name: name of font
 *                     screen: screen name
 *                     window: name
 * With no argument:
 *     "fonz" printed out all the screens each followed by its windows
 *
 * If no window is specified, "fonz" will pick out the first NULL window
 *
 * April 20, 1989
 * Let's call thist Version 1.0
 * Bug found:
 *     Use the [-w] flag, "run" the application. For example,
 * runback c:fonz -w 12 clean Handshake; run df2:c/handshake
 * Now, do something in your CLI while the application is loaded.
 * Do a "status", for example, "fonz" will pretend that it does its job
 * and exist but no font is changed to the application's window.
 *
 * Fix:
 *     I don't know the reason for the bug. Attempt to fix by "delaying"
 * 10 secs before do SetFont(). Seems to work OK now.
 * If you know why please let me know.
 *
 * Hung Le (mott@ucscb.ucsc.edu)
 *  
 * compiled under Manx v3.4a (someday I will upgrade ... )
 * 1> cc fonz.c 
 * 1> ln fonz.o -lc 
 */

#include <intuition/intuitionbase.h>
#include <libraries/diskfont.h>
#include <graphics/rastport.h>
#include <graphics/text.h>
#include <functions.h>
#include <stdio.h>

#define INTUI_REV 0L
#define usage(n) fprintf(stderr,"Usage: %s [-s font_size] [-w wait_max] font_na
e screen [window]\n", n)
/* 500 ticks */
#define TEN_SECS 500L

struct IntuitionBase *IntuitionBase = 0L;
struct Window *Window = 0L;
struct TextFont *Font = 0L;
long DiskfontBase = 0L;
long GfxBase = 0L;

int Font_Size = 0;
int Wait_Max = 0;

/* not parsing workbench */
_wb_parse() {}

main(ac,av)
int ac;
char *av[];
{
       char my_name[20];

       strcpy(my_name, av[0]);

       /* Open the necessary libraries */
       Open_Libs();

       if (ac == 1)
               print_windows();
       else    
       {
               /* two global variables: Font_Size and Wait_Max are set */
               while (av[1][0] == '-')
               {
                       if (strcmp(av[1], "-s") == 0)
                       {
                               Font_Size = atoi(av[2]);
                               ac -= 2; av += 2;
                       }
                       else if (strcmp(av[1], "-w") == 0)
                       {
                               Wait_Max = atoi(av[2]);
                               ac -= 2; av += 2;
                       }
                       else
                       {
                               /* ignore bad flag */
                               ac--; av++;
                       }
               }

               /*
                * Make sure that at least the following two
                * variables are set to default values
                */
               if (Font_Size <= 0)
                       Font_Size = 8;
               if (Wait_Max < 0)
                       Wait_Max = 0;

               if ((ac == 3) || (ac == 4))
                       do_it(ac, av);
               else
               {
                       usage(my_name);
                       clean_up(1);
               }
       }

       /* Returns resouces to the Amiga */
       clean_up(0);
}

do_it(ac, av)
int ac;
char *av[];
{
       char font_name[20], screen_name[81], window_name[81];
       struct TextAttr ta;
       struct TextFont *old_font;
       struct RastPort *rast_port;

       /* set up the parameters */
       sprintf(font_name,"%s.font", av[1]);
       strcpy(screen_name, av[2]);
       if (ac == 4)
               strcpy(window_name, av[3]);
       else
               window_name[0] = '\0';

       /* my text attributes */
       ta.ta_Name = (UBYTE *) font_name;
       ta.ta_YSize = (long) Font_Size;
       ta.ta_Style = 0L;
       ta.ta_Flags = FPF_DISKFONT;

       Font = (struct TextFont *) OpenDiskFont(&ta);
       if (Font == (struct TextFont *) NULL)
       {
               fprintf(stderr,"Cannot find font \"%s %d\"\n", font_name, Font_S
ze);
               clean_up(2);
       }

       while (Wait_Max-- >= 0)
       {
               if (get_window(screen_name, window_name))
               /* Window is a global variable and it is set in get_window() */
               {
                       rast_port = Window->RPort;
                       old_font = rast_port->Font;
                       /* see heading comment box 04/20/89 */
                       if (Wait_Max >= 0) Delay(TEN_SECS);
                       if ( SetFont(rast_port, Font))
                               Font = old_font;        
                       break;
               }
               else  if (Wait_Max >= 0) Delay(TEN_SECS);
       }

       if (Window == (struct Window *) NULL)
       {
               fprintf(stderr,"Cannot find window \"%s\" in screen \"%s\"\n", w
ndow_name, screen_name);
               clean_up(2);
       }
}

get_window(sn, wn)
char *sn;
char *wn;
{

       struct Screen *screen;

       LockIBase(NULL);

       for (screen = IntuitionBase->FirstScreen; screen != (struct Screen *) NU
L; screen = screen->NextScreen)
       {
               if (strncmp(screen->Title, sn, strlen(sn)) == 0)
               {
                       for (Window = screen->FirstWindow; Window != (struct Win
ow *) NULL; Window = Window->NextWindow)
                       {
                               if (wn[0] == '\0')
                               {
                                       if (Window->Title == (UBYTE *) NULL)
                                       {
                                               UnlockIBase(NULL);
                                               return(TRUE);
                                       }
                               }
                               else if (strncmp(Window->Title, wn, strlen(wn)) 
= 0)
                               {
                                       UnlockIBase(NULL);
                                       return(TRUE);
                               }
                       }
               }
       }
       Window = (struct Window *) NULL;
       UnlockIBase(NULL);
       return(FALSE);
}

clean_up(code)
int code;
{
       if (Font) CloseFont(Font);
       if (DiskfontBase) CloseLibrary(DiskfontBase);
       if (IntuitionBase) CloseLibrary(IntuitionBase);
       if (GfxBase) CloseLibrary(GfxBase);
       exit(code);
}

Open_Libs()
{
       GfxBase = (long ) OpenLibrary("graphics.library", INTUI_REV);
       if (GfxBase == NULL)
       {
               fprintf(stderr,"Cannot open graphics.library\n");
               clean_up(1);
       }

       IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library"
 INTUI_REV);
       if (IntuitionBase == (struct IntuitionBase *) NULL)
       {
               fprintf(stderr,"Cannot open intuition.library\n");
               clean_up(1);
       }

       DiskfontBase = (long ) OpenLibrary("diskfont.library", INTUI_REV);
       if (DiskfontBase ==  NULL)
       {
               fprintf(stderr,"Cannot open diskfont.library\n");
               clean_up(1);
       }
}

print_windows()
{
       struct Window *window;
       struct Screen *screen;

       /* Should I lock the IntuitionBase? ... Nah ... */
       for (screen = IntuitionBase->FirstScreen; screen != (struct Screen *) NU
L; screen = screen->NextScreen)
       {
               printf("Screen: \"%s\"\n", screen->Title);
               for (window = screen->FirstWindow; window != (struct Window *) N
LL; window = window->NextWindow)
                       printf("Window: \"%s\"\n", window->Title);
       }
}

