/* Worldchanger - ShadoeLord Extention
 *  WC will attempt to convert your world files from the standard 4+2 Directions to the full
 *  8+2 directions (Adding ne, se, sw, nw)
 * 4/18/2001
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* old numbers */
#define old_north 0
#define old_east 1
#define old_south 2
#define old_west 3
#define old_up 4
#define old_down 5


/* new numbers */
#define north 		0
#define northeast 	1
#define east 		2
#define southeast 	3
#define south 		4
#define southwest 	5
#define west 		6
#define northwest 	7
#define up 		8
#define down 		9

typedef struct _fileData {
	char *line;
	struct _fileData * next;
} fileData, *PfileData;

int main(int argc, char* argv[])
{

	FILE* inFile = NULL;
	PfileData head = NULL;
	char buffer[1024];
	char* fileName = NULL;
	PfileData temp = NULL;
	PfileData temp2 = NULL;
	char rmdir[2];
	int i;

	for( i = 0; i < 1024 ; i++)
		buffer[i] = '\0';

	if( (inFile = fopen(argv[1],"r")) == NULL)
	 {
		fprintf(stderr,"File openning error!\n");
		fflush(stderr);
		exit(1);
	 }
	 
#ifdef DEBUG
	fprintf(stdout,"Argv: %s\nArgc:%d\n",argv[1],argc);
	fflush(stdout);
#endif
	head = (PfileData) malloc( sizeof( fileData ) );
	temp = head;


	/* read in the src file */
	while( fgets(buffer, sizeof( buffer ) , inFile ) != NULL)
	{
		temp2 = temp;
	   	temp->line = (char*) malloc( strlen( buffer ) + 1);
		temp->line[ strlen( buffer )] = '\0';
		strcpy( temp->line , buffer);

		printf("%s",temp->line);
		fflush(stdout);
		
		temp->next = (PfileData) malloc( sizeof( struct _fileData ) );
		temp = temp->next; 
	
	}
	
        temp2->next = NULL;
	free(temp);	
	temp = NULL;
	
	/* write out to old file */
	fclose( inFile );
	fileName = malloc( strlen( argv[1] ) + 1 +4 ); /* + null + .old */
	strcpy( fileName, argv[1] );
	i = strlen(argv[1]);
	fileName[ i ] = '.';
	fileName[ i + 1 ] = 'o';
	fileName[ i + 2 ] = 'l';	
	fileName[ i + 3 ] = 'd';
	fileName[ i + 4 ] = '\0';

#ifdef DEBUG
	fprintf(stdout, "%s\n" , fileName);
	fflush(stdout);
	exit(1);
#endif


	if( (inFile = fopen( fileName ,"w")) == NULL)
	 {
		fprintf(stderr,"File openning error!\n");
		fflush(stderr);
		exit(1);
	 }

	temp = head;
	while( temp != NULL )
	{
		fprintf( inFile, temp->line );
		temp = temp->next;
	}
	
	fclose( inFile );
	
	if( (inFile = fopen( argv[1] ,"w")) == NULL)
	 {
		fprintf(stderr,"File openning error!\n");
		fflush(stderr);
		exit(1);
	 }
	
	temp = head;
	rmdir[1] = '\0';
	while ( temp != NULL )
	 {

		if( temp->line[0] == 'D' && ( temp->line[1] > '0' && temp->line[1] < '9') )
		{
			rmdir[0] = temp->line[1];
			switch ( atoi( rmdir )) {
			case old_north:
				fprintf( inFile, "D%d\n", north);
				break;
			case old_east:
				fprintf( inFile, "D%d\n", east);
				break;
			case old_south:
				fprintf( inFile, "D%d\n", south);
				break;			
			case old_west:
				fprintf( inFile, "D%d\n", west);
				break;			
			case old_up:
				fprintf( inFile, "D%d\n", up);
				break;
			case old_down:
				fprintf( inFile, "D%d\n", down);
				break;			
			default:
				fprintf( inFile, temp->line );
				break;
			}
		}			 
		else
			fprintf( inFile, temp->line);
		temp = temp->next;
	 }
	

	temp = head;
	while( temp != NULL )
	{
#ifdef DEBUG
		printf("%s", temp->line);
#endif
		temp2 = temp->next;
		free( temp->line );
		free( temp );
		temp = temp2;
	}

	fclose( inFile );
	return 0;
}


