/* mmakefile.cpp -- Escrit per Dani Alvarez <dani@uptimed.com>
   Sota llicència GNU/GPL
   http://www.gra2.com
*/

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main() {

int n;
char opcio;
char** fitxer;
char nproj[20];
string comp;
string params;
string ext=".ext";
cout<<"Introdueix nom del projecte: ";
cin>>nproj;
cout<<"Introdueix (C -> gcc || C++ -> g++ || altre): ";
cin>>comp;
cout<<"Necessita el compilador parametres? [S/n] ";
cin>>opcio;
if(opcio=='S' || opcio=='s') {
    cout<<"Introdueix els parametres (ex: -lpthread): ";
    cin>>params;
}
cout<<"Introdueix nombre de fitxers a incloure al makefile: ";
cin>>n;

fitxer=new char*[n];


if(comp=="g++") ext=".cpp";
if(comp=="gcc") ext=".c";
if(ext==".ext") cout<<"No he reconegut el compilador, hauras de canviar l'extensio al Makefile"<<endl;

for(int i=0; i<n; i++) {
fitxer[i]=new char[20];
cout<<endl<<"Introdueix nom del fitxer (sense extensio) "<<i<<": ";
cin>>fitxer[i];
}

cout<<endl<<"OK, generant Makefile..."<<endl;

ofstream f("Makefile", ios::out);

f<<"#Makefile generat per mmakefile pel projecte "<<nproj<<endl<<endl;

f<<"all: "<<nproj<<endl<<endl;

f<<nproj<<": \\"<<endl<<"\t";

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

f<<fitxer[i]<<".o ";

}

f<<endl<<"\t"<<comp<<" -o "<<nproj<<" ";

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

f<<fitxer[i]<<".o ";

}
f<<params;

for(int i=0; i<n; i++) {
f<<endl<<endl;
f<<fitxer[i]<<".o: \\"<<endl;
f<<"\t"<<fitxer[i]<<ext<<endl;
f<<"\t"<<comp<<" -c -g -Wall "<<fitxer[i]<<ext;
}
f<<endl<<endl;
f.close();
return 0;
}
