/*
 * Created on 2004-08-17
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2004
 */
package pl.zalewski.mikolaj.symbdev.tooldetect;

import org.eclipse.core.runtime.Path;

import pl.zalewski.mikolaj.symbdev.IWin32ToolConsumer;
import pl.zalewski.mikolaj.symbdev.IWin32ToolDetector;
import pl.zalewski.mikolaj.symbdev.SymbdevPlugin;
import pl.zalewski.mikolaj.symbdev.Win32ToolEntry;

/**
 * @author mikolaj
 *
 * This class detect the combination of:
 *   Visual C++ Toolkit 1.01
 *   .NET Framework SDK
 *   Platform SDK
 * Which contains all the tools needed for WINS compilation
 */
public class VisualToolkitDetect implements IWin32ToolDetector {

    final int VISUAL_TOOLKIT_ID_MIN=Win32ToolEntry.AUTODETECTED_MIN+0x20000;
    
    /**
     * Default constructor 
     */
    public VisualToolkitDetect() {
        super();
    }
    
    static final String KEY_UNINSTALL="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\";
    static final String PATH_VALUE="InstallLocation";
    
    static final String KEY_FRAMEWORK="SOFTWARE\\Microsoft\\.NETFramework"; 
    static final String FRAMEWORK_VALUE="sdkInstallRootv1.1";
    static final String FRAMEWORK_20_VALUE="sdkInstallRootv2.0";
    
    static final String KEY_PLATFORM_SDK="SOFTWARE\\Microsoft\\MicrosoftSDK\\Directories";
    static final String VALUE_PLATFORM_SDK="Install Dir";
    
    String detectVisualToolkit(String guid)
    {
        String path=SymbdevPlugin.getHKLMRegValueString(KEY_UNINSTALL+guid, PATH_VALUE);
        return path+"bin\\";
    }

    String detectFrameworkSDK()
    {
        String path=SymbdevPlugin.getHKLMRegValueString(KEY_FRAMEWORK, FRAMEWORK_VALUE);
        if (path==null) path=SymbdevPlugin.getHKLMRegValueString(KEY_FRAMEWORK, FRAMEWORK_20_VALUE);
        if (path==null) return null;
        return new Path(path).append("bin\\").toOSString();
    }

    String detectPlatformSDK()
    {
        String path=SymbdevPlugin.getHKLMRegValueString(KEY_PLATFORM_SDK, VALUE_PLATFORM_SDK);
        if (path==null) return null;
        return new Path(path).append("bin\\").toOSString();
    }
    
    /* (non-Javadoc)
     * @see pl.zalewski.mikolaj.symbdev.IWin32ToolDetector#detect(pl.zalewski.mikolaj.symbdev.IWin32ToolConsumer)
     */
    public void detect(IWin32ToolConsumer consumer) {
        String toolkit=detectVisualToolkit("{362882AE-E40A-4435-B214-6420634C401F}");
        if (toolkit==null) return;
        String frameworkSDK=detectFrameworkSDK();
        if (frameworkSDK==null) return;
        String platformSDK=detectPlatformSDK();
        if (platformSDK==null) return;
        
        Win32ToolEntry entry=new Win32ToolEntry();
        entry.id=VISUAL_TOOLKIT_ID_MIN;
        entry.name="Visual C++ Toolkit-based toolchain";
        entry.path=toolkit+';'+frameworkSDK+';'+platformSDK+';'+platformSDK+"Win64\\;"+platformSDK+"WinNT\\";
        entry.target="wins";
        entry.autodetected=true;
        consumer.entryDetected(entry);
    }

}
