using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace SiemensTagOku
{
    static class Program
    {
        static Program()
        {
            var searchPaths = new List<string>();

            for (int ver = 20; ver >= 17; ver--)
            {
                string baseDir = $@"C:\Program Files\Siemens\Automation\Portal V{ver}";
                if (Directory.Exists(baseDir))
                {
                    searchPaths.Add($@"{baseDir}\PublicAPI\V{ver}");
                    searchPaths.Add($@"{baseDir}\PublicAPI\V{ver}\AddIn");
                    searchPaths.Add($@"{baseDir}\bin");
                }
            }

            AppDomain.CurrentDomain.AssemblyResolve += (sender, e) =>
            {
                foreach (var dir in searchPaths)
                {
                    string path = Path.Combine(dir, new AssemblyName(e.Name).Name + ".dll");
                    if (File.Exists(path))
                        return Assembly.LoadFrom(path);
                }
                return null;
            };
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
}
