// ddiff/dpatch - manipulate differences between debian packages.
// Copyright 2000 Tom Rothamel <tom-ddiff@onegeek.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// 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.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  

#include <stdio.h>
#include "pkg.h"

void printgf(GFILE *gf, char *name) {
	char buf[1024];
	int l;

	/* fprintf(stderr, "---> %s <---\n", name); */
	
	if (!gf) {
		fprintf(stderr, "Couldn't find element.\n");
		exit (-1);
	}
	
	while (l = gf->read(buf, 1, 1024)) {
		fwrite(buf, 1, l, stdout);
	}
}
	

int main(int argc, char **argv) {
	int i;
	GFILE *gf = NULL;
	Pkg *pkg;
	
	if (argc < 3) {
		fprintf(stderr, "Too few arguments.\n");
		exit(-1);
	}

	pkg = open_deb(argv[1]);

	if (!pkg) {
		fprintf(stderr, "Couldn't open deb file.\n");
		exit(-1);
	}
	
	if (argc == 3) {
		gf = pkg->element(argv[2], NULL);
		printgf(gf, argv[2]);
	} else {
		for (i = 3; i < argc; i++) {
			gf = pkg->element(argv[2], argv[i]);
			printgf(gf, argv[i]);
		}
	}

	delete pkg;
}
