/*
   Auto: sys1:bin/cc -sa -ss -su -3 +D <path><file> +l 
   Auto: sys1:bin/ln +cd <path><file>.o -lcl
 */
#include <exec/types.h>
#include <intuition/intuition.h>


#define FF 0x0c
#define CR 0x0d
#define ESC 0x1b
#define CLEARMENU 10
#define SAVEMENU 11
#define QUITMENU 13
#define SIDEAGAD 0x50
#define SIDEBGAD 0x70
#define PRINTGAD 0x42
#define BACKGAD  0x43
#define FORWARDGAD 0x44
#define FFORWARDGAD 0x45
#define DOLBYGAD    0x46
#define REWINDGAD   0x47
#define NEWGAD      0x48
#define DELGAD      0x41
#define CLOSED 0x32
#define INTUITION_REV 33
#define GRAPHICS_REV 33
#include <dh2:aztec/progs/tapecover/tapecovergadg.c>
#include <stdio.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
struct IntuitionBase *IntuitionBase = NULL;
struct GfxBase *GfxBase = NULL;
struct Window  *window1 = NULL;
struct RastPort *rp;
struct TL {
       char aname[35];
       char bname[35];
       char ItemAName[12][35];
       char ItemBName[12][35];
       };
struct TL TapeList[200];  
int GadFlag = 0;
short numtapes = 0, currtape = 0, down = 0, direction = 0;
int whichpicked = 0;



VOID 
cleanExit(wind, returnValue)
    struct Window  *wind;
    int             returnValue;
{
    
    if (wind)
       {
        ClearMenuStrip(wind);
	CloseWindow(wind);
       }
    if (GfxBase)
	CloseLibrary((struct Library *) GfxBase);
    if (IntuitionBase)
	CloseLibrary((struct Library *) IntuitionBase);
    exit(returnValue);
}

VOID 
OpenAll(VOID)
{
    FILE *fopen(), *hs;
    int counter = 0, count2;
    IntuitionBase = (struct IntuitionBase *)
	OpenLibrary("intuition.library", INTUITION_REV);
    if (IntuitionBase == NULL)
	cleanExit(NULL, RETURN_WARN);
    GfxBase = (struct GfxBase *)
	OpenLibrary("graphics.library", GRAPHICS_REV);

    if (GfxBase == NULL)
	cleanExit(NULL, RETURN_WARN);

    window1 = (struct Window *) OpenWindow(&NewWindowStructure1);
    if (window1 == NULL)
	cleanExit(NULL, RETURN_WARN);
    rp = window1->RPort;
    PrintIText (window1->RPort, &IntuiTextList1, 0, 0);
    SetMenuStrip (window1, &MenuList1);
    if ((hs = fopen("TapeCover.file", "r")) != NULL) {
      while (feof(hs) ==0)
      {
	fgets(TapeList[counter].aname,100,hs);
           TapeList[counter].aname[strlen(TapeList[counter].aname)-1] = '\0';
        fgets(TapeList[counter].bname,100,hs);
           TapeList[counter].bname[strlen(TapeList[counter].bname)-1] = '\0';
        for (count2 = 0; count2 < 12; count2++) 
        {
           fgets(TapeList[counter].ItemAName[count2],100,hs);
           TapeList[counter].ItemAName[count2][strlen(TapeList[counter].ItemAName[count2])-1] = '\0';
           fgets(TapeList[counter].ItemBName[count2],100,hs);
           TapeList[counter].ItemBName[count2][strlen(TapeList[counter].ItemBName[count2])-1] = '\0';
        }
        counter++;
      }
        numtapes = counter - 2;
	fclose(hs);
        
    }
}


