/* axe - manipulate atari disk images (dos 2.0s only for now)
 * 
 * Usage: ataridir [-a] [-x filename] [-l] [-v] <disk_image.atr>
 * 
 * (-a means list all directory entries, regardless of status byte)
 * -l means dump sector link info for all filenumbers on disk
 * -x means extract file from image
 * -v means hex dump of VTOC sector
 * 
 */

/* indented with: indent -kr -nsai -nsaw -ts3 -i3 -br -brf -brs -cdw -ce */

#include "axe.h"

int all = 0, total_sec, translate = 0;


int main(int argc, char *argv[]) {
#ifdef DEBUG
	int vtoc_dump = 0;
#endif
	int extract = 0, del = 0, dump_sec = 0, print = 1,
		 opt, write = 0, filenum = 0, i, j,
		 lng = 0, tar = 0, blank = 0, create = 0;
	unsigned char buf[256], diskbuf[720 * 128 + 16];
	char newblank[13];
	unsigned char *debuf;
	char filename[13] = { "\0" };
	char fnbuf[13] = "           \0";
	DIR *credir;
	struct dirent *nfile;

	printf("axe (the ATR/XFD Editor) v%s, (c) B. Watson.\n", VERSION);
	printf("Released under the WTFPL.\n\n");
	if(argc < 2)
		usage(argv[0]);
	while((opt = getopt(argc, argv, "ab:c:lvd:D:x:tuw:")) != EOF) {
		switch (opt) {
		case 'a':
			all = 1;
			break;
		case 'b':
			/*write_blank_disk(optarg); */
			blank = 1;
			strcpy(newblank, optarg);
			break;
		case 'c':
			print = 0;
			create = 1;
			strcpy(filename, optarg);
			break;
		case 'd':
			dump_sec = atoi(optarg);
			break;
		case 'l':
			lng = 1;
			break;
		case 'v':
			dump_sec = 360;
			break;
		case 'D':
			del = 1;
			print = 0;
			strcpy(filename, optarg);
			break;
		case 'x':
			strcpy(filename, optarg);
			extract = 1;
			print = 0;
			break;
		case 'w':
			write = 1;
			print = 0;
			strcpy(filename, optarg);
			break;
		case 't':
			tar = 1;
			break;
		case 'u':
			translate = 1;
			break;
		case '?':
		case ':':
			exit(1);
		default:
			printf("How'd I get here?\n");
			exit(255);
		}
	}

	if((create + write + extract + del + tar) > 1) {
		printf("Only one of -t, -w, -c, -x, -D may be specified.\n");
		exit(1);
	}
	if(blank)
		write_blank_disk(newblank);
	if(optind == argc) {
		if(!blank)
			usage(argv[0]);
		exit(!blank);
	}
	printf("Using image file %s\n\n", argv[argc - 1]);
#ifdef DEBUG
	printf("-a:%d  -l:%d\n  -v:%d", all, lng, vtoc_dump);
#endif
	if(filename[0] && (!create)) {
		parse_filename(filename, fnbuf);
	}
#ifdef DEBUG
	printf("Passed filename %s, parsed as %s\n", filename, fnbuf);
#endif
	if(create)
		write_blank_disk(argv[argc - 1]);
	load_disk(diskbuf, argv[argc - 1]);
/*   if(write) {
      if(strcmp(filename,argv[argc-1])==0) {
	 printf("No disk image name given - aborting\n");
	 exit(1);
      }
      write_file(diskbuf,filename);
      write_disk(diskbuf,argv[argc-1]);
      exit(0);
   } */
#ifdef DEBUG
	printf("-t=%d\n", tar);
#endif
	if(create) {
		char msgbuf[256];
		if((credir = opendir(filename)) == NULL) {
			sprintf(msgbuf, "can't open directory %s", filename);
			perror(msgbuf);
			exit(1);
		}
		while((nfile = readdir(credir)) != NULL) {
			if(nfile->d_name[0] == '.')
				continue;
			write_file(diskbuf, nfile->d_name);
		}
		printf("Done.\n");
		write_disk(diskbuf, argv[argc - 1]);
		exit(0);
	}


	if(tar) {
		make_dir(argv[argc - 1]);
	}

	for (i = 361; i < 369; i++) {
		read_sector(diskbuf, i, buf);
		for (j = 0; j < 128; j += 16) {
			debuf = buf + j;
			if((tar) && (debuf[0] & 64) && !(debuf[0] & 128)) {
				strncpy(fnbuf, (char *)(debuf + 5), 11);
				make_filename(fnbuf, filename);
				dump_file(diskbuf, debuf, filename);
			}
			if(print && !(debuf[0] & 128))
				print_entry(debuf);
#ifdef DEBUG
			printf("strncmp: %s, %s\n", fnbuf, debuf + 5);
#endif
			if((strncmp(fnbuf, (char *)(debuf + 5), 11) == 0) && !(debuf[0] & 128)) {
#ifdef DEBUG
				printf("matched filename\n");
#endif
				if(extract)
					dump_file(diskbuf, debuf, filename);
				if(del || write) {
					traverse_file(diskbuf, debuf, TF_DELETE);
					debuf[0] |= 128;
					write_sector(diskbuf, i, buf);
					write_disk(diskbuf, argv[argc - 1]);
				}
			}

			if(lng)
				traverse_file(diskbuf, debuf, TF_PRINT);

			filenum++;
		}
	}
	if(print) {
		read_sector(diskbuf, 360, buf);
		i = buf[3] + 256 * buf[4];
		printf("%03d FREE SECTORS\n", i);
/*      if(i!=(707-total_sec)) printf("Warning: VTOC free sectors != (707 - total_sec), %d vs. %d\n",i,707-total_sec);*/
	}
	if(dump_sec)
		dump_sector(diskbuf, dump_sec);
	if(write) {
		write_file(diskbuf, filename);
		write_disk(diskbuf, argv[argc - 1]);
	}
	exit(0);
}
