private void RunPostInitializeStepsIfNeeded()
        {
            string startupPostStepPackageFile = Path.Combine(_packageSource, InstallPackage.STARTUP_POST_STEP_PACKAGE_FILENAME);

            //remove post step flag file if it exists
            if (File.Exists(startupPostStepPackageFile))
            {
                try
                {
                    using (new SecurityDisabler())
                    {
                        //Load the post step details
                        XmlSerializer serializer = new XmlSerializer(typeof(PostStepDetails));
                        using (TextReader writer = new StreamReader(startupPostStepPackageFile))
                        {
                            PostStepDetails details = serializer.Deserialize(writer) as PostStepDetails;

                            if (details != null)
                            {
                                InstallLogger installLogger = new InstallLogger(new RootLogger(Level.ALL));

                                try
                                {
                                    InstallPackage.ExecutePostSteps(installLogger, details, true);

                                    InstallPackage.NotifiyPackageComplete(InstallPackage.SUCCESS, details);
                                }
                                catch (Exception ex)
                                {
                                    Log.Fatal("An error occured when running post steps", ex, this);

                                    InstallPackage.NotifiyPackageComplete(InstallPackage.FAIL, details);
                                }
                                finally
                                {
                                    installLogger.WriteMessages(Path.Combine(details.HistoryPath, "Install.log"));
                                }
                            }
                        }
                    }
                }
                finally
                {
                    //cleanup the post step
                    File.Delete(startupPostStepPackageFile);
                }
            }
        }
        /// <summary>
        /// Installs the packages found in the package source folder.
        /// </summary>
        private void InstallPackages()
        {
            //Check to see if there is a post-step pending, and skip package install if there is
            if (File.Exists(Path.Combine(_packageSource, STARTUP_POST_STEP_PACKAGE_FILENAME)))
            {
                Log.Info("Install packages skipped because there is a post step pending", this);

                return;
            }

            InstallLogger installLogger = new InstallLogger(new RootLogger(Level.ALL));

            //Return if another installation is happening
            if (GetInstallerState() != InstallerState.Ready)
            {
                Log.Info(string.Format("Install packages skipped because install state is {0}. ", GetInstallerState()), this);

                return;
            }

            //Prevent shutdown
            using (new ShutdownGuard())
            {
                //If Sitecore is shutting down, don't start the installer
                if (ShutdownDetected)
                {
                    Log.Info("Skipping Install because shutdown is pending", this);

                    return;
                }

                //Block further package installs
                SetInstallerState(InstallerState.InstallingPackage);

                using (new SecurityDisabler())
                {
                    //Find pending packages. This loop may not complete if there were binary/config changes
                    foreach (string updatePackageFilename in Directory.GetFiles(_packageSource, "*.update", SearchOption.TopDirectoryOnly).OrderBy(f => f))
                    {
                        string updatePackageFilenameStripped = updatePackageFilename.Split('\\').Last();
                        if (ShutdownDetected)
                        {
                            Log.Info("Install packages aborting due to shutdown", this);

                            if (GetInstallerState() != InstallerState.WaitingForPostSteps)
                            {
                                SetInstallerState(InstallerState.Ready);
                            }

                            break;
                        }

                        Log.Info(String.Format("Begin Installation: {0}", updatePackageFilenameStripped), this);

                        string installationHistoryRoot      = null;
                        List <ContingencyEntry> logMessages = new List <ContingencyEntry>();

                        PostStepDetails postStepDetails = new PostStepDetails
                        {
                            PostStepPackageFilename = updatePackageFilename,
                            ResultFileName          = Path.Combine(Path.GetDirectoryName(updatePackageFilename), Path.GetFileNameWithoutExtension(updatePackageFilename) + ".json")
                        };

                        string installStatus = null;

                        try
                        {
                            //Run the installer
                            if (sitecoreUpdateVersionInfo.ProductMajorPart == 1)
                            {
                                logMessages = UpdateHelper.Install(BuildPackageInfo(updatePackageFilename), installLogger, out installationHistoryRoot);
                            }
                            else
                            {
                                object[] installationParamaters = new object[] { BuildReflectedPackageInfo(updatePackageFilename), installLogger, null };
                                logMessages = (List <ContingencyEntry>)updateHelperType.InvokeMember("Install",
                                                                                                     BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
                                                                                                     null, null, installationParamaters, null);
                                installationHistoryRoot = installationParamaters[2].ToString();
                            }

                            postStepDetails.HistoryPath = installationHistoryRoot;

                            if (_updateConfigurationFiles)
                            {
                                FindAndUpdateChangedConfigs(Path.GetFileNameWithoutExtension(updatePackageFilename));
                            }

                            //Sleep for 4 seconds to see if there was a file change that could cause a problem
                            Thread.Sleep(4000);

                            //Abort if Sitecore is shutting down. The install post steps will have to be completed later
                            if (ShutdownDetected)
                            {
                                SetInstallerState(InstallerState.WaitingForPostSteps);

                                RunPostStepsAtStartup(updatePackageFilename, installationHistoryRoot, postStepDetails);

                                RestartSitecoreServer();

                                break;
                            }
                            else
                            {
                                ExecutePostSteps(installLogger, postStepDetails, false);
                                installStatus = SUCCESS;

                                Log.Info(String.Format("Installation Complete: {0}", updatePackageFilenameStripped), this);
                                SetInstallerState(InstallerState.InstallingPackage);
                            }
                        }
                        catch (PostStepInstallerException ex)
                        {
                            installStatus = FAIL;

                            logMessages             = ex.Entries;
                            installationHistoryRoot = ex.HistoryPath;
                            installLogger.Fatal("Package install failed", ex);

                            throw ex;
                        }
                        catch (Exception ex)
                        {
                            installStatus = FAIL;
                            Log.Error(String.Format("Installation Failed: {0}", updatePackageFilenameStripped), ex, this);
                            installLogger.Fatal("Package install failed", ex);

                            ThreadPool.QueueUserWorkItem(new WaitCallback((ctx) =>
                            {
                                try
                                {
                                    //The update package may be locked because the file object hasn't been disposed. Wait for it.
                                    Thread.Sleep(100);

                                    //I really hate this, but I couldn't find another reliable way to ensure the locked file is closed before I move it.
                                    GC.Collect(2);
                                    GC.WaitForPendingFinalizers();

                                    File.Move(updatePackageFilename, updatePackageFilename + ".error_" + DateTime.Now.ToString("yyyyMMdd.hhmmss"));
                                }
                                catch (Exception ex1)
                                {
                                    Log.Error("Error moving broken package", ex1, this);
                                }
                            }));

                            break;
                        }
                        finally
                        {
                            if (installationHistoryRoot != null)
                            {
                                //Write logs
                                installLogger.WriteMessages(Path.Combine(installationHistoryRoot, "Install.log"));

                                SaveInstallationMessages(installationHistoryRoot, logMessages);
                            }

                            //Send the status if there is one
                            if (installStatus != null)
                            {
                                NotifiyPackageComplete(installStatus, postStepDetails);
                            }
                        }
                    }

                    if (!ShutdownDetected)
                    {
                        //Allow additional installs
                        SetInstallerState(InstallerState.Ready);
                    }
                }
            }
        }