USHORT 
handleIDCMP(struct Window * win)
{
   int i;
    int             flag = 0;
    struct IntuiMessage *message = NULL;
    int             code;
    USHORT          id;
    UWORD           oldtop;
    ULONG           class;
    struct Gadget  *g;
    while (message = (struct IntuiMessage *) GetMsg(win->UserPort)) {
	class = message->Class;
	code = message->Code;
	g = (struct Gadget *) (message->IAddress);

	switch (class) {
	case CLOSEWINDOW:
	    flag = CLOSED;
	    break;
	case GADGETUP:
	    flag = g->GadgetID;
            down = 0;
            direction = 0;
	    break;
        case GADGETDOWN:
            flag = g->GadgetID;
            down = 1;
            break;
        case MENUPICK:
            flag = ITEMNUM(code)+10;
            if (flag == QUITMENU)
                flag = CLOSED;
            
            break;
          }
    	ReplyMsg((struct Message *) message);
    }
    return (flag);
}

VOID PrintRoutine()
{
   FILE  *fopen(), *fp;
   short counter, linecount;
   char normal[10];
   char condensed[10], halffeed[10];
   char outstr[100];
   char headera[10], headerb[10];
   char dataline[50], databline[50]; 
   char blankline[100];
   fp = fopen("PRT:","w");
   if (fp != 0)
   {      blankline[0] = '\0';
      strcpy   (blankline,"|                                                                 |\n\0");
      halffeed[0] = ESC;
      halffeed[1] = 'K';
      halffeed[2] = '\0';
      normal[0] = ESC;
      normal[1] = '\0';
      strcat (normal, "[0w\0");
      condensed[0] = ESC;
      condensed[1] = '\0';
      strcat (condensed, "[4w\0");
      outstr[0] = '\0';
      strcat (outstr,condensed);
      fputs (outstr,fp);
      fputs ("\n-------------------------------------------------------------------\n\0",fp);
      outstr[0] = '\0';
      strcat (outstr,"| ");
      strcat (outstr,sideaSInfo.Buffer);
      for (counter = 0; counter < (32-strlen(sideaSInfo.Buffer)); counter++)
         strcat (outstr, " \0");
      strcat (outstr,sidebSInfo.Buffer); 
      for (counter = 0; counter < (32-strlen(sidebSInfo.Buffer)); counter++)
         strcat (outstr, " \0");
      strcat (outstr,"|\n\0");
      fputs (outstr,fp);
      fputs (blankline,fp);
      for (linecount = 1; linecount < 13; linecount++)
      {       
       outstr[0] = '\0';
       strcat (outstr,"| \0");
       switch (linecount)
       {
          case 1:
            strcpy (headera," 1. \0");
            strcpy (dataline,a1SInfo.Buffer);
            strcpy (headerb," 1. \0");
            strcpy (databline,b1SInfo.Buffer);
           break;
          case 2:
            strcpy (headera," 2. \0");
            strcpy (dataline,a2SInfo.Buffer);
            strcpy (headerb," 2. \0");
            strcpy (databline,b2SInfo.Buffer);
           break;
          case 3:
            strcpy (headera," 3. \0");
            strcpy (dataline,a3SInfo.Buffer);
            strcpy (headerb," 3. \0");
            strcpy (databline,b3SInfo.Buffer);
           break;
          case 4:
            strcpy (headera," 4. \0");
            strcpy (dataline,a4SInfo.Buffer);
            strcpy (headerb," 4. \0");
            strcpy (databline,b4SInfo.Buffer);
           break;
          case 5:
            strcpy (headera," 5. \0");
            strcpy (dataline,a5SInfo.Buffer);
            strcpy (headerb," 5. \0");
            strcpy (databline,b5SInfo.Buffer);
           break;
          case 6:
            strcpy (headera," 6. \0");
            strcpy (dataline,a6SInfo.Buffer);
            strcpy (headerb," 6. \0");
            strcpy (databline,b6SInfo.Buffer);
           break;
          case 7:
            strcpy (headera," 7. \0");
            strcpy (dataline,a7SInfo.Buffer);
            strcpy (headerb," 7. \0");
            strcpy (databline,b7SInfo.Buffer);
           break;
          case 8:
            strcpy (headera," 8. \0");
            strcpy (dataline,a8SInfo.Buffer);
            strcpy (headerb," 8. \0");
            strcpy (databline,b8SInfo.Buffer);
           break;
          case 9:
            strcpy (headera," 9. \0");
            strcpy (dataline,a9SInfo.Buffer);
            strcpy (headerb," 9. \0");
            strcpy (databline,b9SInfo.Buffer);
           break;
          case 10:
            strcpy (headera,"10. \0");
            strcpy (dataline,a10SInfo.Buffer);
            strcpy (headerb,"10. \0");
            strcpy (databline,b10SInfo.Buffer);
           break;
          case 11:
            strcpy (headera,"11. \0");
            strcpy (dataline,a11SInfo.Buffer);
            strcpy (headerb,"11. \0");
            strcpy (databline,b11SInfo.Buffer);
           break;
          case 12:
            strcpy (headera,"12. \0");
            strcpy (dataline,a12SInfo.Buffer);
            strcpy (headerb,"12. \0");
            strcpy (databline,b12SInfo.Buffer);
           break;
       }
      if (strlen(dataline) != 0)
      {
      strcat (outstr, headera);
      strcat (outstr, dataline);
      }
       else
         strcat (outstr,"    \0");
      for (counter = 0; counter < (28-strlen(dataline)); counter++)
         strcat (outstr, " \0");
      if (strlen(databline) != 0)
      {
        strcat (outstr, headerb);
        strcat (outstr, databline);
      }
      else
        strcat (outstr,"    \0");      
      for (counter = 0; counter < (28-strlen(databline)); counter++)
         strcat (outstr, " \0");
      strcat (outstr,"|\n\0");
      fputs (outstr, fp);
     }
      fputs (blankline,fp);
      fputs (halffeed,fp);
      outstr[0] = '\0';
      strcat (outstr,normal);
      strcat (outstr,"   \0");
      strcat (outstr,sideaSInfo.Buffer);
      strcat (outstr,"\n\0");
      fputs (outstr,fp);
      outstr[0] = '\0';
      strcat (outstr,normal);
      strcat (outstr,"   \0");
      strcat (outstr,sidebSInfo.Buffer);
      strcat (outstr,"\n\0");
      fputs (outstr,fp);
      fputs (condensed,fp);
      fputs (halffeed,fp);
      fputs (blankline,fp);
      fputs (blankline,fp);
      fputs (blankline,fp);
      fputs ("-------------------------------------------------------------------\n\0",fp);
      fputs (normal,fp);
      fputc(FF,fp);
      fclose (fp);
   }
}
struct Gadget FindGad(num)
int  num;
{
   struct Gadget tempgad;
   switch (num)
    {
       case 0:
           tempgad = a1;
           break;
       case 1:
           tempgad = a2;
           break;
       case 2:
           tempgad = a3;
           break;
       case 3:
           tempgad = a4;
           break;
       case 4:
           tempgad = a5;
           break;
       case 5:
           tempgad = a6;
           break;
       case 6:
           tempgad = a7;
           break;
       case 7:
           tempgad = a8;
           break;
       case 8:
           tempgad = a9;
           break;
       case 9:
           tempgad = a10;
           break;
       case 10:
           tempgad = a11;
           break;
       case 11:
           tempgad = a12;
           break;
    }
   return (tempgad);
}

