 /*
  * UAE - The Un*x Amiga Emulator
  *
  * routines to handle compressed file automatically
  *
  * (c) 1996 Samuel Devulder
  */

#include "sysconfig.h"
#include "sysdeps.h"

#include "config.h"
#include "options.h"
#include "zfile.h"

#ifdef USE_ZFILE

static struct zfile
{
  struct zfile *next;
  FILE *f;
  char name[L_tmpnam];
} *zlist;

/*
 * called on exit()
 */
void zfile_exit(void)
{
  struct zfile *l;

  while((l = zlist))
    {
      zlist = l->next;
      fclose(l->f);
      free(l);
    }
}

/*
 * fclose() but for a compressed file
 */
int zfile_close(FILE *f)
{
  struct zfile *pl = NULL,
               *l  = zlist;
  int ret;

  while(l && l->f!=f) {pl = l;l=l->next;}
  if(!l) return fclose(f);
  ret = fclose(l->f);

  if(!pl) zlist = l->next;
  else pl->next = l->next;
  free(l);

  return ret;
}

/*
 * gzip decompression
 */
static int gunzip(char *src,char *dst)
{
  char cmd[1024];
  if(!dst) return 1;
  sprintf(cmd,"gzip -d -c %s >%s",src,dst);
  return !system(cmd);
}

/*
 * lha decompression
 */
static int lha(char *src,char *dst)
{
  char cmd[1024];
  if(!dst) return 1;
  sprintf(cmd,"lha -q -N p %s >%s",src,dst);
  return !system(cmd);
}

/*
 * decompresses the file (or check if dest is null)
 */
static int uncompress(char *name,char *dest)
{
    char *ext = strrchr(name, '.');
    char nam[1024];

    if (ext != NULL && access(name,0) >= 0) {
	ext++;
	if(!strcmp(ext,"z")  ||
	   !strcmp(ext,"Z")  ||
	   !strcmp(ext,"gz") ||
	   !strcmp(ext,"GZ") ||
	   0) return gunzip(name,dest);
	if(!strcmp(ext,"lha") ||
	   !strcmp(ext,"LHA") ||
	   !strcmp(ext,"lzh") ||
	   !strcmp(ext,"LZH") ||
	   0) return lha(name,dest);
    }

    if(access(strcat(strcpy(nam,name),".z"),0)>=0  ||
       access(strcat(strcpy(nam,name),".Z"),0)>=0  ||
       access(strcat(strcpy(nam,name),".gz"),0)>=0 ||
       access(strcat(strcpy(nam,name),".GZ"),0)>=0 ||
       0) return gunzip(nam,dest);

    if(access(strcat(strcpy(nam,name),".lha"),0)>=0 ||
       access(strcat(strcpy(nam,name),".LHA"),0)>=0 ||
       access(strcat(strcpy(nam,name),".lzh"),0)>=0 ||
       access(strcat(strcpy(nam,name),".LZH"),0)>=0 ||
       0) return lha(nam,dest);

    return 0;
}

/*
 * fopen() for a compressed file
 */
FILE *zfile_open(char *name,char *mode)
{
    struct zfile *l;
    int fd;
    
    if(!uncompress(name,NULL)) return fopen(name,mode);
    
    if(!(l = malloc(sizeof(*l)))) return NULL;

    tmpnam(l->name);
    fd = creat(l->name, 0666);
    if (fd < 0)
	return NULL;
    
    if(!uncompress(name,l->name)) 
    {
	free(l); close (fd); unlink(l->name); return NULL; 
    }

    l->f=fopen(l->name,mode);
    
    /* Deleting the file now will cause the space for it to be freed
     * as soon as we fclose() it. This saves bookkeeping work.
     */
    unlink (l->name);

    close (fd);
    
    if (l->f == NULL) {
	free(l);
	return NULL;
    }

    l->next = zlist;
    zlist   = l;

    return l->f;
}

#else

/*
 * Stubs for machines that this doesn't work on.
 */

void zfile_exit(void)
{
}

int zfile_close(FILE *f)
{
    return fclose(f);
}

FILE *zfile_open(char *name,char *mode)
{
    return fopen(name, mode);
}

#endif
