using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using CommonLib.IO;
using CommonLib.Misc;
using CommonLib.Windows.Forms;

namespace All_Purpose_Auto_Setup
{
   class GutsSoftwareBuildAssistant
   {
      // An event that can be raised, allowing other classes to
      // subscribe to it and do what they like with the message.
      public event Action<string, string, List<CommonLib.Windows.Forms.TextFormat.TextFontAndColor>> UpdateStatusDisplayAndLogEvent;
      public event Action RestartApplication;

      public List<ConfigFileManager.Ini_KeyValue<string>> gutsPrebuildParams = new List<ConfigFileManager.Ini_KeyValue<string>>();

      Form m_parentForm;

      public GutsSoftwareBuildAssistant(Form parentForm)
      {
         m_parentForm = parentForm;
      }

      // if one were to build GUTS from the files pulled from Synergy, the build will fail because
      // additional files (source and headers) must be added to the GUTS project in order to build
      // GUTS successfully.  There's a perl script (dashboard.pl) that creates all the necessary files.
      // But before the dashboard.pl can create all additional files, that path to the GUTS project 
      // must be specified in the windows environmental system variables and that the path to the Perl
      // executable must be in the system Path variable.  If these 2 things are not correct, the dashboard.pl
      // will not be able to copy files to the GUTS project folder and GUTS will fail the build
      // this function will ensure that everything is right to run the dashboard.pl and that it can create
      // files and add them to the GUTS project folders.
      public bool gutsPrebuildCheck(string sectionName)
      {
         string msg, indentation = String.Empty, gutsRootPath = "";
         bool setupSuccessful = true;
         ConfigFileManager.Ini_KeyValue<string> gutsDevelopmentFolder = new ConfigFileManager.Ini_KeyValue<string>("");

         CommonLib.Windows.Forms.TextFormat.TextFontAndColor defaultTextProp = new CommonLib.Windows.Forms.TextFormat.TextFontAndColor("");
         CommonLib.Windows.Forms.TextFormat.TextFontAndColor textProp = new CommonLib.Windows.Forms.TextFormat.TextFontAndColor("");
         List<CommonLib.Windows.Forms.TextFormat.TextFontAndColor> textPropList = new List<CommonLib.Windows.Forms.TextFormat.TextFontAndColor>();

         // set font to default text and color
         textProp.textFont = defaultTextProp.textFont;
         textProp.textColor = Common.getSetupStepTitleColor();
         textProp.wordIndex = 0;
         textProp.wordCount = 100;
         textPropList.Add(textProp);

         DateTime dat1 = DateTime.Now;
         msg = "\n\n<-------------------Date and Time: " + dat1.ToString(@"MM/dd/yyyy     hh:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture) + "------------------->";

         UpdateStatusDisplayAndLogEvent(msg, msg, textPropList);
         // set font to default text and color
         textProp.textFont = defaultTextProp.textFont;
         textProp.textColor = Common.getSetupStepTitleColor();
         textProp.wordIndex = 0;
         textProp.wordCount = 10;
         // set font size
         textProp.textFont = new Font(textProp.textFont.Name, 12);
         // set font style
         textProp.textFont = new Font(textProp.textFont, FontStyle.Bold);
         textPropList.Add(textProp);

         msg = "\n-=GUTS Pre-build Check=-";
         UpdateStatusDisplayAndLogEvent(msg, msg, textPropList);

         gutsPrebuildParams.Clear();

         ConfigFileManager.parseConfigInfo(sectionName, gutsPrebuildParams);

         if (setupSuccessful)
         {
            setupSuccessful = verifyGutsDirectoryStructure(ref gutsRootPath);

            if (setupSuccessful)
               setupSuccessful = runGutsPrebuildScript(gutsRootPath);
         }

         return setupSuccessful;
      }

      bool verifyGutsDirectoryStructure(ref string gutsRootPath)
      {
         bool setupSuccessful = true;
         string msg, indentation = String.Empty;
         Match regExMatch;
         ConfigFileManager.Ini_KeyValue<string> gutsDevelopmentFolder = new ConfigFileManager.Ini_KeyValue<string>("");
         List<string> paths = new List<string>();

         CommonLib.Windows.Forms.TextFormat.TextFontAndColor defaultTextProp = new CommonLib.Windows.Forms.TextFormat.TextFontAndColor("");
         CommonLib.Windows.Forms.TextFormat.TextFontAndColor textProp = new CommonLib.Windows.Forms.TextFormat.TextFontAndColor("");
         List<CommonLib.Windows.Forms.TextFormat.TextFontAndColor> textPropList = new List<CommonLib.Windows.Forms.TextFormat.TextFontAndColor>();

         gutsRootPath = "";

         gutsDevelopmentFolder = getGutsDevelopmentFolderIniInfo();
         if (gutsDevelopmentFolder.iniValue.Length == 0)
         {
            indentation = Common.getIndentation(1);
            msg = "\n" + indentation + "- FAILED. "
                  + "\n" + indentation + "File: " + ConfigFileManager.ms_configGeneralInfo.pathAndNameOfConfigFile
                  + "\n" + indentation + "Section: " + gutsDevelopmentFolder.iniSectionName
                  + "\n" + indentation + "Key: " + gutsDevelopmentFolder.iniKeyName
                  + "\n" + indentation + "Value: " + gutsDevelopmentFolder.iniValue
                  + "\n" + indentation + "Error Description: The key or value is missing for the above section in the INI file.";

            Common.formatConfigIniFailureMessage(msg, textPropList);

            UpdateStatusDisplayAndLogEvent(msg, msg, textPropList);

            setupSuccessful = false;
         }

         if (setupSuccessful)
         {
            regExMatch = Regex.Match(gutsDevelopmentFolder.iniValue, @"(.+\\GUTS[^\\]*)\\?.*", RegexOptions.IgnoreCase);

            if (regExMatch.Success)
            {
               gutsRootPath = regExMatch.Groups[1].Value;
            }
            else
               gutsRootPath = PathManip.RemoveTrailingSlashInPath(gutsDevelopmentFolder.iniValue) + "\\" + "GUTS";

            paths.Add(gutsRootPath);
            paths.Add(gutsRootPath + "\\SM3_Tools\\Dashboard");
            paths.Add(gutsRootPath + "\\SM3\\src\\app");

            foreach (string path in paths)
            {
               indentation = Common.getIndentation(1);
               msg = "\n" + indentation + "Verify path \"" + PathManip.GetProperDirectoryCapitalization(path) + "\"";

               if (Directory.Exists(path))
               {
                  msg += " - SUCCESS.";

                  // set font to default text and color
                  textProp.textFont = defaultTextProp.textFont;
                  textProp.textColor = Common.getSetupStatusColor(Common.EXECUTION_STATUS.SUCCESS);
                  textProp.wordIndex = StringManip.GetPhraseWordIndexInText(msg, "SUCCESS.");
                  textProp.wordCount = 1;
                  // set font style
                  textProp.textFont = new Font(textProp.textFont, FontStyle.Bold);
                  textPropList.Add(textProp);
               }
               else
               {
                  msg += " - FAILED.";

                  // set font to default text and color
                  textProp.textFont = defaultTextProp.textFont;
                  textProp.textColor = Common.getSetupStatusColor(Common.EXECUTION_STATUS.FAILURE);
                  textProp.wordIndex = StringManip.GetPhraseWordIndexInText(msg, "FAILED.");
                  textProp.wordCount = 1;
                  // set font style
                  textProp.textFont = new Font(textProp.textFont, FontStyle.Bold);
                  textPropList.Add(textProp);

                  setupSuccessful = false;
               }

               UpdateStatusDisplayAndLogEvent(msg, msg, textPropList);

               if (!setupSuccessful)
                  break;
            }
         }

         return setupSuccessful;
      }

      bool runGutsPrebuildScript(string gutsRootPath)
      {
         bool environmentVarChanged = false;
         string msg = "", additionalFileMissing = "", errMsg = "", indentation, dashboardScriptPath, currentPathEnvVar = "", currentSm3SeekerEnvVar = "";
         bool setupSuccessful = true;
         ConfigFileManager.Ini_KeyValue<string> genericIniInfo = new ConfigFileManager.Ini_KeyValue<string>("");
         ConfigFileManager.Ini_KeyValue<string> pathAndNameOfAddtionalFilesList = new ConfigFileManager.Ini_KeyValue<string>("");
         ConfigFileManager.Ini_KeyValue<string> perlBinaryPath = new ConfigFileManager.Ini_KeyValue<string>("");

         CommonLib.Windows.Forms.TextFormat.TextFontAndColor defaultTextProp = new CommonLib.Windows.Forms.TextFormat.TextFontAndColor("");
         CommonLib.Windows.Forms.TextFormat.TextFontAndColor textProp = new CommonLib.Windows.Forms.TextFormat.TextFontAndColor("");
         List<CommonLib.Windows.Forms.TextFormat.TextFontAndColor> textPropList = new List<CommonLib.Windows.Forms.TextFormat.TextFontAndColor>();

         pathAndNameOfAddtionalFilesList = getPathAndNameOfAddtionalFileList();
         genericIniInfo = pathAndNameOfAddtionalFilesList;

         perlBinaryPath = getPerlBinaryPathIniInfo();
         if (genericIniInfo.iniValue.Length > 0)
            genericIniInfo = perlBinaryPath;

         if (genericIniInfo.iniValue.Length == 0)
         {
            indentation = Common.getIndentation(1);
            msg = "\n" + indentation + "- FAILED. "
                  + "\n" + indentation + "File: " + ConfigFileManager.ms_configGeneralInfo.pathAndNameOfConfigFile
                  + "\n" + indentation + "Section: " + genericIniInfo.iniSectionName
                  + "\n" + indentation + "Key: " + genericIniInfo.iniKeyName
                  + "\n" + indentation + "Value: " + genericIniInfo.iniValue
                  + "\n" + indentation + "Error Description: The key or value is missing for the above section in the INI file.";

            Common.formatConfigIniFailureMessage(msg, textPropList);

            UpdateStatusDisplayAndLogEvent(msg, msg, textPropList);

            setupSuccessful = false;
         }

         if (setupSuccessful)
         {
            if (File.Exists(pathAndNameOfAddtionalFilesList.iniValue))
            {
               verifyAdditionalFilesAlreadyExistInGutsProject(gutsRootPath, pathAndNameOfAddtionalFilesList.iniValue, ref additionalFileMissing, ref errMsg);

               if (errMsg.Length > 0)
               {
                  indentation = Common.getIndentation(1);
                  msg = "\n" + indentation + "Parsing file \"" + pathAndNameOfAddtionalFilesList.iniValue + "\" - FAILED. " + errMsg;

                  // set font to default text and color
                  textProp.textFont = defaultTextProp.textFont;
                  textProp.textColor = Common.getSetupStatusColor(Common.EXECUTION_STATUS.FAILURE);
                  textProp.wordIndex = StringManip.GetPhraseWordIndexInText(msg, "FAILED.");
                  textProp.wordCount = 1;
                  // set font style
                  textProp.textFont = new Font(textProp.textFont, FontStyle.Bold);
                  textPropList.Add(textProp);

                  setupSuccessful = false;
               }
               else if (additionalFileMissing.Length > 0)
               {
                  ((frmSetupStatusDisplay)m_parentForm).displayMessageBox(MessageBoxCustom.PopUpMsgType.INFO, null, "File \"" + additionalFileMissing + "\" is missing. \n\nProceeding with running pre-build script to create necessary files.", "Info", -1);
               }
               else
               {
                  indentation = Common.getIndentation(1);
                  msg = "\n" + indentation + "Verify additional files already exist within \"" + gutsRootPath + "\" - SUCCESS.";

                  // set font to default text and color
                  textProp.textFont = defaultTextProp.textFont;
                  textProp.textColor = Common.getSetupStatusColor(Common.EXECUTION_STATUS.SUCCESS);
                  textProp.wordIndex = StringManip.GetPhraseWordIndexInText(msg, "SUCCESS.");
                  textProp.wordCount = 1;
                  // set font style
                  textProp.textFont = new Font(textProp.textFont, FontStyle.Bold);
                  textPropList.Add(textProp);
               }
            }
            else
            {
               indentation = Common.getIndentation(1);
               msg = "\n" + indentation + "Verify file \"" + pathAndNameOfAddtionalFilesList.iniValue + "\" - FAILED.";

               // set font to default text and color
               textProp.textFont = defaultTextProp.textFont;
               textProp.textColor = Common.getSetupStatusColor(Common.EXECUTION_STATUS.FAILURE);
               textProp.wordIndex = StringManip.GetPhraseWordIndexInText(msg, "FAILED.");
               textProp.wordCount = 1;
               // set font style
               textProp.textFont = new Font(textProp.textFont, FontStyle.Bold);
               textPropList.Add(textProp);

               setupSuccessful = false;
            }

            if (msg.Length > 0)
               UpdateStatusDisplayAndLogEvent(msg, msg, textPropList);
         }

         // at least 1 required file is missing from the GUTS project folder, we must run script to copy all the necessary files to GUTS folder
         if (setupSuccessful && additionalFileMissing.Length > 0)
         {
            dashboardScriptPath = gutsRootPath + @"\SM3_Tools\Dashboard\dashboard.pl";

            if (!File.Exists(dashboardScriptPath))
            {
               indentation = Common.getIndentation(1);
               msg = "\n" + indentation + "Verify file \"" + dashboardScriptPath + "\" - FAILED. File does not exist";

               // set font to default text and color
               textProp.textFont = defaultTextProp.textFont;
               textProp.textColor = Common.getSetupStatusColor(Common.EXECUTION_STATUS.FAILURE);
               textProp.wordIndex = StringManip.GetPhraseWordIndexInText(msg, "FAILED.");
               textProp.wordCount = 1;
               // set font style
               textProp.textFont = new Font(textProp.textFont, FontStyle.Bold);
               textPropList.Add(textProp);

               UpdateStatusDisplayAndLogEvent(msg, msg, textPropList);

               setupSuccessful = false;
            }

            string perlAppPath = PathManip.AddTrailingSlashToPath(perlBinaryPath.iniValue) + "Perl.exe";
            if (!File.Exists(perlAppPath))
            {
               indentation = Common.getIndentation(1);
               msg = "\n" + indentation + "Verify file \"" + perlAppPath + "\" - FAILED. File does not exist. Please install Perl.";

               // set font to default text and color
               textProp.textFont = defaultTextProp.textFont;
               textProp.textColor = Common.getSetupStatusColor(Common.EXECUTION_STATUS.FAILURE);
               textProp.wordIndex = StringManip.GetPhraseWordIndexInText(msg, "FAILED.");
               textProp.wordCount = 1;
               // set font style
               textProp.textFont = new Font(textProp.textFont, FontStyle.Bold);
               textPropList.Add(textProp);

               UpdateStatusDisplayAndLogEvent(msg, msg, textPropList);

               setupSuccessful = false;
            }

            if (setupSuccessful)
            {
               try
               {
                  currentPathEnvVar = System.Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine);

                  perlBinaryPath.iniValue = PathManip.RemoveTrailingSlashInPath(perlBinaryPath.iniValue);
               }
               catch (Exception e)
               {
                  indentation = Common.getIndentation(1);
                  msg = "\n" + indentation + "Querying system environment variable \"Path\" - FAILED. (" + e.Message + ")";

                  // set font to default text and color
                  textProp.textFont = defaultTextProp.textFont;
                  textProp.textColor = Common.getSetupStatusColor(Common.EXECUTION_STATUS.FAILURE);
                  textProp.wordIndex = StringManip.GetPhraseWordIndexInText(msg, "FAILED."); ;
                  textProp.wordCount = 1;
                  // set font style
                  textProp.textFont = new Font(textProp.textFont, FontStyle.Bold);
                  textPropList.Add(textProp);

                  UpdateStatusDisplayAndLogEvent(msg, msg, textPropList);

                  setupSuccessful = false;
               }
            }

            if (setupSuccessful)
            {
               try
               {
                  currentSm3SeekerEnvVar = System.Environment.GetEnvironmentVariable("SM3_SEEKER", EnvironmentVariableTarget.Machine);
               }
               catch (Exception e)
               {
                  indentation = Common.getIndentation(1);
                  msg = "\n" + indentation + "Querying system environment variable \"SM3_SEEKER\" - FAILED. (" + e.Message + ")";

                  // set font to default text and color
                  textProp.textFont = defaultTextProp.textFont;
                  textProp.textColor = Common.getSetupStatusColor(Common.EXECUTION_STATUS.FAILURE);
                  textProp.wordIndex = StringManip.GetPhraseWordIndexInText(msg, "FAILED."); ;
                  textProp.wordCount = 1;
                  // set font style
                  textProp.textFont = new Font(textProp.textFont, FontStyle.Bold);
                  textPropList.Add(textProp);

                  UpdateStatusDisplayAndLogEvent(msg, msg, textPropList);

                  setupSuccessful = false;
               }
            }

            if (setupSuccessful)
            {
               // if the perl binary path is not specified in the system environment variable "Path", we add it
               if (!Regex.IsMatch(currentPathEnvVar, @".*" + Regex.Escape(perlBinaryPath.iniValue) + @".*", RegexOptions.IgnoreCase))
               {
                  currentPathEnvVar += ";" + perlBinaryPath.iniValue;

                  indentation = Common.getIndentation(1);
                  msg = "\n" + indentation + "Setting environment variable [Path=$Path;" + perlBinaryPath.iniValue + "]";
                  try
                  {
                     System.Environment.SetEnvironmentVariable("Path", currentPathEnvVar, EnvironmentVariableTarget.Machine);

                     environmentVarChanged = true;

                     msg += " - SUCCESS.";

                     // set font to default text and color
                     textProp.textFont = defaultTextProp.textFont;
                     textProp.textColor = Common.getSetupStatusColor(Common.EXECUTION_STATUS.SUCCESS);
                     textProp.wordIndex = StringManip.GetPhraseWordIndexInText(msg, "SUCCESS."); ;
                     textProp.wordCount = 1;
                     // set font style
                     textProp.textFont = new Font(textProp.textFont, FontStyle.Bold);
                     textPropList.Add(textProp);
                  }
                  catch (Exception e)
                  {
                     msg += " - FAILED. (" + e.Message + ")";

                     // set font to default text and color
                     textProp.textFont = defaultTextProp.textFont;
                     textProp.textColor = Common.getSetupStatusColor(Common.EXECUTION_STATUS.FAILURE);
                     textProp.wordIndex = StringManip.GetPhraseWordIndexInText(msg, "FAILED."); ;
                     textProp.wordCount = 1;
                     // set font style
                     textProp.textFont = new Font(textProp.textFont, FontStyle.Bold);
                     textPropList.Add(textProp);

                     setupSuccessful = false;
                  }

                  UpdateStatusDisplayAndLogEvent(msg, msg, textPropList);

               }
            }

            if (setupSuccessful)
            {
               // if the SM3_SEEKR path does not match the GUTS Development Folder path, we change it
               if (!Regex.IsMatch(currentSm3SeekerEnvVar, @"^" + Regex.Escape(gutsRootPath) + @"\\SM3$", RegexOptions.IgnoreCase))
               {
                  indentation = Common.getIndentation(1);
                  msg = "\n" + indentation + "Setting environment variable [SM3_SEEKER=" + gutsRootPath + @"\SM3]";
                  try
                  {
                     System.Environment.SetEnvironmentVariable("SM3_SEEKER", gutsRootPath + @"\SM3", EnvironmentVariableTarget.Machine);

                     environmentVarChanged = true;

                     msg += " - SUCCESS.";

                     // set font to default text and color
                     textProp.textFont = defaultTextProp.textFont;
                     textProp.textColor = Common.getSetupStatusColor(Common.EXECUTION_STATUS.SUCCESS);
                     textProp.wordIndex = StringManip.GetPhraseWordIndexInText(msg, "SUCCESS."); ;
                     textProp.wordCount = 1;
                     // set font style
                     textProp.textFont = new Font(textProp.textFont, FontStyle.Bold);
                     textPropList.Add(textProp);
                  }
                  catch (Exception e)
                  {
                     msg += " - FAILED. (" + e.Message + ")";

                     // set font to default text and color
                     textProp.textFont = defaultTextProp.textFont;
                     textProp.textColor = Common.getSetupStatusColor(Common.EXECUTION_STATUS.FAILURE);
                     textProp.wordIndex = StringManip.GetPhraseWordIndexInText(msg, "FAILED."); ;
                     textProp.wordCount = 1;
                     // set font style
                     textProp.textFont = new Font(textProp.textFont, FontStyle.Bold);
                     textPropList.Add(textProp);

                     setupSuccessful = false;
                  }

                  UpdateStatusDisplayAndLogEvent(msg, msg, textPropList);
               }
            }

            if (environmentVarChanged)
            {
               ((frmSetupStatusDisplay)m_parentForm).displayMessageBox(MessageBoxCustom.PopUpMsgType.INFO, null, "The application has made changes/additions to the System Environment Variables.\n\n The application must be restarted for the System Environment Variables to take effect.\n\n Click OK to close the application and run the application again.", "Info", -1);

               // raise the event to restart the application
               RestartApplication();
            }

            if (setupSuccessful && !((frmSetupStatusDisplay)m_parentForm).applicationNeedsToBeRestarted())
            {
               // set font to default text and color
               textProp.textFont = defaultTextProp.textFont;
               textProp.textColor = defaultTextProp.textColor;
               textProp.wordIndex = 0;
               textProp.wordCount = 100;
               // set font style
               textProp.textFont = new Font(textProp.textFont, FontStyle.Regular);
               textPropList.Add(textProp);

               indentation = Common.getIndentation(1);
               msg = "\n" + indentation + "Executing script to create required files to GUTS project folder";

               UpdateStatusDisplayAndLogEvent(msg, msg, textPropList);

               msg = ((frmSetupStatusDisplay)m_parentForm).displayMessageBox(MessageBoxCustom.PopUpMsgType.YESNO, null, "Would you like to run the perl script to copy necessary files to GUTS folder?", "Info", -1);

               if (String.Equals(msg, "yes", StringComparison.OrdinalIgnoreCase))
               {
                  try
                  {
                     var process = Process.Start(perlAppPath, dashboardScriptPath);
                     process.WaitForExit();

                     verifyAdditionalFilesAlreadyExistInGutsProject(gutsRootPath, pathAndNameOfAddtionalFilesList.iniValue, ref additionalFileMissing, ref errMsg);

                     if (errMsg.Length == 0 && additionalFileMissing.Length == 0)
                     {
                        // set font to default text and color
                        textProp.textFont = defaultTextProp.textFont;
                        textProp.textColor = Common.getSetupStatusColor(Common.EXECUTION_STATUS.SUCCESS);
                        textProp.wordIndex = 0;
                        textProp.wordCount = 2;
                        // set font style
                        textProp.textFont = new Font(textProp.textFont, FontStyle.Bold);
                        textPropList.Add(textProp);

                        msg = " - SUCCESS";

                     }
                     else
                     {
                        // set font to default text and color
                        textProp.textFont = defaultTextProp.textFont;
                        textProp.textColor = Common.getSetupStatusColor(Common.EXECUTION_STATUS.FAILURE);
                        textProp.wordIndex = 0;
                        textProp.wordCount = 2;
                        // set font style
                        textProp.textFont = new Font(textProp.textFont, FontStyle.Bold);
                        textPropList.Add(textProp);

                        if (errMsg.Length > 0)
                           msg = " - FAILED. " + errMsg;
                        else
                           msg = " - FAILED. " + additionalFileMissing + " not found.";

                        setupSuccessful = false;
                     }
                  }
                  catch (Exception e)
                  {
                     // set font to default text and color
                     textProp.textFont = defaultTextProp.textFont;
                     textProp.textColor = Common.getSetupStatusColor(Common.EXECUTION_STATUS.FAILURE);
                     textProp.wordIndex = 0;
                     textProp.wordCount = 2;
                     // set font style
                     textProp.textFont = new Font(textProp.textFont, FontStyle.Bold);
                     textPropList.Add(textProp);

                     msg = " - FAILED. Error executing file " + dashboardScriptPath + ". " + e.Message;

                     setupSuccessful = false;
                  }
               }
               else
               {
                  // set font to default text and color
                  textProp.textFont = defaultTextProp.textFont;
                  textProp.textColor = Common.getSetupStatusColor(Common.EXECUTION_STATUS.FAILURE);
                  textProp.wordIndex = 0;
                  textProp.wordCount = 2;
                  // set font style
                  textProp.textFont = new Font(textProp.textFont, FontStyle.Bold);
                  textPropList.Add(textProp);

                  msg = " - FAILED. User chooses to not run script.";

                  setupSuccessful = false;
               }

               UpdateStatusDisplayAndLogEvent(msg, msg, textPropList);
            }
         }

         return setupSuccessful;
      }

