/*************************************************************************
*                              unpLibInfo                                *
*                                                                        *
* This program will show which crunchers the library installed supports. *
*                                                                        *
*************************************************************************/

// First we include a lot of things

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <exec/types.h>
#include <dos/rdargs.h>
#include <libraries/unpack.h>

#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/unpack.h>

// Global Variables
struct Library *UnpackBase;
struct UnpackInfo *cinfo=NULL;
struct RDArgs *rda=NULL;


#ifdef LATTICE
void __regargs _CXBRK(void)               // Catch CTRL-C Handling
{
  fprintf(stderr,"*** User breaked\n");   // Print out break message
  upFreeCInfo(cinfo);                     // and clean up after us
  CloseLibrary(UnpackBase);
  FreeArgs(rda);
  exit(0);                                // then we exit our program.
}
#endif


// Main program
void main (void)
{
extern struct ExecBase *SysBase;

LONG   arguments[1] = {0};
struct NumberStruct *unpnumber;
struct UnpListStruct *unplist;
char *name;
int j,k;
int i=0;


  if( ((struct Library*) DOSBase)->lib_Version < 37 )
  {
      fprintf(stderr,"unpLibInfo requires at least Kickstart 2.04!\n");
      exit(0);
  }

  if (rda = ReadArgs("MORE/S",arguments,NULL))
  {
    if (UnpackBase = OpenLibrary("unpack.library",42L))
    {
      if (cinfo = upAllocCInfo())
      {
        printf("unpLibInfo 1.0 (2/10-1995) by Thomas Neumann\n\n");

        unpnumber = upNewUnpackNum();
        printf("Contents of unpack.library %d.%d (types: %d, total: %d)\n\n",
                unpnumber->ns_Version, unpnumber->ns_Revision,
                unpnumber->ns_Types, unpnumber->ns_Unpackers);

        if (arguments[0] == 0)
        {
        // This method is the old one. This will only print out the
        // names of the decrunchers.

          printf("%s\n",upUnpackList(cinfo));
          while (name = upUnpackListNext(cinfo))
            printf("%s\n",name);
        }
        else
        {
        // Well, here we have a new function from version 42. This
        // function will return some more informations about the
        // decrunchers.

        printf("Name                                                 |Number|TypeNum|Type\n");
        printf("-----------------------------------------------------+------+-------+----\n");

        while (unplist = upGetUnpacker(cinfo, i++))
        {
          printf("%s",unplist->ul_Name);
          if ((k = 53-strlen(unplist->ul_Name)) < 0)
            k=1;

          for (j=0;j<k;j++)
            printf(" ");

          printf("| %4d | %5d |",unplist->ul_CrunchNum, unplist->ul_TypeNum);

          switch (unplist->ul_Type)
          {
            case 1: printf("Archive");break;
            case 2: printf("Data");break;
            case 3: printf("Object");break;
            case 5: printf("Track");break;
            case 6: printf("Address");break;
          }

          if (unplist->ul_Flags && (1<<ULFB_Encrypted))
            printf("!Encrypted");
          printf("\n");
        }
        }
        upFreeCInfo(cinfo);
      }
      CloseLibrary(UnpackBase);
    }
    else
      fprintf(stderr,"You need unpack.library version 42 or higher\n");

  FreeArgs(rda);
  }
  else
    PrintFault(IoErr(),NULL);
}
