package cwbinstaller.commands;

import java.io.File;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.util.Util;
import org.osgi.framework.Version;
import org.txm.core.preferences.TBXPreferences;
import org.txm.core.preferences.TXMPreferences;
import org.txm.utils.BundleUtils;
import org.txm.utils.logger.Log;

import cwbinstaller.CQPPreferences;

public class DoInstallStep extends org.txm.rcp.commands.TxmCommand {
	protected String name = "cwbbinaries.commands.DoInstallStep"; //$NON-NLS-1$
	
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void install() {	
		
	}
	
	@Override
	public void preInstall() {	
		String CQPFRAGMENT = "CQP."+System.getProperty("osgi.os");
		
		Log.info("CQP.DoInstallStep.install()");
		String saved = TXMPreferences.getString(CQPPreferences.PREFERENCES_NODE, CQPPreferences.VERSION, "");
		Version currentVersion = BundleUtils.getBundleVersion(CQPFRAGMENT); // the CQPFRAGMENT plugin contains the right version
		
		if (saved != null && saved.length() > 0) {
			Version savedVersion = new Version(saved);
			if (currentVersion.compareTo(savedVersion) <= 0) {
				Log.info("No post-installation of CQP to do");
				return; // nothing to do
			}
		}
		System.out.println("Updating CWB preferences for CQP version="+currentVersion);
		
		String os = "win";
		String ext = "";
		if (Util.isWindows()) { //$NON-NLS-1$
			if (System.getProperty("os.arch").contains("64")) { os = "win64"; } 
			else { os = "win32"; }
			ext = ".exe";
		} else if (Util.isMac()) { //$NON-NLS-1$
			os = "macosx";
		} else {
			os = "linux32";
			if (System.getProperty("os.arch").contains("64")) 
				os = "linux64";
		}

		File bundleDir = BundleUtils.getBundleFile(CQPFRAGMENT);
		if (bundleDir == null) {
			System.out.println("Error while retrieving TreeTaggerInstaller bundle directory.");
			return;
		}
		File cwbDir = new File(bundleDir, "res");
		File initFile = new File(cwbDir, "cqpserver.init");
		File OSDir = new File(cwbDir, os);
		File execFile = new File(OSDir, "cqpserver"+ext);

		TXMPreferences.putString(TBXPreferences.PREFERENCES_NODE, TBXPreferences.CQI_SERVER_PATH_TO_CQPLIB, OSDir.getAbsolutePath());
		TXMPreferences.putString(TBXPreferences.PREFERENCES_NODE, TBXPreferences.CQI_SERVER_PATH_TO_INIT_FILE, initFile.getAbsolutePath());
		TXMPreferences.putString(TBXPreferences.PREFERENCES_NODE, TBXPreferences.CQI_SERVER_PATH_TO_EXECUTABLE, execFile.getAbsolutePath());
		if (!Util.isWindows()) { 
			try {
				Log.info("Setting execution file rights to: "+OSDir.getAbsolutePath());
				Runtime.getRuntime().exec("chmod -R +x "+OSDir.getAbsolutePath()).waitFor();
			} catch (Exception e) {
				System.out.println("Error while setting execution file rights to: "+OSDir.getAbsolutePath());
				e.printStackTrace();
			}
		}
	
		if (!execFile.canExecute()) {
			System.out.println("File right setting error, cannot execute: "+execFile);
		}
		Log.warning("SearchEngine preferences set with: "+OSDir.getAbsolutePath()+ ", "+initFile.getAbsolutePath()+ " and "+execFile.getAbsolutePath());
		TXMPreferences.putString(CQPPreferences.PREFERENCES_NODE, CQPPreferences.VERSION, currentVersion.toString());
	}
}
