/////////////////////////////////////////////////////////////////////////////
// Name:        wxbuild.cpp
// Purpose:     wxWidgetsBuild, wxWidgetsBuildArray
// Author:      Francesco Montorsi
// Modified by:
// Created:     2006-06-29
// RCS-ID:      $Id: wxbuild.cpp,v 1.1.1.1 2006/12/12 09:39:37 frm Exp $
// Copyright:   (c) Francesco Montorsi
// Licence:     wxWidgets licence
/////////////////////////////////////////////////////////////////////////////


// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

// includes
#include <wx/config.h>

#include "wxp/packagedep.h"
#include "wxp/packagecmd.h"
#include "wxp/package.h"
#include "wxp/version.h"


// RTTI
IMPLEMENT_DYNAMIC_CLASS(wxWidgetsBuild, wxObject)


// wx array implementations
#include <wx/arrimpl.cpp> // this is a magic incantation which must be done!
WX_DEFINE_OBJARRAY(wxWidgetsBuildArrayHelper);

// static
wxWidgetsBuildArray wxWidgetsBuildArray::s_arrWxBuilds;



// ----------------------------------------------------------------------------
// wxWidgetsBuild
// ----------------------------------------------------------------------------

wxString wxWidgetsBuild::GetUserFriendlyName() const
{
    return wxString::Format(wxT("%s %s [%s - %s %s %s]"),
            wxPlatformInfo::GetPortIdName(m_port, false).c_str(),
            m_version.GetAsString().c_str(),
            wxPackageCompilerFormat2String(m_format).c_str(),
            m_bUnicode ? wxT("Unicode") : wxT("ANSI"),
            m_bStatic ? wxT("static") : wxT("DLL"),
            m_bDebug ? wxT("debug") : wxT("release"));
}

bool wxWidgetsBuild::Load(wxConfigBase *p, const wxString &path)
{
    m_strLocation = p->Read(path + wxT("/Location"), wxEmptyString);
    m_version = p->Read(path + wxT("/Version"), wxEmptyString);

    m_port = (wxPortId)p->Read(path + wxT("/PortId"), 0l);
    m_format = (wxPackageCompilerFormat)p->Read(path + wxT("/Compiler"), 0l);

    p->Read(path + wxT("/Static"), &m_bStatic);
    p->Read(path + wxT("/Debug"), &m_bDebug);
    p->Read(path + wxT("/Unicode"), &m_bUnicode);

    return IsOk();
}

void wxWidgetsBuild::Save(wxConfigBase *p, const wxString &path) const
{
    p->Write(path + wxT("/Location"), m_strLocation);
    p->Write(path + wxT("/Version"), m_version);
    p->Write(path + wxT("/PortId"), (long)m_port);
    p->Write(path + wxT("/Compiler"), (long)m_format);

    p->Write(path + wxT("/Static"), m_bStatic);
    p->Write(path + wxT("/Debug"), m_bDebug);
    p->Write(path + wxT("/Unicode"), m_bUnicode);
}

// static
wxArrayString wxWidgetsBuild::GetSpecialOptionNames()
{
    // FIXME: maybe these vars and the way they are modified could
    //        be not-hardcoded somehow ?
    wxArrayString specialopt;
    specialopt.Add(wxT("WX_DIR"));
    specialopt.Add(wxT("WX_VERSION"));
    specialopt.Add(wxT("WX_SHARED"));
    specialopt.Add(wxT("WX_UNICODE"));
    specialopt.Add(wxT("WX_DEBUG"));

    return specialopt;
}

void wxWidgetsBuild::UpdateOptions(wxPackageCommandOptionArray &arr) const
{
    wxString ver = 
        wxString::Format(wxT("%d%d"), GetVersion().GetMajor(), GetVersion().GetMinor());

    arr.ItemByName(wxT("WX_DIR")).SetValue(GetLocation());
    arr.ItemByName(wxT("WX_VERSION")).SetValue(ver);
    arr.ItemByName(wxT("WX_SHARED")).SetValue(IsStaticBuild() ? wxT("0") : wxT("1"));
    arr.ItemByName(wxT("WX_UNICODE")).SetValue(IsUnicodeBuild() ? wxT("1") : wxT("0"));
    arr.ItemByName(wxT("WX_DEBUG")).SetValue(IsDebugBuild() ? wxT("1") : wxT("0"));
}



// ----------------------------------------------------------------------------
// wxWidgetsBuildArray
// ----------------------------------------------------------------------------

// static
bool wxWidgetsBuildArray::LoadGlobals(wxConfigBase *p, const wxString &path)
{
    wxWidgetsBuild temp;
    wxString str;
    long dummy;

    // try to load everything under path/WxBuilds
    p->SetPath(path + wxT("/WxBuilds/"));
    bool bCont = p->GetFirstGroup(str, dummy);
    while ( bCont )
    {
        if (temp.Load(p, str))
            s_arrWxBuilds.Add(temp);

        bCont = p->GetNextGroup(str, dummy);
    }

    return true;
}

void wxWidgetsBuildArray::SaveGlobals(wxConfigBase *p, const wxString &path)
{
    p->DeleteGroup(path + wxT("/WxBuilds"));
    for (size_t i=0; i<s_arrWxBuilds.GetCount(); i++)
        s_arrWxBuilds[i].Save(p, path + wxString::Format(wxT("/WxBuilds/build%d"), i));
}


