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

int main() {
	char buffer[200], oldbuffer[200];
	char origsource[300], source[300], target[300];
	ifstream infile, inpfile;
	ofstream outfile, outpfile;
	infile.open("waves2.txt", ios::in);
	outfile.open("execute.bat", ios::out);
		oldbuffer[0] = 0;
		int counter = 0;

		while((!infile.fail()) && (counter < 2)) {
			//counter++;
			infile.getline(buffer, 200);
			if (strcmp(buffer, oldbuffer) != 0) {

				strcpy(origsource, "c:\\dist\\");
				strcat(origsource, buffer);

				strcpy(source, "c:\\dist\\");
				strcpy(target, "c:\\dist\\");
				strcat(source, buffer);

				short int numchannels;
				long int samplerate;
				inpfile.open(source, ios::binary);
				inpfile.seekg(22, ios::beg);
				inpfile.read((char*) &numchannels, 2);
				inpfile.read((char*) &samplerate, 4);
				inpfile.close();

				char commandln[300];
				strcpy(commandln, "c:\\eac\\lame -h -b 64 -t ");
				strcat(commandln, source);
				system(commandln);
				strcat(source, ".mp3");
				strcat(target, buffer);
				strcat(target, ".wav");

				inpfile.open(source, ios::binary);
				inpfile.seekg(0, ios::end);
				int mp3size = inpfile.tellg();
				inpfile.close();
				cout << "Read " << mp3size << " bytes\n";

				cout << "Writing to " << target << endl;
				cout << "Filesize: " << mp3size+70 << endl;
				cout << "Channels: " << numchannels << ", " << samplerate << "hz" << endl;
				outpfile.open(target, ios::binary);

				// first header
				char stringbuff[5];
				unsigned long int intbuff;
				unsigned short int wordbuff;

				strcpy(stringbuff, "RIFF");
				outpfile.write((char*) stringbuff, 4);
				intbuff = mp3size + 70;
				cout << "Total size: " << intbuff << endl;
				outpfile.write((char*) &intbuff, 4);
				strcpy(stringbuff, "WAVE");
				outpfile.write((char*) stringbuff, 4);

				// first subblock
				strcpy(stringbuff, "fmt ");
				outpfile.write((char*) stringbuff, 4);
				intbuff = 0x1E; // length of block
				outpfile.write((char*) &intbuff, 4);
				wordbuff = 0x55; // mp3 format specifier
				outpfile.write((char*) &wordbuff, 2);
				outpfile.write((char*) &numchannels, 2);
				outpfile.write((char*) &samplerate, 4);
				intbuff = 8000;
				outpfile.write((char*) &intbuff, 4);
				wordbuff = 1;
				outpfile.write((char*) &wordbuff, 2);
				wordbuff = 0;
				outpfile.write((char*) &wordbuff, 2);
				intbuff = 0x0001000c;
				outpfile.write((char*) &intbuff, 4);
				intbuff = 0x00000002;
				outpfile.write((char*) &intbuff, 4);
				intbuff = 0x000100d0;
				outpfile.write((char*) &intbuff, 4);
				wordbuff = 0x0571;
				outpfile.write((char*) &wordbuff, 2);

				// extra info
				intbuff = 0x74636166;
				outpfile.write((char*) &intbuff, 4);
				intbuff = 0x00000004;
				outpfile.write((char*) &intbuff, 4);
				intbuff = 0x00000000;
				outpfile.write((char*) &intbuff, 4);

				// 2nd subblock

				strcpy(stringbuff, "data");
				outpfile.write((char*) stringbuff, 4);
				outpfile.write((char*) &mp3size, 4);

				// mp3 data

				inpfile.open(source, ios::binary);
				char *mp3data;
				if (!(mp3data = new char[mp3size+1])) {
					cout << "out of memory.\n";
					return(1);
				}
				inpfile.read((char*) mp3data, mp3size);
				outpfile.write((char*) mp3data, mp3size);
				outpfile.close();
				inpfile.close();

				delete[] mp3data;

				// delete .wav.mp3
				strcpy(commandln, "del \"");
				strcat(commandln, source);
				strcat(commandln, "\"");
				outfile << commandln << endl;

				// delete .wav
				strcpy(commandln, "del \"");
				strcat(commandln, origsource);
				strcat(commandln, "\"");
				outfile << commandln << endl;

				// move .wav.wav to .wav
				strcpy(commandln, "move \"");
				strcat(commandln, target);
				strcat(commandln, "\" \"");
				strcat(commandln, origsource);
				strcat(commandln, "\"");
				outfile << commandln << endl;

			}
			strcpy(oldbuffer, buffer);
		}
	outfile.close();
	infile.close();
}
