/***************************************************************************
*   Copyright (C) 2006 by Andreas Pakulat                                 *
*   apaku@gmx.de                                                          *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************/

#include "qmakedefaultopts.h"

#include <kdebug.h>
#include <tdetempfile.h>
#include <tqregexp.h>
#include <kprocess.h>

#include <blockingkprocess.h>

TQMakeDefaultOpts::TQMakeDefaultOpts()
{

}

void TQMakeDefaultOpts::readVariables( const TQString& qmake, const TQString& projdir )
{
    KTempFile makefile (projdir+"/", ".mf");
    KTempFile qmakefile(projdir+"/", ".pro");
    if ( makefile.status() == 0 && qmakefile.status() == 0 )
    {
        makefile.close();
        qmakefile.close();

        BlockingTDEProcess proc;
        kdDebug(9024) << "KProc Working dir:" << projdir << endl;
        proc.setWorkingDirectory( projdir );
        proc << qmake;
        proc << "-d";
        proc << "-o";
        proc << makefile.name();
        proc << qmakefile.name();
        kdDebug(9024) << "Executing:" << proc.args() << endl;
        proc.start( TDEProcess::NotifyOnExit, TDEProcess::Stderr );
        if( !proc.isRunning() && !proc.normalExit() )
        {
            kdDebug(9024) << "Couldn't execute qmake: " << proc.args() << endl;
            makefile.unlink();
            qmakefile.unlink();
            m_variables.clear();
            m_keys.clear();
        }else
        {
            makefile.unlink();
            qmakefile.unlink();
            TQStringList lines = TQStringList::split( "\n", proc.stdErr() );
            kdDebug(9024) << "Got " << lines.count() << " lines" << endl;
            for ( TQStringList::const_iterator it = lines.begin(); it != lines.end(); ++it)
            {
                TQString line = *it;
                TQRegExp re( "DEBUG 1: ([^ =:]+) === (.*)" );
                if ( re.exactMatch( line ) )
                {
                    TQString var = re.cap( 1 );
                    TQStringList values = TQStringList::split( " :: ", re.cap( 2 ) );
                    m_variables[var] = values;
                    m_keys.append( var );
                }
            }
        }
    }
}

TQMakeDefaultOpts::~TQMakeDefaultOpts()
{
}

const TQStringList TQMakeDefaultOpts::variableValues( const TQString& var ) const
{
//     TQStringList result;
    if ( m_variables.contains(var) )
        return m_variables[var];
    return TQStringList();
}

const TQStringList& TQMakeDefaultOpts::variables() const
{
    return m_keys;
}
