/* Copyright 1997 Ken Siders */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "atr.h"
#include "atdos.h"

int main( int argc, char **argv) {
	AtrFilePtr atr;
	int stat = 0, ret = 0;
	char *file = "*.*";
	char *path = NULL;

	printf("AtrExtr Version 0.9u (c)1997 Ken Siders\n");
	printf("Ported and modified by B. Watson, 2007\n");
	printf("This program may be freely distributed\n\n");

	if(argc < 2 || argc > 4) {
		printf("usage: atrextr atrname.atr [file] [dir]\n"
				"Wildcards may be used for file (default: *.*)\n"
				"dir will be created, if it does not exist\n");
		exit(-1);
	}

	atr = OpenAtr(argv[1]);
	if(atr == 0) {
		printf("Error reading ATR file: %s\n", argv[1]);
		CloseAtr(atr);
		exit(2);
	}

	CloseAtr(atr);

	if(argc >= 3 && argv[2][0])
		file = argv[2];

	if(argc == 4) {
		path = argv[3];
		mkdir(path, 0777); /* ignore errors (ExtractAtariFile() will fail instead) */
	}

	while ( !stat ) {
		SetVerbose(1);
		stat = ExtractAtariFile( argv[1], file, path );
		SetVerbose(0);
	}

	if( stat < 0 ) {
		printf("\nError encountered when extracting file(s)\n");
		printf("some files may have been extracted incorrectly\n");
		stat = -stat;
		ret = 1;
	}

	printf("\n%d file(s) extracted\n", stat);

	exit(ret);
}


