
/*
 *  DECOMPACTE   version reqtools
 *
 *  Idée originale : Xavier Leclercq
 *  Update         : Thibaut Maquet
 *
 *  Avril 1993 .
 *
 */


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

#include <intuition/intuition.h>

#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/reqtools.h>
#include <proto/powerpacker.h>

#include <libraries/reqtools.h>
#include <libraries/ppbase.h>

struct ReqToolsBase *ReqToolsBase = NULL;
struct PPBase *PPBase = NULL;
struct rtFileRequester *filereq;

FILE *fich = NULL;
char filename[80],pathname[80];
char *buffer = NULL;
ULONG len;


void quitte(void)
{
  CloseLibrary( (struct Library *)PPBase);
  CloseLibrary( (struct Library *)ReqToolsBase);
  Exit(0);
}


char *rt_getpathname(char *intitule)
{

  struct rtFileRequester *filereq;
  char nom_fichier[80],chemin_acces[80];

  if (filereq = rtAllocRequestA (RT_FILEREQ, NULL)) 
  {
    if (rtFileRequest (filereq,nom_fichier,intitule,TAG_END))
    {
      strcpy(chemin_acces,filereq->Dir);      
      if (chemin_acces[strlen(chemin_acces) - 1] != ':')
        strcat(chemin_acces,"/");
      strcat(chemin_acces,nom_fichier);
      return chemin_acces;
    }
    else
      return 0;  
    rtFreeRequest (filereq);
  }
  else
    return 0;
}


void unpack(void)
{
  if (ppLoadData(pathname,DECR_SCROLL,0L,&buffer,&len,NULL))
  {
    rtEZRequest ("Fichier introuvable\n","Ok", NULL, NULL);
    quitte();
  }
}


void ecrit_buffer(void) 
{
  if (fich = fopen(pathname,"w"))
  {
    fwrite(buffer,1,len,fich); 
    fclose(fich);
  }
  else
  {
    rtEZRequest ("Erreur d'écriture\n","Ok", NULL, NULL);
    quitte();
  }
}


main(int argc,char **argv)
{ 

	if (!(ReqToolsBase = (struct ReqToolsBase *)
            OpenLibrary (REQTOOLSNAME, REQTOOLSVERSION)))
        {
	  printf("reqtools.library V37 introuvable!\n");
	  Exit (5);
	}

	if (!(PPBase = (struct PPBase *)
            OpenLibrary (PPNAME, PPVERSION)))
        {
          rtEZRequest ("powerpacker.library introuvable\n","Ok", NULL, NULL);
	  Exit (5);
	}

	if (argc > 1)
        {
          if (argv[1][0] == '?')
            printf("\nDecompacte 1.1 par Thibaut MAQUET.\nUsage : Decompacte fichier_source fichier_destination\n\n");
          else
          {
	    strcpy(pathname,argv[1]);
	    unpack();
	    strcpy(pathname,argv[2]);
            ecrit_buffer();
          }
        }
        else
        {
          strcpy(pathname,rt_getpathname("Choisissez un fichier"));
          if (!pathname[0])
            quitte();
          unpack(); 
          strcpy(pathname,rt_getpathname("Sauvez le fichier"));
          if (!pathname[0])
            quitte();
          ecrit_buffer();
        }
        quitte();

}
