#include <fileSystem.h>

void fileSystem_writeFile( char * filepath, char * data )
{
    FILE *fp = fopen( filepath, "wb" );

    if (fp != NULL)
    {
    	
        fputs( data, fp );
        
        fclose( fp );

    }

}

int fileSystem_textIsFilename( char * value ) {

	array * parts		= text_split( text_copy( value ) , "." );

	int partsCount		= array_length( parts );

	if( partsCount > 1 ) {

		return 1;

	} else {

		return 0;

	}

}

void fileSystem_ensureDirectory( char * filePath ) {

	//printf("fileSystem_ensureDirectory:		%s\n\n\n\n", filePath);


	struct array * parts	= text_split( text_copy( filePath ), "/" );

	int count				= array_length( parts );

	for ( int i = 1; i < count; ++i )
	{
		
		struct text * currentPath = text_new( "" );

		for (int j = 0; j < i; ++j)
		{

			char * pathPart		= array_get( parts, j );
		
			text_append( currentPath, pathPart );

			text_append( currentPath, "/" );

			if( fileSystem_exists( currentPath->value ) ) {

			

			} else {


				if( strlen( currentPath->value ) > 100 ) {

					printf("	ERROR:		TempPath is to big.  '%s' ", currentPath->value);

					exit( 0 );

				}


				printf("Directory does not exists, Creating Directory%i	'%s'   \n\n", strlen( currentPath->value ),currentPath->value );

				fileSystem_makeDirectory( currentPath->value );

			}


		}
/*
		struct array * tempParts	= text_split( text_copy( filePath ), "/" );

		for (int j = 0; j < count-i; ++j)
		{
			array_pop( tempParts );
		}

		char * tempPath		= text_join( tempParts, "/" );

		if( fileSystem_exists( tempPath ) ) {

		

		} else {


			if( strlen( tempPath ) > 100 ) {

				printf("	ERROR:		TempPath is to big.  '%s' ", filePath);

				exit( 0 );

			}


			printf("Directory does not exists, Creating Directory%i	'%s'   \n\n", strlen( tempPath ),tempPath );

			fileSystem_makeDirectory( tempPath );

		}

		printf(	" tempPath:		%s\n", tempPath);
*/
	}


}

int fileSystem_makeDirectory( char * path ) {

	if ( fileSystem_exists( path ) == NULL ) {

		mkdir( path, 0700 );

	}

	return 1;

}


int fileSystem_exists( char * path ) {

	if ( access( path, F_OK ) == 0 ) {

		return 1;

	} else {

		return 0;

	}

}

struct array * fileSystem_readDir( char * filePath ) {

	DIR * dir;

	struct dirent * entry;

	struct array * files = array_new();

	if ( ( dir = opendir( filePath ) ) == NULL ){

		 perror("opendir() error");

	} else {

		while( ( entry = readdir( dir ) ) != NULL ) {

			char * filename = (char *)entry->d_name;

			if ( strcmp( filename, ".." ) != 0 && strcmp( filename, "." ) != 0 ) {

				array_add( files, filename );

				//printf("%s\n", filename);

			}
		}
	}

	return files;
	//closedir(dir);
}

char * fileSystem_readFile( char * name ) {

	FILE * file = fopen( name, "r" );

	if ( file == NULL ) {

		fprintf(stderr, "Error: Can't open file '%s'.   \n\n", name);

		exit(EXIT_FAILURE);

	}
	
	fseek( file, 0, SEEK_END );

	long length = ftell(file);

	fseek( file, 0, SEEK_SET );

	char * buffer = malloc( sizeof( char ) * ( length + 1 ) );

	buffer[ length ] = '\0';

	fread( buffer, sizeof(char), length, file );

	fclose(file);

	return buffer;

}