/**********************************************************************
 
	Copyright (C) 2003 Hirohisa MORI <joshua@nichibun.ac.jp>
 
	This program is free software; you can redistribute it 
	and/or modify it under the terms of the GLOBALBASE 
	Library General Public License (G-LGPL) as published by 

	http://www.globalbase.org/
 
	This program is distributed in the hope that it will be 
	useful, but WITHOUT ANY WARRANTY; without even the 
	implied warranty of MERCHANTABILITY or FITNESS FOR A 
	PARTICULAR PURPOSE.

**********************************************************************/


#include	<fcntl.h>
#include	<stdio.h>
#include	<unistd.h>
#include	<stdlib.h>

#define BUF_SIZE	4000

extern int offset;
char buffer[BUF_SIZE];

int
grue(char * arg)
{
int fd,to;
int size;
int er;
int s;
	fd = open(arg,O_RDONLY);
	if ( fd < 0 ) {
		perror("grue open");
		exit(1);
	}
	to = open("tmp.tar.gz",O_RDWR|O_CREAT|O_TRUNC,0644);
	if ( to < 0 ) {
		perror("grue tmp.tar.gz open");
		exit(1);
	}
	size = lseek(fd,0,SEEK_END);
	size = size - offset;
	lseek(fd,offset,SEEK_SET);
	for ( ; size ; ) {
		if ( size > BUF_SIZE )
			s = BUF_SIZE;
		else	s = size;
		er = read(fd,buffer,s);
		if ( er != s ) {
			fprintf(stderr,"er = %i %i\n",er,s);
			perror("grue read");
			exit(1);
		}
		write(to,buffer,s);
		size -= s;
	}
	close(fd);
	close(to);
	system("gzip -d tmp.tar.gz");
	system("tar xvf tmp.tar");
	unlink("tmp.tar");
	return 0;
}