using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace TeslaConsole;

[RunInstaller(true)]
public class MigrateIsolatedStorage : Installer
{
	private IContainer components;

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		components = new Container();
	}

	public MigrateIsolatedStorage()
	{
		InitializeComponent();
	}

	public override void Commit(IDictionary savedState)
	{
		base.Commit(savedState);
		base.Context.LogMessage("Migrating IsolatedStorage");
		string path = base.Context.Parameters["targetdir"];
		string fileName = Path.GetFileName(Assembly.GetExecutingAssembly().CodeBase);
		string text = Path.Combine(path, fileName);
		base.Context.LogMessage("Running \"" + text + "\" /migrateIsoStore");
		using Process process = new Process();
		process.StartInfo.FileName = text;
		process.StartInfo.Arguments = "/migrateIsoStore";
		process.StartInfo.UseShellExecute = false;
		process.StartInfo.CreateNoWindow = true;
		if (!process.Start())
		{
			throw new InstallException("Unable to start IsolatedStorage migrator process.");
		}
		process.WaitForExit();
		if (process.ExitCode != 0)
		{
			throw new InstallException("IsolatedStorage migrator exited with code " + process.ExitCode);
		}
	}
}
