
// Created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2021 Hiperscan GmbH Dresden, Germany
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    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 General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.



using System;
using System.Diagnostics;
using Hiperscan.Common.I18n;

namespace Hiperscan.Extensions.ApoIdentSoftwareUpgrade
{
    public partial class Upgrader
    {
        private void Lookup()
        {
            CatalogLocale.LocaleType locale = this.Options.LocaleType;

            this.QuickStepLookupUnit = new LookupUnit(ProjectType.QuickStep, 
                                                      locale, 
                                                      0, 
                                                      this.VersionSearcher);
            this.QuickStepLookupUnit.Url = this.LinkManager.GetLink(ProjectType.QuickStep);
            this.StartLookupProcess(this.QuickStepLookupUnit);
            this.QuickStepLookupUnit.Write();

            this.IdentModuleLookupUnit = new LookupUnit(ProjectType.IdentModule, 
                                                        locale, 
                                                        0,
                                                        this.VersionSearcher);
            this.IdentModuleLookupUnit.Url = this.LinkManager.GetLink(ProjectType.IdentModule);
            this.StartLookupProcess(this.IdentModuleLookupUnit);
            this.IdentModuleLookupUnit.Write();

            this.QuantifierModuleLookopUnit = new LookupUnit(ProjectType.QuantifierModule,
                                                           locale, 
                                                             0,
                                                             this.VersionSearcher);
            this.QuantifierModuleLookopUnit.Url = this.LinkManager.GetLink(ProjectType.QuantifierModule);
            this.StartLookupProcess(this.QuantifierModuleLookopUnit);
            this.QuantifierModuleLookopUnit.Write();
        }

        private void StartLookupProcess(LookupUnit lookup_unit)
        {
            try
            {
                Process proc = new Process();
                proc.StartInfo.FileName = "curl";
                proc.StartInfo.Arguments = string.Format("-IL \"{0}\"", lookup_unit.Url);
                proc.StartInfo.CreateNoWindow = true;
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardOutput = true;

                proc.Start();
                proc.WaitForExit();

                if (proc.ExitCode != 0)
                    throw new Exception(String.Format("Exited with {0}", proc.ExitCode));

                lookup_unit.SetFromLookup(proc.StandardOutput.ReadToEnd());
            }
            catch (Exception ex)
            {
                Console.WriteLine("Lookup process failed: {0}", ex);
            }
        }
    }
}
