/* dietaid -- a diet planning aid */
/* copywrited June 1986 by Terry W. Gintz */

#include "dietaid.h"
#include "ingredients.c"
#include "dietpad.c"

struct   GfxBase       *GfxBase;
struct   IntuitionBase *IntuitionBase;

FILE *console;
int used_resource = NULL;
extern void addtorecipe(),clearlist(),cutoff(),divide();
extern void loadrecipe(),saverecipe(),showdata();
extern short int saved;

main()
{
   FILE *fopen();
   char req[31];

   GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
   if (GfxBase == NULL)
      cleanup("Can't open graphics.library");
   used_resource |= L_GRAPHICS;
 
   IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0);
   if (IntuitionBase == NULL)
      cleanup("Can't open intuition.library");
   used_resource |= L_INTUITION;

   console = fopen("con:0/0/640/200/DietAid 1.0","w+");
   if (console == NULL)
      cleanup("Can't open console window");
   used_resource |= W_CONSOLE;

   help();

   for (;;) {

      fprintf(console,"What is your request? ");
      rewind(console);
      fgets(req,30,console);
      rewind(console);
      switch (toupper(req[0])) {
         case 'A':
            fprintf(console,"\f");
            addtorecipe();
            break;
         case 'N':
            fprintf(console,"\f");
            newrecipe();
            break;
         case 'C':
            fprintf(console,"\f");
            cutoff();
            break;
         case 'D':
            fprintf(console,"\f");
            divide();
            break;
         case 'L':
            fprintf(console,"\f");
            loadrecipe();
            break;
         case 'Q':
            if (NOT saved)
               saveoption();
            cleanup("Dieting can be creative!");
            break;
         case 'R':
            showdata();
            break;
         case 'S':
            fprintf(console,"\f");
            saverecipe();
            break;
         case 'T':
            fprintf(console,"\f");
            showtable();
            break;
         default:
            help();
            break;
      }
   }
}

help()
{
            fprintf(console,"\f\n\n\n");
            fprintf(console,"\t\t         Available Commands\n");
            fprintf(console,"\t\t------------------------------------\n");
            fprintf(console,"\t\t 'A' to add ingredients to recipe\n");
            fprintf(console,"\t\t 'C' to cut ingredients from recipe\n");
            fprintf(console,"\t\t 'D' to divide recipe into portions\n");
            fprintf(console,"\t\t 'L' to load recipe from diskette\n");
            fprintf(console,"\t\t 'N' to enter new recipe into memory\n");
            fprintf(console,"\t\t 'Q' to quit program\n");
            fprintf(console,"\t\t 'R' to review recipe in memory\n");
            fprintf(console,"\t\t 'S' to save recipe to diskette\n");
            fprintf(console,"\t\t 'T' to show ingredient table\n");
            fprintf(console,"\t\t '?' to display this list\n");
            fprintf(console,"\n");
            fprintf(console,"\t\t\t");
}

cleanup(s)
char *s;
{
   if (used_resource & W_CONSOLE)
      fclose(console);
   if (used_resource & L_GRAPHICS)
      CloseLibrary(GfxBase);
   if (used_resource & L_INTUITION)
      CloseLibrary(IntuitionBase);
   printf("%s\n",s);
   exit(0);
}
