/*
**  this code is not a example for well programming :-)
**  but for use of amigametaformat.library and parsing AMF files
**  compile with 'sc test LINK'
*/

#include <intuition/intuition.h>
#include <graphics/view.h>
#include <dos/dos.h>
#include <exec/memory.h>
#include <exec/libraries.h>
#include <exec/types.h>
#include <libraries/iffparse.h>
#include <libraries/amigametaformat.h>
#include <pragmas/intuition_pragmas.h>
#include <pragmas/exec_pragmas.h>
#include <pragmas/dos_pragmas.h>
#include <pragmas/iffparse_pragmas.h>
#include <pragmas/amigametaformat_pragmas.h>
#include <stdio.h>
#include <string.h>

extern  struct  Library *SysBase;

void main()
{
          APTR        amf;
   struct IFFHandle  *iff;
   struct Screen     *scr;
   struct Window     *wnd;
   struct StoredProperty *sp;
   struct Library    *IFFParseBase;
   struct Library    *AmigaMetaFormatBase;
   struct Library    *IntuitionBase;
   struct Library    *DOSBase;
          STRPTR      arg;
          ULONG      *myarray, function, count, error;
          ULONG      mydata[10], file;
          ULONG      pens[] = { ~0 };

   if (DOSBase = OpenLibrary( "dos.library", 37))
   {
       if (IntuitionBase = OpenLibrary( "intuition.library", 37))
       {
           if (IFFParseBase = OpenLibrary( "iffparse.library", 37))
           {
               if (AmigaMetaFormatBase = OpenLibrary( "amigametaformat.library", 6))
               {
                   if (scr = OpenScreenTags( NULL,
                                             SA_Width,-1,
                                             SA_Height,-1,
                                             SA_Depth,4,
                                             SA_Pens,pens,
                                             SA_FullPalette,TRUE,
                                             SA_Type,CUSTOMSCREEN,
                                             SA_SharePens,TRUE,
                                             TAG_DONE))
                   {
                      if (wnd = OpenWindowTags( NULL,
                                                WA_CustomScreen,scr,
                                                WA_IDCMP,IDCMP_MOUSEBUTTONS,
                                                WA_Title,"AMF file viewer (simple)",
                                                TAG_DONE))
                      {
                          arg = GetArgStr();
                          arg[strlen(arg)-1] = 0;
                          if (file = Open( arg, MODE_OLDFILE))
                          {
                              if (iff = AllocIFF())
                              {
                                  InitIFFasDOS( iff);
                                  iff->iff_Stream = file;
                                  if (!OpenIFF( iff, IFFF_READ))
                                  {
                                      mydata[0] = ID_AMFF;
                                      mydata[1] = ID_VERS;
                                      mydata[2] = ID_AMFF;
                                      mydata[3] = ID_FVER;
                                      mydata[4] = ID_AMFF;
                                      mydata[5] = ID_HEAD;
                                      mydata[6] = ID_AMFF;
                                      mydata[7] = ID_BODY;
                                      if (!PropChunks( iff, (LONG *)mydata, 4))
                                      {
                                          if (!StopChunk( iff, ID_AMFF, ID_BODY))
                                          {
                                              if (!ParseIFF(iff,IFFPARSE_SCAN))
                                              {
                                                  if (sp = FindProp( iff, ID_AMFF, ID_VERS))
                                                  {
                                                      /* nicht sehr elegant, aber funkioniert :-) */
                                                      myarray = (ULONG *)sp->sp_Data;
                                                      printf( "Alte Version: %ld.%ld\n", myarray[0], myarray[1]);
                                                  }
                                                  if (sp = FindProp( iff, ID_AMFF, ID_FVER))
                                                      printf( "Neue Version: %s\n", sp->sp_Data);
                                                  if (sp = FindProp( iff, ID_AMFF, ID_HEAD))
                                                  {
                                                      /* nicht sehr elegant, aber funkioniert :-) */
                                                      myarray = (ULONG *)sp->sp_Data;
                                                      printf( "Dimension: %d x %d\n", myarray[0], myarray[1]);
                                                  }
                                                  mydata[0] = (ULONG )wnd;
                                                  mydata[1] = (ULONG )scr->ViewPort.ColorMap;
                                                  if (amf = AmfOpen( AMF_WINDOW, mydata))
                                                  {
                                                      while (ReadChunkBytes( iff, &function, 4) == 4)
                                                      {
                                                         if (ReadChunkBytes( iff, &count, 4) == 4)
                                                         {
                                                             if (myarray = AllocMem( count*4, MEMF_CLEAR))
                                                             {
                                                                 if (ReadChunkBytes( iff, myarray, count*4) == (count*4))
                                                                 {
                                                                     error = AmfFunction( amf, myarray, function, count);
                                                                     if (error != 0)
                                                                         printf("function:%ld count:%ld error:%ld\n",function,count,error);
                                                                 }
                                                                 FreeMem( myarray, count*4);
                                                             }
                                                         }
                                                      }
                                                      AmfClose(amf);
                                                  }
                                              }
                                          }
                                      }
                                      CloseIFF( iff);
                                  }
                                  FreeIFF( iff);
                              }
                              Close( file);
                           }else
                           {
                               printf( "can`t open file: %s\n" , arg);
                           }
                           WaitPort( wnd->UserPort);
                           CloseWindow( wnd);
                       }else
                       {
                           printf( "no window\n");
                       }
                       CloseScreen( scr);
                   }else
                   {
                       printf( "no screen\n");
                   }
                   CloseLibrary( AmigaMetaFormatBase);
               }else
               {
                   printf( "no amigametaformat.library 6+\n");
               }
               CloseLibrary( IFFParseBase);
           }else
           {
               printf( "no iffparse.library 37+\n");
           }
           CloseLibrary( IntuitionBase);
       }else
       {
           printf( "please use OS 2.0 or better for this\n");
       }
       CloseLibrary( DOSBase);
   }else
   {
       printf( "please use OS 2.0 or better for this\n");
   }
}

