////////////////////////////////////////////////////////////////////////////////
//
// Code::Blocks new project wizard script
//
// Project: MCS51 project
// Author:  H. Metin OZER
//
////////////////////////////////////////////////////////////////////////////////

// Global Vars
MemoryModel <- _T("");     // The chosen memory model
IntelHex <- true;         // produce Intel Hex file?

function BeginWizard()
{
    local wiz_type = Wizard.GetWizardType();

    if (wiz_type == wizProject)
    {
        local intro_msg = _T("Welcome to the MCS51 project wizard!\n" +
                             "This wizard will guide you to create a new MCS51 project.\n\n" +
                             "When you 're ready to proceed, please click \"Next\"...");

        Wizard.AddInfoPage(_T("MCS51Intro"), intro_msg);
        Wizard.AddProjectPathPage();
        Wizard.AddCompilerPage(_T("SDCC Compiler"), _T("sdcc*"), false, true);
        Wizard.AddPage(_T("memoryModelChoice"));
    }
    else
        print(wiz_type);
}

function GetFilesDir()
{
    local result = _T("mcs51/files");
    return result;
}

////////////////////////////////////////////////////////////////////////////////
// Memory Model choice page
////////////////////////////////////////////////////////////////////////////////


function OnLeave_memoryModelChoice(fwd)
{
    if (fwd)
    {
        MemoryModel = Wizard.GetComboboxStringSelection(_T("comboboxMem"));
        if ( Wizard.GetRadioboxSelection(_T("radioboxOutput")) == 1 )
        {
            IntelHex = false;
        }
    }
    return true;
}

function SetupProject(project)
{
    //Variables
    local out_ext = ::wxString();

    // Post Build steps
    local pb_hex = ::wxString();

    // Post build commands
    if (PLATFORM_MSW == PLATFORM)
        pb_hex = _T("cmd /c \"packihx <$(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME).ihx >$(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME).hex\"");
    else
        pb_hex = _T("packihx <$(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME).ihx >$(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME).hex");

    // Project compiler options
    project.AddCompilerOption(_T("-mmcs51"));
    project.AddCompilerOption(_T("--model-") + MemoryModel);

    // Project linker options

    // Project post-build steps
    if (IntelHex)
    {
        project.AddLinkerOption(_T("--out-fmt-ihx"));
		project.AddCommandsAfterBuild(pb_hex);
        out_ext = _T(".ihx");
    }
    else // (MotS19)
    {
        project.AddLinkerOption(_T("--out-fmt-s19"));
        out_ext = _T(".s19");
    }

    // Debug build target
    local target = project.GetBuildTarget(Wizard.GetDebugName());
    if (!IsNull(target))
    {
        target.SetTargetType(ttNative);
        target.SetTargetFilenameGenerationPolicy(tgfpPlatformDefault, tgfpNone);
        target.SetOutputFilename(Wizard.GetDebugOutputDir() + Wizard.GetProjectName() + out_ext);
        DebugSymbolsOn(target, Wizard.GetCompilerID());
    }

    // Release build target
    target = project.GetBuildTarget(Wizard.GetReleaseName());
    if (!IsNull(target))
    {
        target.SetTargetType(ttNative);
        target.SetTargetFilenameGenerationPolicy(tgfpPlatformDefault, tgfpNone);
        target.SetOutputFilename(Wizard.GetReleaseOutputDir() + Wizard.GetProjectName() + out_ext);
        OptimizationsOn(target, Wizard.GetCompilerID());
    }

    return true;
}