struct Gadget FindBGad(num)
int  num;
{
   struct Gadget tempgad;
   switch (num)
    {
       case 0:
           tempgad = b1;
           break;
       case 1:
           tempgad = b2;
           break;
       case 2:
           tempgad = b3;
           break;
       case 3:
           tempgad = b4;
           break;
       case 4:
           tempgad = b5;
           break;
       case 5:
           tempgad = b6;
           break;
       case 6:
           tempgad = b7;
           break;
       case 7:
           tempgad = b8;
           break;
       case 8:
           tempgad = b9;
           break;
       case 9:
           tempgad = b10;
           break;
       case 10:
           tempgad = b11;
           break;
       case 11:
           tempgad = b12;
           break;
    }
   return (tempgad);
}

VOID SaveTape ()
{
   strcpy (TapeList[currtape].aname, sideaSIBuff);
   strcpy (TapeList[currtape].ItemAName[0], a1SIBuff);
   strcpy (TapeList[currtape].ItemAName[1], a2SIBuff);
   strcpy (TapeList[currtape].ItemAName[2], a3SIBuff);
   strcpy (TapeList[currtape].ItemAName[3], a4SIBuff);
   strcpy (TapeList[currtape].ItemAName[4], a5SIBuff);
   strcpy (TapeList[currtape].ItemAName[5], a6SIBuff);
   strcpy (TapeList[currtape].ItemAName[6], a7SIBuff);
   strcpy (TapeList[currtape].ItemAName[7], a8SIBuff);
   strcpy (TapeList[currtape].ItemAName[8], a9SIBuff);
   strcpy (TapeList[currtape].ItemAName[9], a10SIBuff);
   strcpy (TapeList[currtape].ItemAName[10], a11SIBuff);
   strcpy (TapeList[currtape].ItemAName[11], a12SIBuff);
   strcpy (TapeList[currtape].bname, sidebSIBuff);
   strcpy (TapeList[currtape].ItemBName[0], b1SIBuff);
   strcpy (TapeList[currtape].ItemBName[1], b2SIBuff);
   strcpy (TapeList[currtape].ItemBName[2], b3SIBuff);
   strcpy (TapeList[currtape].ItemBName[3], b4SIBuff);
   strcpy (TapeList[currtape].ItemBName[4], b5SIBuff);
   strcpy (TapeList[currtape].ItemBName[5], b6SIBuff);
   strcpy (TapeList[currtape].ItemBName[6], b7SIBuff);
   strcpy (TapeList[currtape].ItemBName[7], b8SIBuff);
   strcpy (TapeList[currtape].ItemBName[8], b9SIBuff);
   strcpy (TapeList[currtape].ItemBName[9], b10SIBuff);
   strcpy (TapeList[currtape].ItemBName[10], b11SIBuff);
   strcpy (TapeList[currtape].ItemBName[11], b12SIBuff);
}