      void verifyAdditionalFilesAlreadyExistInGutsProject(string gutsRootPath, string pathAndNameToListOfFiles, ref string fileMissing, ref string errMsg)
      {
         string line, filePart2, filePath;
         Match regExMatch;
         errMsg = "";
         fileMissing = "";
         FileStream fs = null;
         StreamReader reader = null;

         try
         {
            fs = new FileStream(pathAndNameToListOfFiles, FileMode.Open, FileAccess.Read);
            reader = new StreamReader(fs, Encoding.Default);

            while ((line = reader.ReadLine()) != null)
            {
               line = line.Trim();

               if (line.Length > 0)
               {
                  regExMatch = Regex.Match(line, @"(.*\\)?(SM3\\.+)", RegexOptions.IgnoreCase);

                  if (regExMatch.Success)
                  {
                     filePart2 = regExMatch.Groups[2].Value;

                     filePath = gutsRootPath + "\\" + filePart2;

                     if (!File.Exists(filePath))
                     {
                        fileMissing = filePath;
                        break;
                     }
                  }
               }
            }
         }
         catch (Exception ex)
         {
            errMsg = ex.Message;
         }
         finally
         {
            if (reader != null)
            {
               reader.Close();
               fs.Close();
            }
         }

      }

      ConfigFileManager.Ini_KeyValue<string> getPerlBinaryPathIniInfo()
      {
         ConfigFileManager.Ini_KeyValue<string> iniInfo = new ConfigFileManager.Ini_KeyValue<string>("");
         iniInfo.iniKeyName = "perl_binary_path";
         iniInfo.iniSectionName = ConfigFileManager.enum_INI_SECTION_NAMES.GUTS_Pre_Build_Assistance.ToString();

         foreach (ConfigFileManager.Ini_KeyValue<string> iniParam in gutsPrebuildParams)
         {
            if (String.Equals(iniParam.iniKeyName, iniInfo.iniKeyName, StringComparison.OrdinalIgnoreCase))
            {
               iniInfo = iniParam;
               if (iniInfo.iniValue.Length > 0)
                  iniInfo.iniValue = Path.GetFullPath(iniInfo.iniValue);
               break;
            }
         }

         return iniInfo;
      }

