/***************************************************************************
*                    Copyright (c) 1986, Tim Mooney                        *
*                                                                          *
*      This software is released into the public domain.  You may freely   *
*   use, copy, modify, and redistribute it if this notice is retained.     *
*   You may not sell it, but you may charge a nominal copying/hassle fee   *
*   for making it available.                                               *
*                                                                          *
***************************************************************************/

#define MAIN_MODULE 1
#include "tdp.h"

#define NAMELEN 30
char *strncpy(), line[80], *cb;
void trch();
int numeric();
FILE *fp, *fopen();

/*************/
main(argc,argv)
char *argv[];
int argc;
{
   register short i=0;
   int keepgoing;
   char filename[NAMELEN];

   /*** PROCESS COMMAND LINE ***/
   if ((argc < 2) || (argv[1][0] == '?')) {
      printf("usage:  tdp [-d#] [-n] [-a] [-i] [-b] filename\n");
      printf("    # is debug level, 0 => no debug\n");
      printf("   -n disables plotting of crossnet\n");
      printf("   -a enables plotting of axes\n");
      printf("   -i plots white on black\n");
      printf("   -b plots bottom side also\n");
      exit(0);
   }
     
   while (++i < argc) {
      if (argv[i][0] == '-') {
      	 if (argv[i][1] == 'd') debug = atoi(&argv[i][2]);
         else if (argv[i][1] == 'n') netplot = 'N';
         else if (argv[i][1] == 'a') axes = 'Y';
         else if (argv[i][1] == 'i') inverse_vid = TRUE;
         else if (argv[i][1] == 'b') nsides = 2;
      }
      else
         (void) strncpy(filename,argv[i],NAMELEN);
   }
   
   FFPLARGE = 1.0e10; FFPSMALL = 1.0e-10;
   d2r = (FFP)DEG2RAD;

   /*** LOOP UNTIL USER SAYS TO QUIT ***/
   for (ans = 'y' ; ans != 'n';) {
      printf("phi, theta: \x9b2m(suggest 40 -70)\x9bm > ");
      while (!(cb = gets(line)));
      if (*cb) {
         if (numeric(cb)) phi = atoFFP(getwrd(&cb));
         while ((int)(phi) > 180) phi -= 360;
         while ((int)(phi) < -180) phi += 360;
         if (numeric(cb)) theta = atoFFP(getwrd(&cb));
         while ((int)(theta) > 180) theta -= 360.;
         while ((int)(theta) < -180) theta += 360;
         if (debug) printf("phi=%f, theta=%f\n", phi, theta);
         phi *= d2r; theta *= d2r;
      }

      if ((netplot != 'Y') && (netplot != 'N')) {
         printf("Want net plot? <y | Y | n | N> (cap: don't ask again) > ");
         while (!gets(str));
         if ((*str=='y')||(*str=='Y')||(*str=='n')||(*str=='N')) netplot=*str;
      }
      if ((axes != 'Y') && (axes != 'N')) {
         printf("Want axes? <y | Y | n | N> (cap: don't ask again) > ");
         while (!gets(str));
         if ((*str=='y')||(*str=='Y')||(*str=='n')||(*str=='N')) axes=*str;
      }
      
      /* GET DATA */
      fp = fopen(filename,"r");
      if (fp != NULL) {
         (void)fread((char *)&nxpts,sizeof(int),1,fp);
         (void)fread((char *)&nypts,sizeof(int),1,fp);
         (void)fread((char *)zd,sizeof(float),nxpts*nypts,fp);
         if (debug) printf("%d floats read.  ",nxpts*nypts);
         (void) fclose(fp);
      }
      else {
         printf("can't open %s\n",filename);
         exit(1);
      }
      

      /* PLOT DATA */
      printf("\x9b2mWorking...\x9bm ");
      plot3d();

      /* CHECK USER */
      keepgoing = TRUE; PlotFile = FALSE;
      do {
         Wait(1 << w->UserPort->mp_SigBit);
         while (message = (struct IntuiMessage *) GetMsg(w->UserPort)) {;
            class = message->Class;
            code = message->Code;
            ReplyMsg(message);
            if ((class == MENUPICK) && (code != MENUNULL)) {
               switch (MENUNUM(code)) {
               case M_PROJECT:
                  switch (ITEMNUM(code)) {
                  case MI_OPEN: break;
                  case MI_PLOT:
                     pfp = fopen("tdp.plt","w"); PlotFile = TRUE; break;
                  case MI_TOMCAD:
                     mfp = fopen("tdp.cad","w"); To_mCAD = TRUE; break;
                  case MI_QUIT: keepgoing = FALSE; break;
                  default: break;
                  }
                  break;
               default: break;
               }
            }
         }
      } while(keepgoing);

      CleanUp();      

      printf("continue?  < y | n > ");
      while (!gets(str)); ans=*str;
      if (ans == 'N') ans = 'n';
   }
   return(0);
}