VOID DisplayTape()
{
   strcpy (sideaSIBuff,TapeList[currtape].aname);
   strcpy (a1SIBuff,TapeList[currtape].ItemAName[0]);
   strcpy (a2SIBuff,TapeList[currtape].ItemAName[1]);
   strcpy (a3SIBuff,TapeList[currtape].ItemAName[2]);
   strcpy (a4SIBuff,TapeList[currtape].ItemAName[3]);
   strcpy (a5SIBuff,TapeList[currtape].ItemAName[4]);
   strcpy (a6SIBuff,TapeList[currtape].ItemAName[5]);
   strcpy (a7SIBuff,TapeList[currtape].ItemAName[6]);
   strcpy (a8SIBuff,TapeList[currtape].ItemAName[7]);
   strcpy (a9SIBuff,TapeList[currtape].ItemAName[8]);
   strcpy (a10SIBuff,TapeList[currtape].ItemAName[9]);
   strcpy (a11SIBuff,TapeList[currtape].ItemAName[10]);
   strcpy (a12SIBuff,TapeList[currtape].ItemAName[11]);
   strcpy (sidebSIBuff,TapeList[currtape].bname);
   strcpy (b1SIBuff,TapeList[currtape].ItemBName[0]);
   strcpy (b2SIBuff,TapeList[currtape].ItemBName[1]);
   strcpy (b3SIBuff,TapeList[currtape].ItemBName[2]);
   strcpy (b4SIBuff,TapeList[currtape].ItemBName[3]);
   strcpy (b5SIBuff,TapeList[currtape].ItemBName[4]);
   strcpy (b6SIBuff,TapeList[currtape].ItemBName[5]);
   strcpy (b7SIBuff,TapeList[currtape].ItemBName[6]);
   strcpy (b8SIBuff,TapeList[currtape].ItemBName[7]);
   strcpy (b9SIBuff,TapeList[currtape].ItemBName[8]);
   strcpy (b10SIBuff,TapeList[currtape].ItemBName[9]);
   strcpy (b11SIBuff,TapeList[currtape].ItemBName[10]);
   strcpy (b12SIBuff,TapeList[currtape].ItemBName[11]);
   RefreshGList(&sidea,window1,NULL,32);
}

