// SinnerSol
// menu editor3.cpp : Defines the entry point for the console application.
//
#include <fstream>
#include <iostream>
//#include <stdio>
//#include <stdio.h>
////#include <unistd.h>
//#include <stdlib.h>
//#include <pthread.h>
//#include <dlfcn.h>



using namespace std;
bool checkfile(char * filename);
void writefiles();

int menulocationApps,menulocationGames=0; // bad globals, i know
char apps[255][255];
char games[255][255];
char appsTitle[255][255];
char gamesTitle[255][255];

void main(int argc, char* argv[])
{
//cout <<(checkfile("xos\\ini\\games.ini"));
	if ((checkfile(".\\xos\\ini\\games.ini") == false) ||  (checkfile(".\\xos\\ini\\apps.ini" ) == false))

{
cout << "File check failed\n";
getchar();
return;
}

do{
	//clr();
	checkfile("");
	cout <<"\n     Xbox-OS menu creator 1.0\n\n\n";
   	cout <<" 1. Add Game Directory\n";
	cout <<" 2. Add App Directory\n";
	cout <<" 3. Add Game\n";
	cout <<" 4. Add App\n";
	cout <<" 5. Print Games list\n";
	cout <<" 6. Print App list\n";
//	cout <<" 7. Modify Game by number\n"; // im lazy
//	cout <<" 8. Modify App by number\n";
	cout <<" 7. Write Files & Exit\n";
	cout <<" 8. Exit\n";
	char char1[1];
	cin >>char1;
	switch(char1[0])
	{
	case '1':
	strcpy(gamesTitle[menulocationGames],"autoadd");
	cout << "Enter pull path to directory, example  e:\\games\\ or f:\\games\\ \n";
	cin >> games[menulocationGames++];
	if (games[menulocationGames-1][strlen(games[menulocationGames-1])-1] != '\\')
			strcat(games[menulocationGames-1],"\\");// idiots check..
	if (games[menulocationGames-1][2] == ':')
	{
		cout << "Bad directory path\n";
		menulocationGames--;
	}
		
	break;
	case '2':
	strcpy(appsTitle[menulocationApps],"autoadd");
	cout << "Enter pull path to directory, example  e:\\apps\\ or f:\\apps\\ \n";
	cin >> apps[menulocationApps++];
		if (apps[menulocationApps-1][strlen(apps[menulocationApps-1])-1] != '\\')
			strcat(apps[menulocationApps-1],"\\");// idiots check..
	if (apps[menulocationApps-1][2] == ':')
	{
		cout << "Bad directory path\n";
		menulocationApps--;
	}

	break;
	case '3':
	cout << "Enter Name of game\n";
	cin  >>gamesTitle[menulocationGames];
	cout << "Enter pull path to games, example  e:\\testdrive\\default.xbe \n";
	cin  >>games[menulocationGames++];
		if (games[menulocationGames-1][2] == ':')
	{
		cout << "Bad directory path\n";
		menulocationGames--;
	}

	break;
	case '4':
	cout << "Enter Name of App\n";
	cin >>appsTitle[menulocationApps];
	cout << "Enter pull path to app, example  e:\\xboxplorer\\default.xbe \n";
	cin >> apps[menulocationApps++];
		if (apps[menulocationApps-1][2] == ':')
	{
		cout << "Bad directory path\n";
		menulocationApps--;
	}

	break;
	case '5':
	cout << "Print Games \n";
	{
	for (int go=0;go <menulocationGames;go++)
		cout <<gamesTitle[go]<<"=="<<games[go]<<"\n";
	}
	break;
	case '6':
	cout << "Print Apps \n";
		{
	for (int go=0;go <menulocationApps;go++)
		cout <<appsTitle[go]<<"=="<<apps[go]<<"\n";
	}
	break;
	case '7':
	writefiles();
	case '8':
	exit(0);// really bad structure, i know.. dont tell me
	break;

	default:
	cout << "Next selection\n";
	break;
	}//end of switch
	//cout << "Next selection\n";
}while(true);
	//return 0;
}

bool checkfile(char * filename)
{	ifstream is(filename);
  if (is == NULL)
  {  is.close();
     return false;}	
  else
	return true;
	}

void writefiles()
{
  char temp[255];
  ofstream is(".\\xos\\ini\\apps.ini");
  if (is == NULL)
	  return;
  else
  {//write file
	 for(int go=0;go<menulocationApps;go++)
	 {
		strcpy(temp,appsTitle[go]);
		strcat(temp,"==");
		strcat(temp,apps[go]);
		strcat(temp,"\n");
		is.write(temp,strlen(temp));
	 }
  }//end of else
  is.close();

  ofstream isg(".\\xos\\ini\\games.ini");

  if (isg == NULL)
	  return;
  else
  {//write file
	 for(int go=0;go<menulocationGames;go++)
	 {
		strcpy(temp,gamesTitle[go]);
		strcat(temp,"==");
		strcat(temp,games[go]);
		strcat(temp,"\n");
		isg.write(temp,strlen(temp));
	 }
	 isg.close();
  }//end of else



}