      ConfigFileManager.Ini_KeyValue<string> getGutsDevelopmentFolderIniInfo()
      {
         ConfigFileManager.Ini_KeyValue<string> iniInfo = new ConfigFileManager.Ini_KeyValue<string>("");
         iniInfo.iniKeyName = "GUTS_Development_Folder";
         iniInfo.iniSectionName = ConfigFileManager.enum_INI_SECTION_NAMES.GUTS_Pre_Build_Assistance.ToString();

         foreach (ConfigFileManager.Ini_KeyValue<string> iniParam in gutsPrebuildParams)
         {
            if (String.Equals(iniParam.iniKeyName, iniInfo.iniKeyName, StringComparison.OrdinalIgnoreCase))
            {
               iniInfo = iniParam;
               if (iniInfo.iniValue.Length > 0)
                  iniInfo.iniValue = Path.GetFullPath(iniInfo.iniValue);
               break;
            }
         }

         return iniInfo;
      }

      ConfigFileManager.Ini_KeyValue<string> getPathAndNameOfAddtionalFileList()
      {
         ConfigFileManager.Ini_KeyValue<string> iniInfo = new ConfigFileManager.Ini_KeyValue<string>("");
         iniInfo.iniKeyName = "list_of_additional_files";
         iniInfo.iniSectionName = ConfigFileManager.enum_INI_SECTION_NAMES.GUTS_Pre_Build_Assistance.ToString();

         foreach (ConfigFileManager.Ini_KeyValue<string> iniParam in gutsPrebuildParams)
         {
            if (String.Equals(iniParam.iniKeyName, iniInfo.iniKeyName, StringComparison.OrdinalIgnoreCase))
            {
               iniInfo = iniParam;
               if (iniInfo.iniValue.Length > 0)
                  iniInfo.iniValue = Path.GetFullPath(iniInfo.iniValue);
               break;
            }
         }

         return iniInfo;
      }
   }
}
