#include <stdio.h>
#include <mintbind.h>
#include <stdlib.h>
#include <dos.h>
#include <cookie.h>

long execle(char *,...);

int	main(int ac,char **av)
{
	char	*cmd = "-xf",*p;
	char	prg1[FMSIZE];
	char	prg2[FMSIZE];
	short	fds[2];
	long		pid,exit_status;

	if(getcookie('MiNT',NULL)==0)
	{
		fprintf(stderr,"detaz: needs MINT!!\n");
		exit(EXIT_FAILURE);
	}

	if(ac<2 || ac>3)
	{
		fprintf(stderr,"detaz [-l] infile\n");
		exit(EXIT_FAILURE);
	}
	p = av[1];
	if(*p=='-')
	{
		++p;
		if(*p=='l'||*p=='L')
		{
			cmd = "-tf";
			++av;
			--ac;
		}
	}
	if(getpfe(prg1,"gzip.ttp"))
	{
		fprintf(stderr,"detaz:can't find gzip\n");
		exit(EXIT_FAILURE);
	}
	if(getpfe(prg2,"tar.ttp"))
	{
		fprintf(stderr,"detaz:can't find tar\n");
		exit(EXIT_FAILURE);
	}
	if(Fpipe(fds)<0)
	{
		fprintf(stderr,"detaz can't pipe!!\n");
		exit(EXIT_FAILURE);
	}
	if(Pfork()==0)
	{
		Fforce(1,fds[1]);
		execle(prg1,"-dc",av[1],NULL);
	}
	if((pid=Pfork())==0)
	{
		Fforce(0,fds[0]);
		execle(prg2,"xf","-",NULL);
	}
	
	Pwaitpid(pid,0,&exit_status);
	return 0;
}

	