// Program.cs created with MonoDevelop
//
// Copyright (C) 2018  Thomas Klose & Torsten Schache, Hiperscan GmbH Dresden, Germany
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program; if not, see http://www.gnu.org/licenses or write to
// the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA, 02110-1301 USA
//

using System;
using System.IO;
using System.Reflection;
using Hiperscan.Spectroscopy;
using Hiperscan.Spectroscopy.IdentModule;
using Hiperscan.Unix;
using Microsoft.Win32;

namespace Hiperscan.UpdateCertificateStarter
{
    class MainClass
    {
        public static void Main(string[] args)  
        {  
            try
            {
                string file = Assembly.GetExecutingAssembly().Location;
                file = Directory.GetParent(file).FullName;
                file = Directory.GetParent(file).FullName;
                file = Directory.GetParent(file).FullName;
                file = Path.Combine(file, "IdentServer");
                file = Path.Combine(file, Extensions.ApoIdentCommon.DefaultNames.IdentServer.IDENT_MODULE_VERSION_FILE);

                string path = String.Empty;

                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i] == "--log")
                        continue;

                    path = args[i];
                    break;
                }

                if (String.IsNullOrEmpty(path))
                path = Path.Combine(".", "..", "..", "Messages");
                path = Path.GetFullPath(path);

                Catalog.Init("Hiperscan", path);

                IdentModuleInfo ident_module_info = IdentModuleInfo.Read(file);

                UpdateCertificateInfo info = new UpdateCertificateInfo();
                info.Version = ident_module_info.ShortIdentModuleVersion;

                try
                {
                    string reg_im_path = "SOFTWARE\\Hiperscan\\IdentModule " + info.Version.Split('-')[0];

                    RegistryKey rk = Registry.LocalMachine.OpenSubKey(reg_im_path, false);
                    string date = (string)rk.GetValue("InstallDate");
                    string[] tmp_date = date.Split(new char[] { '-', ':', ' ', '.' });
                    int day = Int32.Parse(tmp_date[0]);
                    int month = Int32.Parse(tmp_date[1]);
                    int year = Int32.Parse(tmp_date[2]);
                    int hour = Int32.Parse(tmp_date[3]);
                    int minute = Int32.Parse(tmp_date[4]);
                    int second = Int32.Parse(tmp_date[5]);

                    info.Date = new DateTime(year, month, day, hour, minute, second);
                }
                catch
                {
                    info.Date = DateTime.Now;
                }

                string dir = Path.GetTempPath();
                string out_file = Path.Combine(dir, Extensions.ApoIdentCommon.DefaultNames.IdentServer.UPDATE_CERTIFICATE_FILE);

                if (File.Exists(out_file) == true)
                {
                    try
                    {
                        File.Delete(out_file);
                    }
                    catch (Exception) { }
                }

                Hiperscan.UpdateCertificate.UpdateCertificate.CreateCertificate(info, dir);

                string url = "file://" + out_file.Replace("\\", "/");
                System.Diagnostics.Process.Start(url);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        } 
    }
}
