#include <stdio.h>
#include <exec/types.h>
#include "struct.h"
#include "plot.h"

extern int debug;

extern char *gets();
extern char *getwrd();

void GetHowTo(Pict)
struct Pict *Pict;
{
   short i;
   static short firstcall = TRUE;
   char s[100], *sp, *wp; /* STRING BUFFER, STRING POINTER, WORD POINTER */
   struct Plot *Plot;

   if (debug) {
      printf("GetHowTo: entry\n");
      printf("   Pict->ErrBar = %d\n",Pict->ErrBar);
      printf("   Pict->ShowErr = %d\n",Pict->ShowErr);
   }
   if (firstcall) {
      firstcall = FALSE;
      /*** ESTABLISH DEFAULTS ***/
      Pict->Grid = FALSE;
      Pict->ShowErr = Pict->ErrBar;
      Pict->Tics->NX = Pict->Tics->NY = 5;
   }

   /*** GET PLOTTING INSTRUCTIONS ***/
   printf("\x9b2m%d\x9bm data sets.\n",Pict->NPlt);
   printf("Enter \x9b2mhow_to string\x9bm (? for help) > ");
   sp = gets(s);
   while (*sp == '?') {
      printf("\n\tA \x9b2mhow_to STRING\x9bm is:\n");
      printf("\t  an optional \x9b2mswitch string\x9bm followed by one of:\n");
      printf("\t     1)  \x9b2mreturn\x9bm   default plot of all data sets        .OR.\n");
      printf("\t     2)    \x9b2m!p\x9bm     default point plot of all data sets  .OR.\n");
      printf("\t     3)  one or more \x9b2mhow_to\x9bm WORDs separated by spaces\n\n");
      printf("\tA \x9b2mswitch string\x9bm is \x9b2m/\x9bm followed by any of:\n");
      printf("\t     \x9b2md\x9bm(\x9b2mD\x9bm)   debug on (off)\n");
      printf("\t     \x9b2me\x9bm(\x9b2mE\x9bm)   error bars plotted (not plotted)\n");
      printf("\t     \x9b2mg\x9bm(\x9b2mG\x9bm)   grid drawn (not drawn) on plot\n");
      printf("\t     \x9b2mt#     #\x9bm divisions for tic marks or grid\n\n");
      printf("\tA \x9b2mhow_to WORD\x9bm is \x9b2m*\x9bm or \x9b2m-\x9bm or [\x9b2m#1\x9bm]\x9b2mt\x9bm[\x9b2m#2\x9bm]  where:\n");
      printf("\t     \x9b2m*\x9bm   gets default plot for a data set\n");
      printf("\t     \x9b2m-\x9bm   skips (does not plot) a data set\n");
      printf("\t     []  encloses an optional parameter\n");
      printf("\t     \x9b2m#1\x9bm  is a color number in [0..9] \n");
      printf("\t     \x9b2mt\x9bm   is one of \x9b2ml\x9bm (line) or \x9b2mp\x9bm (point)\n");
      printf("\t     \x9b2m#2\x9bm  is a point size in [1..10]\n\n");
      printf("\tEXAMPLE:  \x9b2m/gt5 - 0p4 1l - - 2p 3l\x9bm\n\n");
      printf("Enter \x9b2mhow_to string\x9bm (? for help) > ");
      sp = gets(s);
   }

   if (*sp=='/') {
      sp++;
      while ( (*sp) && (*sp != ' ') ) {
         switch (*sp++) {
            case 'd': debug = TRUE; printf("*** debug on ***\n"); break;
            case 'D': debug = FALSE; break;
            case 'g': Pict->Grid = TRUE; break;
            case 'G': Pict->Grid = FALSE; break;
            case 'e': Pict->ShowErr = Pict->ErrBar; break;
            case 'E': Pict->ShowErr = FALSE; break;
            case 't':
               Pict->Tics->NX = atoi(sp);
               Pict->Tics->NY = Pict->Tics->NX;
               sp++;
               break;
            default: break;
         }
      }
   }
   
   while (*sp==' ') sp++;
   if (!(*sp) || (*sp=='!')) {

      Plot = Pict->Plot;
      i = 0;
      while (Plot) {
         Plot->Enabled = TRUE;
         Plot->Color = PLOTCOLORBASE + i;
         if ((*sp=='!') && (sp[1]=='p')) {
            if (isdigit(sp[2]))
               Plot->PointSize = atoi(&sp[2]);
            else if (sp[2]=='-')
               Plot->PointSize = 
                     (isdigit(sp[3]) ? atoi(&sp[2]) : -DEFAULT_POINT_SIZE);
            else
               Plot->PointSize = DEFAULT_POINT_SIZE;
         }
         else
            Plot->PointSize = 0;

         while (Plot->Continued) {
            Plot->NextPlot->Color = Plot->Color;
            Plot->NextPlot->PointSize = Plot->PointSize;
            Plot->NextPlot->Enabled = Plot->Enabled;
            Plot = Plot->NextPlot;
         } 
         Plot = Plot->NextPlot;
         i++;
      }
   }
   
   else {
      /* INIT ALL PLOTS TO "DISABLED" */
      Plot = Pict->Plot;
      while (Plot) {Plot->Enabled = FALSE; Plot = Plot->NextPlot;}

      /* GET USER'S INSTRUCTIONS FOR EACH PLOT */
      Plot = Pict->Plot;
      i = 0;
      while( *(wp=getwrd(&sp)) && Plot) {
         if (*wp == '-')
            Plot->Enabled = FALSE;
         else {
            Plot->Enabled = TRUE;
            if (isdigit(*wp))
               {Plot->Color = PLOTCOLORBASE + atoi(wp); wp++;}
            else
               Plot->Color = PLOTCOLORBASE + i;

            if (*(wp++) == 'p') {
               if (isdigit(*wp))
                  Plot->PointSize = atoi(wp);
               else if (*wp=='-')
                  Plot->PointSize = 
                     (isdigit(wp[1]) ? atoi(wp) : -DEFAULT_POINT_SIZE);
               else
                  Plot->PointSize = DEFAULT_POINT_SIZE;
            }
            else
               Plot->PointSize = 0; /* GETS LINE PLOT */
         }

         while (Plot->Continued) {
            Plot->NextPlot->Color = Plot->Color;
            Plot->NextPlot->PointSize = Plot->PointSize;
            Plot->NextPlot->Enabled = Plot->Enabled;
            Plot = Plot->NextPlot;
         } 
         Plot = Plot->NextPlot;
         i++;
      }
   }
   if (debug) printf("GetHowTo: exit\n");
}
