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

import pl.zalewski.mikolaj.symbdev.IWin32ToolConsumer;
import pl.zalewski.mikolaj.symbdev.IWin32ToolDetector;
import pl.zalewski.mikolaj.symbdev.Win32ToolEntry;
import com.ice.jni.registry.*;

/**
 * @author mikolaj
 *
 * Autodetects the Borland C++Builder 5/6 and C++BuilderX
 */
public class BorlandToolDetect implements IWin32ToolDetector
{
  final int BORLAND_COMPILERS_ID_MIN=Win32ToolEntry.AUTODETECTED_MIN+0x10000;
  final int BORLAND_BUILDERX_OFFSET=0x1000;
	
  final String BORLAND_CPPBUILDER_KEY="Software\\Borland\\C++Builder\\";
  final String BORLAND_CPPBUILDER_PATH="RootDir";
  final String BORLAND_CPPBUILDERX_KEY="Software\\Borland\\C++BuilderX\\";
  final String BORLAND_CPPBUILDERX_PATH="PathName";
	
  Win32ToolEntry getBuilderEntry(String version, String versionName, int nr)
  {
  	String path=null;
  	try {
  		RegistryKey key=Registry.HKEY_LOCAL_MACHINE.openSubKey(BORLAND_CPPBUILDER_KEY+version);
  		path=key.getStringValue(BORLAND_CPPBUILDER_PATH);
  		key.closeKey();
  	}
  	catch (RegistryException e)
	{
  		if (!(e instanceof NoSuchKeyException) &&
  			!(e instanceof NoSuchValueException))
  			e.printStackTrace();
  	    return null;
	}
  	
  	Win32ToolEntry result=new Win32ToolEntry();
  	result.name="Borland C++Builder"+versionName;
  	result.path=path+"\\bin";
  	result.target="winsb";
  	result.autodetected=true;
  	result.id=BORLAND_COMPILERS_ID_MIN+BORLAND_BUILDERX_OFFSET+nr;
  	return result;
  }

  Win32ToolEntry getBuilderXEntry(String version, String versionName, int nr)
  {
  	String path=null;
  	try {
  		RegistryKey key=Registry.HKEY_LOCAL_MACHINE.openSubKey(BORLAND_CPPBUILDERX_KEY+version);
  		path=key.getStringValue(BORLAND_CPPBUILDERX_PATH);
  		key.closeKey();
  	}
  	catch (RegistryException e)
	{
  		if (!(e instanceof NoSuchKeyException) &&
  			!(e instanceof NoSuchValueException))
  			e.printStackTrace();
  	    return null;
	}
  	
  	Win32ToolEntry result=new Win32ToolEntry();
  	result.name="Borland C++BuilderX"+versionName;
  	result.path=path+"\\bin";
  	result.target="winsb";
  	result.autodetected=true;
  	result.id=BORLAND_COMPILERS_ID_MIN+nr;
  	return result;
  }  
  
  public void detect(IWin32ToolConsumer consumer)
  {
  	Win32ToolEntry ent;
  	ent=getBuilderXEntry("1.0", "", 1); if (ent!=null) consumer.entryDetected(ent);
  	ent=getBuilderEntry("6.0", " 6", 6); if (ent!=null) consumer.entryDetected(ent);
  	ent=getBuilderEntry("5.0", " 5", 5); if (ent!=null) consumer.entryDetected(ent);
  }
}
