#include <fstream>
#include <sys/stat.h>
#include <string.h>
#include <iostream>
#include <io.h>
using namespace std;

int main() {
	const int bufsize = 10000;
	char buffer[20000], filebuffer[bufsize+1];
	char tempbuf[200], pathname[210], extendfname[210], extendfnamezwei[310];
	char ext[5];
	long int delta;
	ifstream infile;
	ofstream outfile;

	infile.open("one.txt", ios::in);
	outfile.open("waves.txt", ios::out);
//	outfile << "@echo off" << endl;

	while(!infile.fail()) {
		infile.getline(buffer, 20000);

		if (buffer[0] != ';') {

		//lowercase it

		for (int i = 0; i < strlen(buffer); i++) {
			if ((buffer[i] > 64) && (buffer[i] < 91)) buffer[i]+=32;
		}

		char ftype[5];
		strcpy(ftype, ".wav");
		char* buff;
		int even = 1;
		buff = strtok(buffer, "\"");
		while (buff != NULL) {
			if ((even % 2) == 0) {
				strcpy(tempbuf, buff);
				if (tempbuf[0] == ':') {
					while(1) {
						strcpy(tempbuf, &tempbuf[1]);
						if (tempbuf[0] == ';') break;
					}
					strcpy(tempbuf, &tempbuf[1]);
				}
				ext[0] = 0;
				if (strlen(tempbuf) > 4) {
					strcpy(ext, &tempbuf[strlen(tempbuf)-4]);
				}
				strcpy(extendfname, "c:\\dist\\");
				strcat(extendfname, tempbuf);
				strcpy(extendfnamezwei, "c:\\progra~1\\nekoneko\\neko_fd\\");
				strcat(extendfnamezwei, tempbuf);

				//cout << extendfname << endl;
				//cout << extendfnamezwei << endl;
				//char s[40];
				//gets(s);
				//cout << tempbuf << endl;

				if (ext[0] == '.') {

					ifstream inpfile, inpfile2, inpfile3;
					ofstream outpfile;

					inpfile3.open(extendfnamezwei, ios::binary);
					if (!inpfile3.is_open()) { // check if file exists
						inpfile3.close();
						cout << extendfnamezwei << " does not exist." << endl;
					} else {
						inpfile3.seekg(0, ios::end);
						int matchpos = inpfile3.tellg();
						inpfile3.close();


						inpfile2.open(extendfname, ios::binary);
						int temp = 0;
						inpfile2.seekg(0, ios::end);
						temp = inpfile2.tellg();
						if ((temp == matchpos) && (temp > 0)) { // check if target DOES NOT exist
							cout << extendfname << " already exists." << endl;
							inpfile2.close();
						} else {

							inpfile2.close();

							if ((strcmp(ext, ".bmp") == 0) || (strcmp(ext, ".jpg") == 0)
					 		 || (strcmp(ext, ".mp3") == 0) || (strcmp(ext, ".avi") == 0)
					 		 || (strcmp(ext, ".wav") == 0) || (strcmp(ext, ".nbz") == 0)) {

								// create directory if needed
								for (int i = 0; i<strlen(extendfname); i++) {
									if (extendfname[i] == '\\') {
										strncpy(pathname, extendfname, i);
										pathname[i] = 0;
										mkdir(pathname);
									}
								}

								inpfile.open(extendfnamezwei, ios::binary);
								outpfile.open(extendfname, ios::binary);

								//cout << inpfile.is_open() << endl;
								//cout << outpfile.is_open() << endl;

								inpfile.seekg(0, ios::end);
								delta = inpfile.tellg();
								//cout << delta << endl;

								outpfile.seekp(0, ios::beg);

								cout << "Copying " << tempbuf << " size " << delta << endl;
								inpfile.seekg(0, ios::beg);
								while (delta > bufsize) {
									inpfile.read((char*) &filebuffer, bufsize);
									outpfile.write((char*) &filebuffer, bufsize);
									delta-=bufsize;
								}
								inpfile.read((char*) &filebuffer, delta);
								outpfile.write((char*) &filebuffer, delta);
								outpfile.close();
								inpfile.close();

							}
							if ((strcmp(ext, ".wav") == 0) && (strstr(buffer, "dwave")) == NULL) {
								// not sound effect - compress via lame

								char commandln[250];

								/*
								strcpy(commandln, "c:\\eac\\lame -b 64 -h "); // compress to .wav.mp3
								strcat(commandln, extendfname);
								outfile << commandln << endl;
								strcpy(commandln, "c:\\eac\\wavemp3 /Q "); // convert to .wav.wav
								strcat(commandln, extendfname);
								strcat(commandln, ".mp3");
								outfile << commandln << endl;
								strcpy(commandln, "del "); // del .wav.mp3
								strcat(commandln, extendfname);
								strcat(commandln, ".mp3");
								outfile << commandln << endl;
								strcpy(commandln, "move "); // overwrite .wav.wav to .wav
								strcat(commandln, extendfname);
								strcat(commandln, ".wav ");
								strcat(commandln, extendfname);
								outfile << commandln << endl;
								*/

								strcpy(commandln, tempbuf);
								outfile << commandln << endl;

							}
						}
					}

				}



			}
			buff = strtok(NULL, "\"");
			even++;
		}


		}

	}
	infile.close();
	outfile.close();


}
