/////////////////////////////////////////////////////////////////////////////
// Name:        app.h
// Purpose:     wxWidgets command-line Package Manager and Packager
// Author:      Francesco Montorsi
// Modified by:
// Created:     2006-06-08
// RCS-ID:      $Id: app.h,v 1.10 2007/01/31 19:38:38 frm Exp $
// Copyright:   (c) Francesco Montorsi
// Licence:     wxWidgets license
/////////////////////////////////////////////////////////////////////////////

#ifndef _APP_H_
#define _APP_H_

#include "wx/app.h"
#include "wx/config.h"
#include "wx/filename.h"
#include "wx/apptrait.h"

#define DEFAULT_TEMPLATE        wxT("default.wxp")

// ----------------------------------------------------------------------------
// enums
// ----------------------------------------------------------------------------

enum wxWorkMode
{
    wxWM_INVALID = -1,

    // PM functions:
    wxWM_SHOWINFO_PACKAGE,
    wxWM_DOWNLOAD_PACKAGE,
    wxWM_DECOMPRESS_PACKAGE,
    wxWM_BUILD_PACKAGE,
    wxWM_INSTALL_PACKAGE,
    wxWM_UNINSTALL_PACKAGE,
    wxWM_LIST_PACKAGES,
    wxWM_LIST_REPOSITORIES,
    wxWM_ADD_REPOSITORY,
    wxWM_UPDATE,

    // PKG functions:
    wxWM_MAKE_TEMPLATE,
    wxWM_PACK,
    wxWM_UPLOAD,
    wxWM_CHECK,

    // additional functions:
    wxWM_GET_INFO,
    wxWM_CREATE_WXL,
    wxWM_OUTPUT_STAT,
    wxWM_OUTPUT_HTML,
    wxWM_LIST_VIEW_FORMATS,

    wxWM_MAX
};

wxDEFINE_STRING2ENUM(WorkMode)


// ----------------------------------------------------------------------------
// wxPackageConsoleApp
// ----------------------------------------------------------------------------

class wxPackageConsoleApp : public wxApp
{
public:
    wxPackageConsoleApp();
    virtual ~wxPackageConsoleApp();

	// Operations
    bool OnInit();
    bool RunCommand(wxWorkMode mode);
    int OnExit();
    int OnRun();

    // Helpers
    void Usage();
    bool InitLocalPackages();
    bool InitRemotePackages();
    bool InitLocalPackagesAndSearch(const wxString &pkgname);

    bool UpgradePackageTo(wxPackage &pkg, wxPackageStatus stat);

    wxArrayString GetValidParamFiles(int start, int end = -1);

    wxString GetRepoCacheFile(size_t i) const
    {
        return wxString::Format(wxT("%srepo%d.wxl"), 
                                m_strRepoCachePath.c_str(), i);
    }


public:     // core of the app

    bool ShowInfo(const wxString &pkgname);
    bool Download(const wxString &pkgname);
    bool Decompress(const wxString &pkgname, const wxString &path);

    bool RunStage(const wxString &pkgname, wxPackageCompilerFormat compiler, 
                  const wxStringHashMap &options, wxPackageBuildSystemStage stage);

    bool AddRepository(bool remote, const wxString &repo);
    bool ListRepositories(bool remote);
    bool ListPackages(bool remote);
    bool Update();

    bool OutputTemplate();
    bool Pack(const wxString &file);
    bool Upload(const wxString &file);
    bool Check(const wxString &file);
    
    bool GetPackage(wxPackageInfo &pkg, const wxString &file);

    bool GetInfo(const wxString &keyword, const wxString &packagefile);
    bool CreateWXL(const wxString &outfile, const wxArrayString &arr);
    bool OutputHTML(const wxString &viewmode, bool showSummary, const wxArrayString &file);
    bool OutputStat(const wxArrayString &arr);
    bool ListViewFormats();

protected:
    /// Our command-line parser.
    wxCmdLineParser m_cmdParser;

    /// The stdout.
    wxFile m_stdout;

    /// The array of local packages.
    wxPackageArray m_arrLocalPackage;

    /// The array of remote packages.
    wxPackageArray m_arrRemotePackage;

    /// The path to the cached WXL files.
    wxString m_strRepoCachePath;

    /// The template file.
    wxString m_strTemplate;
};

DECLARE_APP(wxPackageConsoleApp)

#endif
  // _APP_H_