VOID DeleteTape()
{
   int counter;
   for (counter = currtape; counter < numtapes; counter++)   
       TapeList[counter] = TapeList[counter+1];
   TapeList[numtapes].aname[0] = '\0';
   TapeList[numtapes].bname[0] = '\0';
   for (counter = 0; counter <= 11; counter++)
   {
       TapeList[numtapes].ItemAName[counter][0] = '\0';
       TapeList[numtapes].ItemBName[counter][0] = '\0';
   } 
   numtapes--;
   if (numtapes < currtape)
        currtape = numtapes;
}


VOID StoreTape()
{
   FILE *fopen(), *hs;
   int counter = 0, count2;
      if ((hs = fopen("TapeCover.file", "w")) != NULL) {
      for (counter = 0; counter <= numtapes; counter++)
      {
	fputs(TapeList[counter].aname,hs);
        fputs ("\n",hs);
        fputs(TapeList[counter].bname,hs);
        fputs ("\n",hs);
        for (count2 = 0; count2 < 12; count2++) 
         {
           fputs(TapeList[counter].ItemAName[count2],hs);
           fputs("\n",hs);
           fputs(TapeList[counter].ItemBName[count2],hs);
           fputs("\n",hs); 
         }   
     }
        fflush(hs);
	fclose(hs);
    }
}

VOID 
main()
{
    struct Gadget tempgad, tempgad2;
    short dolby, changed = 0;
    ULONG           signalmask, signals;
    OpenAll();    
    DisplayTape();
    signalmask = 1L << window1->UserPort->mp_SigBit;
    ActivateGadget(&sidea,window1,NULL);
    while (GadFlag != CLOSED)
   {
    signals = Wait(signalmask);
    GadFlag = handleIDCMP(window1);
    if (down == 0)
    {
    if ((direction == 1) && (currtape < numtapes))
    {
        currtape++;
        DisplayTape();
    }
    else if ((direction == -1) && (currtape > 0))
    {
        currtape--;
        DisplayTape();
    } else
    switch (GadFlag)
    {
      case PRINTGAD: 
           PrintRoutine();
           break;
      case DOLBYGAD:
           if (dolby == 0)
                dolby = 1;
               else dolby = 0;
           break;
      case FORWARDGAD:
           if (changed == 1)
              {
                changed = 0;
                SaveTape();
              }
           if (currtape < numtapes)
           {
              currtape++;
              DisplayTape();
           }
           break;
      case BACKGAD:
           if (changed == 1)
           {
             changed = 0;
             SaveTape();
           }  
            if (currtape > 0)
           {
                 currtape--;
            DisplayTape();
           }
           break;
      case NEWGAD:
           if (changed == 1)
           {
              changed = 0;
              SaveTape();
           }
           numtapes++;
           currtape = numtapes;
           DisplayTape();
           ActivateGadget(&sidea,window1,NULL);
           break;
      case DELGAD:
           if (changed == 1)
             changed = 0;
           DeleteTape();
           if (numtapes < 0)
           {
             numtapes++;
             currtape++;
           }
           DisplayTape();
           break;
      case CLEARMENU:
           while (numtapes != -1)
              DeleteTape();
           numtapes = 0;
           currtape = 0;
           DisplayTape();
           break;
      case SAVEMENU:
           if (changed == 1)
           { 
            changed = 0;
            SaveTape();
           }
           StoreTape();
           break;

    }
    }
    else
      switch (GadFlag)
    {
       case FFORWARDGAD:
           if (changed == 1)
           {
             changed = 0;
             SaveTape();
           }  
             direction = 1;
             down =0;
             break;
       case REWINDGAD:
           if (changed == 1)
           {
             changed = 0;
             SaveTape();
           }  
             direction = -1;
             down = 0;
             break;
     }
    if ((GadFlag >= SIDEAGAD) && (GadFlag <= SIDEAGAD+12))
{
    changed = 1;
    tempgad = FindGad (GadFlag - SIDEAGAD);
    ActivateGadget(&tempgad,window1,NULL);
}
 else
    if ((GadFlag >= SIDEBGAD) && (GadFlag <= SIDEBGAD+12))
{
    changed = 1;
    tempgad = FindBGad (GadFlag - SIDEBGAD);
    ActivateGadget(&tempgad,window1,NULL);
}
   }
    cleanExit(window1, RETURN_OK);
}
