// Created with MonoDevelop
//
//    Copyright (C) 2018 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.IO;
using System.Reflection;

namespace Hiperscan.Common
{
    public static class Misc
    {
        public static string MonoVersion
        {
            get
            {
                try
                {
                    string version = String.Empty;

                    Type type = Type.GetType("Mono.Runtime");
                    if (type != null)
                    {
                        MethodInfo displayName = type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static);
                        if (displayName != null)
                            version = displayName.Invoke(null, null).ToString();
                    }

                    return version;
                }
                catch(Exception)
                {
                    return String.Empty;
                }
            }
        }

        public static string AssemblyVersion
        {
            get
            {
                return Assembly.GetEntryAssembly().GetName().Version.ToString();
            }
        }

        public static double ParseDoubleFromEnv(string variable_name, double default_val)
        {
            string variable_value = Environment.GetEnvironmentVariable(variable_name);
            if (string.IsNullOrEmpty(variable_value))
                return default_val;

            double parsed_val = double.Parse(variable_value, System.Globalization.CultureInfo.InvariantCulture);
            Console.Error.WriteLine("Using Environment variable {0}={1} to set internal double parameter.", variable_name, parsed_val);

            return parsed_val;
        }
    }
}
