//###############################################################################
//#										#
//# AX81 computer with ATmega1284P						#
//# copyright (c) 2010-2012 Joerg Wolfram (joerg@jcwolfram.de)			#
//#										#
//#										#
//# 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 3 of		#
//# the License, or (at your option) any later version.				#
//#										#
//# This library 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 library; if not, write to the			#
//# Free Software Foundation, Inc., 59 Temple Place - Suite 330,		#
//# Boston, MA 02111-1307, USA.							#
//#										#
//###############################################################################

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>

struct stat buf;

int main( void )
{
	char* buffer;
	struct dirent *CurrentFile;
	DIR *Directory;
	int tape,i,j,k,stat,tfile,w_char,nsize;
	unsigned char tprog[16896];
	char filename[1000];
	FILE* imagefile;
	FILE* pfile;
	long offset;
	long filesize;
	int size_low;
	int size_high;
	int fs_type;


	buffer = (char*)malloc(1000);

	if( (buffer = getcwd( NULL, 0 )) == NULL )
	{
		perror("cannot get working dir");
		return EXIT_FAILURE;
	}

	tape=0;
	fs_type=0;

	for(i=0;i<256;i++)
	{
		sprintf(filename,"\TAPES81_ZX81\TAPE_%03u",i);
		if (strstr(buffer,filename))
		{
			tape=i; 	//found
			fs_type=1;
		}

		sprintf(filename,"\TAPES81_ZX80\TAPE_%03u",i);
		if (strstr(buffer,filename))
		{
			tape=i; 	//found
			fs_type=2;
		}

		sprintf(filename,"\TAPES81_JACE\TAPE_%03u",i);
		if (strstr(buffer,filename))
		{
			tape=i; 	//found
			fs_type=3;
		}

		sprintf(filename,"\TAPES81_SPEC\TAPE_%03u",i);
		if (strstr(buffer,filename))
		{
			tape=i; 	//found
			fs_type=4;
		}
	}


	printf("##############################################################################\n");
	printf("#                                                                            #\n");
	printf("#  AX81b Filesystem Reader V1.26 for Windows                                 #\n");
	printf("#  (c) 2012 Joerg Wolfram                                                    #\n");
	printf("#                                                                            #\n");
	switch(fs_type)
	{
		case 1:
		printf("#  ZX81 - Mode                                                               #\n");
		break;

		case 2:
		printf("#  ZX80 - Mode                                                               #\n");
		break;

		case 3:
		printf("#  Jupiter ACE - Mode                                                        #\n");
		break;

		case 4:
		printf("#  ZX Spectrum - Mode                                                        #\n");
		break;

		default:
		printf("#  Unknown Mode                                                              #\n");
		printf(filename);
		break;
	}

	printf("#                                                                            #\n");
	printf("##############################################################################\n\n");

	if(fs_type < 1)
	{
//		printf("WD=%s\n",buffer);
//		printf("FN=%s\n",filename);
		free(buffer);
		printf("Wrong place.\n");
		return EXIT_FAILURE;
	}

	for(i=0;i<16896;i++)
	{
		tprog[i]=255;
	}
	offset=512+540672*tape;

	if(fs_type == 1)
	{
		printf("ZX81 Mode, Tape %d \n",tape);

		for(tfile=0;tfile<32;tfile++)
		{
			imagefile = fopen("..\..\ax81_x81.bin","rb");
			offset=512+540672*tape+16896*tfile;
			stat=fseek(imagefile,offset,SEEK_SET);
			stat = fread (tprog , 1 , 16896 , imagefile );
			fclose(imagefile);

			if(tprog[0]<128)
			{
				filesize=tprog[10]+256*tprog[11];
				buffer[0]=0;
				nsize=0;

				for(j=0;j<10;j++)
				{
					if (tprog[j] > 32)
					{
						w_char=tprog[j];
						buffer[nsize++]=w_char;
						buffer[nsize]=0;
					}
				}
				strcat(buffer,".P");


				pfile = fopen(buffer,"wb");
				stat=fwrite(tprog+512 , 1 , filesize , pfile );
				fclose(pfile);
			}
		}
	}

	if(fs_type == 2)
	{
		printf("ZX80 Mode, Tape %d \n",tape);

		for(tfile=0;tfile<32;tfile++)
		{
			imagefile = fopen("..\..\ax81_x80.bin","rb");
			offset=512+540672*tape+16896*tfile;
			stat=fseek(imagefile,offset,SEEK_SET);
			stat = fread (tprog , 1 , 16896 , imagefile );
			fclose(imagefile);

			if(tprog[0]<128)
			{
				filesize=tprog[10]+256*tprog[11];
				buffer[0]=0;
				nsize=0;

				for(j=0;j<10;j++)
				{
					if (tprog[j] > 32)
					{
						w_char=tprog[j];
						buffer[nsize++]=w_char;
						buffer[nsize]=0;
					}
				}
				strcat(buffer,".O");


				pfile = fopen(buffer,"wb");
				stat=fwrite(tprog+512 , 1 , filesize , pfile );
				fclose(pfile);
			}
		}

	}

	if(fs_type == 3)
	{
		printf("Jupiter ACE Mode, Tape %d \n",tape);
		for(tfile=0;tfile<32;tfile++)
		{
			imagefile = fopen("..\..\ax81_ace.bin","rb");
			offset=512+540672*tape+16896*tfile;
			stat=fseek(imagefile,offset,SEEK_SET);
			stat = fread (tprog , 1 , 16896 , imagefile );
			fclose(imagefile);

			if(tprog[0]<128)
			{
				for(j=0;j<10;j++)
				{
					if (tprog[j] > 32)
					{
						w_char=tprog[j];
						buffer[nsize++]=w_char;
						buffer[nsize]=0;
					}
				}
				strcat(buffer,".ACE");

			}
		}
	}

	if(fs_type == 4)
	{
		printf("ZX Spectrum Mode, Tape %d \n",tape);
		for(tfile=0;tfile<32;tfile++)
		{
			imagefile = fopen("..\..\ax81_spc.bin","rb");
			offset=512+540672*tape+16896*tfile;
			stat=fseek(imagefile,offset,SEEK_SET);
			stat = fread (tprog , 1 , 16896 , imagefile );
			fclose(imagefile);

			if(tprog[0]<128)
			{
				for(j=0;j<10;j++)
				{
					if (tprog[j] > 32)
					{
						w_char=tprog[j];
						buffer[nsize++]=w_char;
						buffer[nsize]=0;
					}
				}
				strcat(buffer,".Z80");

			}
		}

	}

	free(buffer);
	return(0);
}


