//=====================================================================

        /// <summary>
        /// This is the main program entry point
        /// </summary>
        /// <param name="args">The command line arguments</param>
        /// <returns>An exit code that indicates the success or failure of the process</returns>
        public static int Main(string[] args)
        {
            List<CommandLineArgument> allArgs = new List<CommandLineArgument>();
            List<string> execArgs = new List<string>();
            string product = null, version = null, locale = null, catalogName = null, commandLine;
            int result = HelpLibraryManagerException.Success;
            bool isInstall = false, isSilent = false, showHelp = false;
            Version viewerVersion = new Version(1, 0);

            Assembly asm = Assembly.GetExecutingAssembly();

            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
            Console.WriteLine("{0}, version {1}\r\n{2}\r\nE-Mail: [email protected]\r\n",
                fvi.ProductName, fvi.ProductVersion, fvi.LegalCopyright);

            try
            {
                // Parse the command line arguments
                foreach(string arg in args)
                    allArgs.Add(new CommandLineArgument(arg));

                for(int idx = 0; idx < allArgs.Count; idx++)
                {
                    if(allArgs[idx].IsSwitch)
                    {
                        // This is used internally and isn't passed on
                        if(allArgs[idx].MatchesSwitch("viewerVersion") && idx < allArgs.Count - 1)
                        {
                            idx++;
                            viewerVersion = new Version(allArgs[idx].Value);
                            continue;
                        }

                        if(allArgs[idx].MatchesSwitch("catalogName") && idx < allArgs.Count - 1)
                            catalogName = allArgs[idx + 1].Value;

                        if(allArgs[idx].MatchesSwitch("product") && idx < allArgs.Count - 1)
                            product = allArgs[idx + 1].Value;

                        if(allArgs[idx].MatchesSwitch("version") && idx < allArgs.Count - 1)
                            version = allArgs[idx + 1].Value;

                        if(allArgs[idx].MatchesSwitch("locale") && idx < allArgs.Count - 1)
                            locale = allArgs[idx + 1].Value;

                        if(allArgs[idx].MatchesSwitch("install") || allArgs[idx].MatchesSwitch("sourceMedia") ||
                          allArgs[idx].MatchesSwitch("sourceUri"))
                            isInstall = true;

                        if(allArgs[idx].MatchesSwitch("silent"))
                            isSilent = true;

                        if(allArgs[idx].MatchesSwitch("help") || allArgs[idx].Value[1] == '?')
                            showHelp = true;
                    }

                    execArgs.Add(allArgs[idx].ToCommandLineOption());
                }

                if(allArgs.Count == 0 || showHelp)
                {
                    ShowHelp();
                    return HelpLibraryManagerException.MissingCommandLineArgument;
                }

                if(viewerVersion.Major == 1)
                {
                    // These two are required for Help Viewer 1.0
                    if(String.IsNullOrEmpty(product))
                        throw new HelpLibraryManagerException(viewerVersion,
                            HelpLibraryManagerException.MissingCommandLineArgument, "/product");

                    if(String.IsNullOrEmpty(version))
                        throw new HelpLibraryManagerException(viewerVersion,
                            HelpLibraryManagerException.MissingCommandLineArgument, "/version");

                    // This is only used by Help Viewer 2.0
                    if(!String.IsNullOrEmpty(catalogName))
                        throw new HelpLibraryManagerException(viewerVersion,
                            HelpLibraryManagerException.InvalidCmdArgs,
                            "/catalogName is only valid for Help Viewer 2.0");
                }
                else
                {
                    if(!String.IsNullOrEmpty(product))
                        throw new HelpLibraryManagerException(viewerVersion,
                            HelpLibraryManagerException.InvalidCmdArgs,
                            "/product is only valid for Help Viewer 1.0");

                    if(!String.IsNullOrEmpty(version))
                        throw new HelpLibraryManagerException(viewerVersion,
                            HelpLibraryManagerException.InvalidCmdArgs,
                            "/version is only valid for Help Viewer 1.0");

                    // If not specified, default the catalog name based on the viewer version
                    if(String.IsNullOrEmpty(catalogName))
                    {
                        catalogName = HelpLibraryManager.DefaultCatalogName(viewerVersion);

                        if(catalogName == null)
                            throw new HelpLibraryManagerException(viewerVersion,
                                HelpLibraryManagerException.MissingCommandLineArgument, "/catalogName");

                        Console.WriteLine("Catalog name not specified, the default catalog name '{0}' will " +
                            "be used.", catalogName);

                        execArgs.Add("/catalogName");
                        execArgs.Add(catalogName);
                    }
                }

                HelpLibraryManager hlm = new HelpLibraryManager(viewerVersion);

                // Can't do anything if the Help Library Manager is not installed
                if(hlm.HelpLibraryManagerPath == null)
                    throw new HelpLibraryManagerException(viewerVersion,
                        HelpLibraryManagerException.HelpLibraryManagerNotFound);

                // Can't do anything if the Help Library Manager is already running
                if(Process.GetProcessesByName(Path.GetFileNameWithoutExtension(hlm.HelpLibraryManagerPath)).Length > 0)
                    throw new HelpLibraryManagerException(viewerVersion,
                        HelpLibraryManagerException.HelpLibraryManagerAlreadyRunning);

                // Can't do anything if the local store is not initialized
                if(!hlm.LocalStoreInitialized)
                    throw new HelpLibraryManagerException(viewerVersion,
                        HelpLibraryManagerException.LocalStoreNotInitialized);

                // If not specified, try to figure out the default locale
                if(String.IsNullOrEmpty(locale))
                {
                    if(viewerVersion.Major == 1)
                    {
                        locale = hlm.FindLocaleFor(product, version);

                        if(locale == null)
                            throw new HelpLibraryManagerException(viewerVersion,
                                HelpLibraryManagerException.CatalogNotInstalled, String.Format(
                                CultureInfo.InvariantCulture, "Product: {0}  Version: {1}", product, version));
                    }
                    else
                    {
                        locale = hlm.FindLocaleFor(catalogName);

                        if(locale == null)
                            throw new HelpLibraryManagerException(viewerVersion,
                                HelpLibraryManagerException.CatalogNotInstalled, String.Format(
                                CultureInfo.InvariantCulture, "Catalog Name: {0}", catalogName));
                    }

                    Console.WriteLine("No locale specified, the default locale '{0}' will be used", locale);

                    execArgs.Add("/locale");
                    execArgs.Add(locale);
                }

                // Execute the request
                Console.WriteLine("Running Help Library Manager to perform the requested action.  Please wait...");

                commandLine = String.Join(" ", execArgs.ToArray());

                try
                {
                    // If installing, we must always run as administrator.  Everything else can run as a normal
                    // user.
                    if(isInstall)
                    {
                        if(isSilent)
                            result = hlm.RunAsAdministrator(commandLine, ProcessWindowStyle.Minimized);
                        else
                            result = hlm.RunAsAdministrator(commandLine, ProcessWindowStyle.Normal);
                    }
                    else
                    {
                        result = hlm.RunAsNormalUser(commandLine, ProcessWindowStyle.Minimized);

                        // For content manager for Help Viewer 2.0 returns almost immediately.  If there is not
                        // content to uninstall, a subsequent install attempt can fail because the other instance
                        // hasn't quite shutdown yet.  This works around the issue by pausing for a couple of
                        // seconds.
                        if(viewerVersion.Major == 2)
                            System.Threading.Thread.Sleep(2000);
                    }
                }
                catch(Exception ex)
                {
                    throw new HelpLibraryManagerException(viewerVersion, HelpLibraryManagerException.UnknownError,
                        String.Format(CultureInfo.InvariantCulture, "Failed to execute \"{0}\" {1}.\r\nError: {2}",
                        hlm.HelpLibraryManagerPath, commandLine, ex.Message));
                }

                if(result != HelpLibraryManagerException.Success)
                    throw new HelpLibraryManagerException(viewerVersion, result);

                Console.WriteLine("The operation completed successfully");
            }
            catch(HelpLibraryManagerException hlmEx)
            {
                result = hlmEx.ErrorCode;
                Console.WriteLine("\r\nERROR: The requested operation could not be performed.\r\nDetails: {0}",
                    hlmEx.Message);
            }
            catch(Exception ex)
            {
                result = HelpLibraryManagerException.UnknownError;
                Console.WriteLine("\r\nERROR: The requested operation could not be performed.\r\nDetails: {0}",
                    ex.ToString());
            }
#if DEBUG
            if(System.Diagnostics.Debugger.IsAttached)
            {
                Console.WriteLine("Hit ENTER to exit...");
                Console.ReadLine();
            }
#endif
            return result;
        }
        //=====================================================================

        /// <summary>
        /// This performs the requested task
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void actionThread_DoWork(object sender, DoWorkEventArgs e)
        {
            ThreadAction action = (ThreadAction)e.Argument;
            string arguments, contentSetupFile;
            int errorCode;

            runningThread = Thread.CurrentThread;

            try
            {
                HelpLibraryManager hlm = new HelpLibraryManager(viewerVersion);

                // Remove old content.  We'll remove it if installing to be sure that the latest copy is
                // installed.
                if(action == ThreadAction.Install || action == ThreadAction.Remove)
                {
                    if(action == ThreadAction.Install)
                        actionThread.ReportProgress(0, (int)ThreadState.RemovingOldContent);
                    else
                        actionThread.ReportProgress(0, (int)ThreadState.RemovingContent);

                    if(viewerVersion.Major == 1)
                        arguments = String.Format(CultureInfo.InvariantCulture,
                            "/product \"{0}\" /version \"{1}\" /locale {2} /uninstall /silent /vendor " +
                            "\"{3}\" /productName \"{4}\" /mediaBookList \"{5}\"",
                            project.CatalogProductId, project.CatalogVersion, project.Language.Name,
                            !String.IsNullOrEmpty(project.VendorName) ? project.VendorName : "Vendor Name",
                            !String.IsNullOrEmpty(project.ProductTitle) ? project.ProductTitle : project.HelpTitle,
                            project.HelpTitle);
                    else
                        arguments = String.Format(CultureInfo.InvariantCulture,
                            "/catalogName \"{0}\" /locale {1} /wait 0 /operation uninstall /vendor \"{2}\" " +
                            "/productName \"{3}\" /bookList \"{4}\" ", catalogName, project.Language.Name,
                            !String.IsNullOrEmpty(project.VendorName) ? project.VendorName : "Vendor Name",
                            !String.IsNullOrEmpty(project.ProductTitle) ? project.ProductTitle : project.HelpTitle,
                            project.HelpTitle);

                    // If there are substitution tags present, have a go at resolving them
                    if(arguments.IndexOf("{@", StringComparison.Ordinal) != -1)
                    {
                        try
                        {
                            var bp = new BuildProcess(project);
                            arguments = bp.SubstitutionTags.TransformText(arguments);
                        }
                        catch(Exception ex)
                        {
                            throw new InvalidOperationException("Unable to transform substitution tags: " +
                                ex.Message, ex);
                        }
                    }

                    // This doesn't have to run as an administrator
                    errorCode = hlm.RunAsNormalUser(arguments, ProcessWindowStyle.Minimized);

                    // Ignore it if not found and we are installing
                    if(errorCode != HelpLibraryManagerException.Success &&
                      (errorCode != HelpLibraryManagerException.NoBooksToInstall || action == ThreadAction.Remove))
                        throw new HelpLibraryManagerException(viewerVersion, errorCode);
                }

                if(action == ThreadAction.Install)
                {
                    // Install the new content
                    actionThread.ReportProgress(0, (int)ThreadState.InstallingContent);

                    // Copy the MSHA file to the required name
                    contentSetupFile = Path.Combine(Path.GetDirectoryName(setupFile), "HelpContentSetup.msha");
                    File.Copy(setupFile, contentSetupFile, true);

                    if(viewerVersion.Major == 1)
                        arguments = String.Format(CultureInfo.InvariantCulture, "/product \"{0}\" " +
                            "/version \"{1}\" /locale {2} /sourceMedia \"{3}\"", project.CatalogProductId,
                            project.CatalogVersion, project.Language.Name, contentSetupFile);
                    else
                        arguments = String.Format(CultureInfo.InvariantCulture, "/catalogName \"{0}\" " +
                            "/locale {1} /wait 0 /operation install /sourceUri \"{2}\"", catalogName,
                            project.Language.Name, contentSetupFile);

                    // Always interactive and must run as administrator.  We can't run silently as we don't have
                    // a signed cabinet file.
                    errorCode = hlm.RunAsAdministrator(arguments, ProcessWindowStyle.Normal);

                    if(errorCode != HelpLibraryManagerException.Success)
                        throw new HelpLibraryManagerException(viewerVersion, errorCode);

                    // Open it if installed successfully
                    action = ThreadAction.OpenCurrent;
                }

                if(action == ThreadAction.OpenCurrent)
                {
                    arguments = null;

                    if(msHelpViewer == null)
                    {
                        msHelpViewer = hlm.HelpViewerPath;

                        if(msHelpViewer == null)
                            msHelpViewer = "ms-xhelp:///?method=page&id=-1";
                        else
                            if(viewerVersion.Major == 2)
                                arguments = "/catalogname \"" + catalogName + "\"";
                    }

                    actionThread.ReportProgress(0, (int)ThreadState.OpeningContent);
                    System.Diagnostics.Process.Start(msHelpViewer, arguments);
                }

                if(action == ThreadAction.OpenContentManager)
                {
                    actionThread.ReportProgress(0, (int)ThreadState.OpeningContentManager);

                    // Can't do anything if the Help Library Manager is not installed
                    if(hlm.HelpLibraryManagerPath == null)
                        throw new HelpLibraryManagerException(viewerVersion,
                            HelpLibraryManagerException.HelpLibraryManagerNotFound);

                    if(viewerVersion.Major == 1)
                        hlm.LaunchInteractive(String.Format(CultureInfo.InvariantCulture,
                            "/product \"{0}\" /version \"{1}\" /locale {2}", project.CatalogProductId,
                            project.CatalogVersion, project.Language.Name));
                    else
                        hlm.LaunchInteractive(String.Format(CultureInfo.InvariantCulture,
                            "/catalogName \"{0}\" /locale {1} /manage", catalogName, project.Language.Name));
                }
            }
            catch(ThreadAbortException)
            {
                // Ignore thread abort exceptions
            }
        }
        //=====================================================================

        /// <summary>
        /// This performs the requested task
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void actionThread_DoWork(object sender, DoWorkEventArgs e)
        {
            ThreadAction action = (ThreadAction)e.Argument;
            string       arguments, contentSetupFile;
            int          errorCode;

            runningThread = Thread.CurrentThread;

            try
            {
                HelpLibraryManager hlm = new HelpLibraryManager(viewerVersion);

                // Remove old content.  We'll remove it if installing to be sure that
                // the latest copy is installed.
                if (action == ThreadAction.Install || action == ThreadAction.Remove)
                {
                    if (action == ThreadAction.Install)
                    {
                        actionThread.ReportProgress(0, (int)ThreadState.RemovingOldContent);
                    }
                    else
                    {
                        actionThread.ReportProgress(0, (int)ThreadState.RemovingContent);
                    }

                    if (viewerVersion.Major == 1)
                    {
                        arguments = String.Format(CultureInfo.InvariantCulture,
                                                  "/product \"{0}\" /version \"{1}\" /locale {2} /uninstall /silent /vendor " +
                                                  "\"{3}\" /productName \"{4}\" /mediaBookList \"{5}\"",
                                                  project.CatalogProductId, project.CatalogVersion, project.Language.Name,
                                                  !String.IsNullOrEmpty(project.VendorName) ? project.VendorName : "Vendor Name",
                                                  !String.IsNullOrEmpty(project.ProductTitle) ? project.ProductTitle : project.HelpTitle,
                                                  project.HelpTitle);
                    }
                    else
                    {
                        arguments = String.Format(CultureInfo.InvariantCulture,
                                                  "/catalogName \"{0}\" /locale {1} /wait 0 /operation uninstall /vendor \"{2}\" " +
                                                  "/productName \"{3}\" /bookList \"{4}\" ",
                                                  project.CatalogName, project.Language.Name,
                                                  !String.IsNullOrEmpty(project.VendorName) ? project.VendorName : "Vendor Name",
                                                  !String.IsNullOrEmpty(project.ProductTitle) ? project.ProductTitle : project.HelpTitle,
                                                  project.HelpTitle);
                    }

                    // This doesn't have to run as an administrator
                    errorCode = hlm.RunAsNormalUser(arguments, ProcessWindowStyle.Minimized);

                    // Ignore it if not found and we are installing
                    if (errorCode != HelpLibraryManagerException.Success &&
                        (errorCode != HelpLibraryManagerException.NoBooksToInstall || action == ThreadAction.Remove))
                    {
                        throw new HelpLibraryManagerException(viewerVersion, errorCode);
                    }
                }

                if (action == ThreadAction.Install)
                {
                    // Install the new content
                    actionThread.ReportProgress(0, (int)ThreadState.InstallingContent);

                    // Copy the MSHA file to the required name
                    contentSetupFile = Path.Combine(Path.GetDirectoryName(setupFile), "HelpContentSetup.msha");
                    File.Copy(setupFile, contentSetupFile, true);

                    if (viewerVersion.Major == 1)
                    {
                        arguments = String.Format(CultureInfo.InvariantCulture, "/product \"{0}\" " +
                                                  "/version \"{1}\" /locale {2} /sourceMedia \"{3}\"", project.CatalogProductId,
                                                  project.CatalogVersion, project.Language.Name, contentSetupFile);
                    }
                    else
                    {
                        arguments = String.Format(CultureInfo.InvariantCulture, "/catalogName \"{0}\" " +
                                                  "/locale {1} /wait 0 /operation install /sourceUri \"{2}\"", project.CatalogName,
                                                  project.Language.Name, contentSetupFile);
                    }

                    // Always interactive and must run as administrator.  We can't run silently as we don't have
                    // a signed cabinet file.
                    errorCode = hlm.RunAsAdministrator(arguments, ProcessWindowStyle.Normal);

                    if (errorCode != HelpLibraryManagerException.Success)
                    {
                        throw new HelpLibraryManagerException(viewerVersion, errorCode);
                    }

                    // Open it if installed successfully
                    action = ThreadAction.OpenCurrent;
                }

                if (action == ThreadAction.OpenCurrent)
                {
                    arguments = null;

                    if (msHelpViewer == null)
                    {
                        msHelpViewer = hlm.HelpViewerPath;

                        if (msHelpViewer == null)
                        {
                            msHelpViewer = "ms-xhelp:///?method=page&id=-1";
                        }
                        else
                        if (viewerVersion.Major == 2)
                        {
                            arguments = "/catalogname \"" + project.CatalogName + "\"";
                        }
                    }

                    actionThread.ReportProgress(0, (int)ThreadState.OpeningContent);
                    System.Diagnostics.Process.Start(msHelpViewer, arguments);
                }
            }
            catch (ThreadAbortException)
            {
                // Ignore thread abort exceptions
            }
        }
        //=====================================================================

        /// <summary>
        /// This is the main program entry point
        /// </summary>
        /// <param name="args">The command line arguments</param>
        /// <returns>An exit code that indicates the success or failure of the process</returns>
        public static int Main(string[] args)
        {
            List <CommandLineArgument> allArgs = new List <CommandLineArgument>();
            List <string> execArgs = new List <string>();
            string        product = null, version = null, locale = null, catalogName = null, commandLine;
            int           result = HelpLibraryManagerException.Success;
            bool          isInstall = false, isSilent = false, showHelp = false;
            Version       viewerVersion = new Version(1, 0);

            Assembly asm = Assembly.GetExecutingAssembly();

            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);

            Console.WriteLine("{0}, version {1}\r\n{2}\r\nE-Mail: [email protected]\r\n",
                              fvi.ProductName, fvi.ProductVersion, fvi.LegalCopyright);

            try
            {
                // Parse the command line arguments
                foreach (string arg in args)
                {
                    allArgs.Add(new CommandLineArgument(arg));
                }

                for (int idx = 0; idx < allArgs.Count; idx++)
                {
                    if (allArgs[idx].IsSwitch)
                    {
                        // This is used internally and isn't passed on
                        if (allArgs[idx].MatchesSwitch("viewerVersion") && idx < allArgs.Count - 1)
                        {
                            idx++;
                            viewerVersion = new Version(allArgs[idx].Value);
                            continue;
                        }

                        if (allArgs[idx].MatchesSwitch("catalogName") && idx < allArgs.Count - 1)
                        {
                            catalogName = allArgs[idx + 1].Value;
                        }

                        if (allArgs[idx].MatchesSwitch("product") && idx < allArgs.Count - 1)
                        {
                            product = allArgs[idx + 1].Value;
                        }

                        if (allArgs[idx].MatchesSwitch("version") && idx < allArgs.Count - 1)
                        {
                            version = allArgs[idx + 1].Value;
                        }

                        if (allArgs[idx].MatchesSwitch("locale") && idx < allArgs.Count - 1)
                        {
                            locale = allArgs[idx + 1].Value;
                        }

                        if (allArgs[idx].MatchesSwitch("install") || allArgs[idx].MatchesSwitch("sourceMedia") ||
                            allArgs[idx].MatchesSwitch("sourceUri"))
                        {
                            isInstall = true;
                        }

                        if (allArgs[idx].MatchesSwitch("silent"))
                        {
                            isSilent = true;
                        }

                        if (allArgs[idx].MatchesSwitch("help") || allArgs[idx].Value[1] == '?')
                        {
                            showHelp = true;
                        }
                    }

                    execArgs.Add(allArgs[idx].ToCommandLineOption());
                }

                if (allArgs.Count == 0 || showHelp)
                {
                    ShowHelp();
                    return(HelpLibraryManagerException.MissingCommandLineArgument);
                }

                if (viewerVersion.Major == 1)
                {
                    // These two are required for Help Viewer 1.0
                    if (String.IsNullOrEmpty(product))
                    {
                        throw new HelpLibraryManagerException(viewerVersion,
                                                              HelpLibraryManagerException.MissingCommandLineArgument, "/product");
                    }

                    if (String.IsNullOrEmpty(version))
                    {
                        throw new HelpLibraryManagerException(viewerVersion,
                                                              HelpLibraryManagerException.MissingCommandLineArgument, "/version");
                    }

                    // This is only used by Help Viewer 2.0
                    if (!String.IsNullOrEmpty(catalogName))
                    {
                        throw new HelpLibraryManagerException(viewerVersion,
                                                              HelpLibraryManagerException.InvalidCmdArgs,
                                                              "/catalogName is only valid for Help Viewer 2.0");
                    }
                }
                else
                {
                    if (!String.IsNullOrEmpty(product))
                    {
                        throw new HelpLibraryManagerException(viewerVersion,
                                                              HelpLibraryManagerException.InvalidCmdArgs,
                                                              "/product is only valid for Help Viewer 1.0");
                    }

                    if (!String.IsNullOrEmpty(version))
                    {
                        throw new HelpLibraryManagerException(viewerVersion,
                                                              HelpLibraryManagerException.InvalidCmdArgs,
                                                              "/version is only valid for Help Viewer 1.0");
                    }

                    // If not specified, default the catalog name based on the viewer version
                    if (String.IsNullOrEmpty(catalogName))
                    {
                        catalogName = HelpLibraryManager.DefaultCatalogName(viewerVersion);

                        if (catalogName == null)
                        {
                            throw new HelpLibraryManagerException(viewerVersion,
                                                                  HelpLibraryManagerException.MissingCommandLineArgument, "/catalogName");
                        }

                        Console.WriteLine("Catalog name not specified, the default catalog name '{0}' will " +
                                          "be used.", catalogName);
                    }
                }

                HelpLibraryManager hlm = new HelpLibraryManager(viewerVersion);

                // Can't do anything if the Help Library Manager is not installed
                if (hlm.HelpLibraryManagerPath == null)
                {
                    throw new HelpLibraryManagerException(viewerVersion,
                                                          HelpLibraryManagerException.HelpLibraryManagerNotFound);
                }

                // Can't do anything if the Help Library Manager is already running
                if (Process.GetProcessesByName(Path.GetFileNameWithoutExtension(hlm.HelpLibraryManagerPath)).Length > 0)
                {
                    throw new HelpLibraryManagerException(viewerVersion,
                                                          HelpLibraryManagerException.HelpLibraryManagerAlreadyRunning);
                }

                // Can't do anything if the local store is not initialized
                if (!hlm.LocalStoreInitialized)
                {
                    throw new HelpLibraryManagerException(viewerVersion,
                                                          HelpLibraryManagerException.LocalStoreNotInitialized);
                }

                // If not specified, try to figure out the default locale
                if (String.IsNullOrEmpty(locale))
                {
                    if (viewerVersion.Major == 1)
                    {
                        locale = hlm.FindLocaleFor(product, version);

                        if (locale == null)
                        {
                            throw new HelpLibraryManagerException(viewerVersion,
                                                                  HelpLibraryManagerException.CatalogNotInstalled, String.Format(
                                                                      CultureInfo.InvariantCulture, "Product: {0}  Version: {1}", product, version));
                        }
                    }
                    else
                    {
                        locale = hlm.FindLocaleFor(catalogName);

                        if (locale == null)
                        {
                            throw new HelpLibraryManagerException(viewerVersion,
                                                                  HelpLibraryManagerException.CatalogNotInstalled, String.Format(
                                                                      CultureInfo.InvariantCulture, "Catalog Name: {0}", catalogName));
                        }
                    }

                    Console.WriteLine("No locale specified, the default locale '{0}' will be used", locale);

                    execArgs.Add("/locale");
                    execArgs.Add(locale);
                }

                // Execute the request
                Console.WriteLine("Running Help Library Manager to perform the requested action.  Please wait...");

                commandLine = String.Join(" ", execArgs.ToArray());

                try
                {
                    // If installing, we must always run as administrator.  Everything else can run as a normal
                    // user.
                    if (isInstall)
                    {
                        if (isSilent)
                        {
                            result = hlm.RunAsAdministrator(commandLine, ProcessWindowStyle.Minimized);
                        }
                        else
                        {
                            result = hlm.RunAsAdministrator(commandLine, ProcessWindowStyle.Normal);
                        }
                    }
                    else
                    {
                        result = hlm.RunAsNormalUser(commandLine, ProcessWindowStyle.Minimized);

                        // For content manager for Help Viewer 2.0 returns almost immediately.  If there is not
                        // content to uninstall, a subsequent install attempt can fail because the other instance
                        // hasn't quite shutdown yet.  This works around the issue by pausing for a couple of
                        // seconds.
                        if (viewerVersion.Major == 2)
                        {
                            System.Threading.Thread.Sleep(2000);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new HelpLibraryManagerException(viewerVersion, HelpLibraryManagerException.UnknownError,
                                                          String.Format(CultureInfo.InvariantCulture, "Failed to execute \"{0}\" {1}.\r\nError: {2}",
                                                                        hlm.HelpLibraryManagerPath, commandLine, ex.Message));
                }

                if (result != HelpLibraryManagerException.Success)
                {
                    throw new HelpLibraryManagerException(viewerVersion, result);
                }

                Console.WriteLine("The operation completed successfully");
            }
            catch (HelpLibraryManagerException hlmEx)
            {
                result = hlmEx.ErrorCode;
                Console.WriteLine("\r\nERROR: The requested operation could not be performed.\r\nDetails: {0}",
                                  hlmEx.Message);
            }
            catch (Exception ex)
            {
                result = HelpLibraryManagerException.UnknownError;
                Console.WriteLine("\r\nERROR: The requested operation could not be performed.\r\nDetails: {0}",
                                  ex.ToString());
            }
#if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                Console.WriteLine("Hit ENTER to exit...");
                Console.ReadLine();
            }
#endif
            return(result);
        }
        //=====================================================================
        /// <summary>
        /// This is the main program entry point
        /// </summary>
        /// <param name="args">The command line arguments</param>
        public static int Main(string[] args)
        {
            List<CommandLineArgument> allArgs = new List<CommandLineArgument>();
            List<string> execArgs = new List<string>();
            string product = null, version = null, locale = null, commandLine;
            int result = HelpLibraryManagerException.Success;
            bool isInstall = false, isSilent = false, showHelp = false;

            Assembly asm = Assembly.GetExecutingAssembly();

            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
            Console.WriteLine("{0}, version {1}\r\n{2}\r\nE-Mail: [email protected]\r\n",
                fvi.ProductName, fvi.ProductVersion, fvi.LegalCopyright);

            try
            {
                HelpLibraryManager hlm = new HelpLibraryManager();

                // Can't do anything if the Help Library Manager is not installed
                if(hlm.HelpLibraryManagerPath == null)
                    throw new HelpLibraryManagerException(HelpLibraryManagerException.HelpLibraryManagerNotFound);

                // Can't do anything if the Help Library Manager is already running
                if(Process.GetProcessesByName(Path.GetFileNameWithoutExtension(hlm.HelpLibraryManagerPath)).Length > 0)
                    throw new HelpLibraryManagerException(HelpLibraryManagerException.HelpLibraryManagerAlreadyRunning);

                // Can't do anything if the local store is not initialized
                if(!hlm.LocalStoreInitialized)
                    throw new HelpLibraryManagerException(HelpLibraryManagerException.LocalStoreNotInitialized);

                // Parse the command line arguments
                foreach(string arg in args)
                    allArgs.Add(new CommandLineArgument(arg));

                for(int idx = 0; idx < allArgs.Count; idx++)
                {
                    if(allArgs[idx].IsSwitch)
                    {
                        if(allArgs[idx].MatchesSwitch("product") && idx < allArgs.Count - 1)
                            product = allArgs[idx + 1].Value;

                        if(allArgs[idx].MatchesSwitch("version") && idx < allArgs.Count - 1)
                            version = allArgs[idx + 1].Value;

                        if(allArgs[idx].MatchesSwitch("locale") && idx < allArgs.Count - 1)
                            locale = allArgs[idx + 1].Value;

                        if(allArgs[idx].MatchesSwitch("install") || allArgs[idx].MatchesSwitch("sourceMedia"))
                            isInstall = true;

                        if(allArgs[idx].MatchesSwitch("silent"))
                            isSilent = true;

                        if(allArgs[idx].MatchesSwitch("help") || allArgs[idx].Value[1] == '?')
                            showHelp = true;
                    }

                    execArgs.Add(allArgs[idx].ToCommandLineOption());
                }

                if(allArgs.Count == 0 || showHelp)
                {
                    ShowHelp();
                    return HelpLibraryManagerException.MissingCommandLineArgument;
                }

                // These two are required
                if(String.IsNullOrEmpty(product))
                    throw new HelpLibraryManagerException(HelpLibraryManagerException.MissingCommandLineArgument,
                        "/product");

                if(String.IsNullOrEmpty(version))
                    throw new HelpLibraryManagerException(HelpLibraryManagerException.MissingCommandLineArgument,
                        "/version");

                // If not specified, try to figure out the default locale
                if(String.IsNullOrEmpty(locale))
                {
                    locale = hlm.FindLocaleFor(product, version);

                    if(locale == null)
                        throw new HelpLibraryManagerException(HelpLibraryManagerException.CatalogNotInstalled,
                            String.Format(CultureInfo.InvariantCulture, "Product: {0}  Version: {1}", product, version));

                    Console.WriteLine("No locale specified, the default locale '{0}' will be used", locale);

                    execArgs.Add("/locale");
                    execArgs.Add(locale);
                }

                // Execute the request
                Console.WriteLine("Running Help Library Manager to perform the requested action.  Please wait...");

                commandLine = String.Join(" ", execArgs.ToArray());

                try
                {
                    // If installing, we must always run as administrator.  Everything else can
                    // run as a normal user.
                    if(isInstall)
                    {
                        if(isSilent)
                            result = hlm.RunAsAdministrator(commandLine, ProcessWindowStyle.Minimized);
                        else
                            result = hlm.RunAsAdministrator(commandLine, ProcessWindowStyle.Normal);
                    }
                    else
                        result = hlm.RunAsNormalUser(commandLine, ProcessWindowStyle.Minimized);
                }
                catch(Exception ex)
                {
                    throw new HelpLibraryManagerException(HelpLibraryManagerException.UnknownError, String.Format(
                        CultureInfo.InvariantCulture, "Failed to execute \"{0}\" {1}.\r\nError: {2}",
                        hlm.HelpLibraryManagerPath, commandLine, ex.Message));
                }

                if(result != HelpLibraryManagerException.Success)
                    throw new HelpLibraryManagerException(result);

                Console.WriteLine("The operation completed successfully");
            }
            catch(HelpLibraryManagerException hlmEx)
            {
                result = hlmEx.ErrorCode;
                Console.WriteLine("\r\nERROR: The requested operation could not be performed.\r\nDetails: {0}",
                    hlmEx.Message);
            }
            catch(Exception ex)
            {
                result = HelpLibraryManagerException.UnknownError;
                Console.WriteLine("\r\nERROR: The requested operation could not be performed.\r\nDetails: {0}",
                    ex.ToString());
            }
            #if DEBUG
            if(System.Diagnostics.Debugger.IsAttached)
            {
                Console.WriteLine("Hit ENTER to exit...");
                Console.ReadLine();
            }
            #endif
            return result;
        }
        //=====================================================================
        /// <summary>
        /// This performs the requested task
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void actionThread_DoWork(object sender, DoWorkEventArgs e)
        {
            ThreadAction action = (ThreadAction)e.Argument;
            string arguments, contentSetupFile;
            int errorCode;

            runningThread = Thread.CurrentThread;

            try
            {
                HelpLibraryManager hlm = new HelpLibraryManager();

                // Remove old content.  We'll remove it if installing to be sure that
                // the latest copy is installed.
                if(action == ThreadAction.Install || action == ThreadAction.Remove)
                {
                    if(action == ThreadAction.Install)
                        actionThread.ReportProgress(0, (int)ThreadState.RemovingOldContent);
                    else
                        actionThread.ReportProgress(0, (int)ThreadState.RemovingContent);

                    arguments = String.Format(CultureInfo.InvariantCulture,
                        "/product \"{0}\" /version \"{1}\" /locale {2} /uninstall /silent /vendor " +
                        "\"{3}\" /mediaBookList \"{4}\" /productName \"{5}\"",
                        project.CatalogProductId, project.CatalogVersion, project.Language.Name,
                        project.VendorName, project.HelpTitle, project.ProductTitle);

                    // This doesn't have to run as an administrator
                    errorCode = hlm.RunAsNormalUser(arguments, ProcessWindowStyle.Minimized);

                    // Ignore it if not found and we are installing
                    if(errorCode != HelpLibraryManagerException.Success &&
                      (errorCode != HelpLibraryManagerException.NoBooksToInstall || action == ThreadAction.Remove))
                        throw new HelpLibraryManagerException(errorCode);
                }

                if(action == ThreadAction.Install)
                {
                    // Install the new content
                    actionThread.ReportProgress(0, (int)ThreadState.InstallingContent);

                    // Copy the MSHA file to the required name
                    contentSetupFile = Path.Combine(Path.GetDirectoryName(setupFile), "HelpContentSetup.msha");
                    File.Copy(setupFile, contentSetupFile, true);

                    arguments = String.Format(CultureInfo.InvariantCulture,
                        "/product \"{0}\" /version \"{1}\" /locale {2} /brandingPackage Dev10.mshc " +
                        "/sourceMedia \"{3}", project.CatalogProductId, project.CatalogVersion, project.Language.Name,
                        contentSetupFile);

                    // Always interactive and must run as administrator.  We can't run
                    // silently as we don't have a signed cabinet file.
                    errorCode = hlm.RunAsAdministrator(arguments, ProcessWindowStyle.Normal);

                    if(errorCode != HelpLibraryManagerException.Success)
                        throw new HelpLibraryManagerException(errorCode);

                    // Open it if installed successfully
                    action = ThreadAction.OpenCurrent;
                }

                if(action == ThreadAction.OpenCurrent)
                {
                    actionThread.ReportProgress(0, (int)ThreadState.OpeningContent);
                    System.Diagnostics.Process.Start(msHelpViewer);
                }
            }
            catch(ThreadAbortException )
            {
                // Ignore thread abort exceptions
            }
        }