// ap.exe should be called from OOBE [Shift] + [F10] command prompt via:

        // powershell -c iwr ap.domain.com -o ap.exe & qr
        // or
        // curl -o ap.exe ap.domain.com & ap

        // curl remarks:
        // -L is used to follow redirects e.g. redirect to https
        // curl -o ap.exe -L ap.domain.com & ap
        static async Task Main(string[] args)
        {
            _logger        = new LogUtil();
            _backendUrl    = ParseCommandlineArgs(args);
            _backendClient = new BackendClient();
            _backendClient.MessageReceived += BackendClient_MessageReceived;
            _backendClient.ResultReceived  += BackendClient_ResultReceived;
            _windowsAutopilotHashService    = new WindowsAutopilotHashService();
            _windowsAutopilotHashService.MessageReceived += WindowsAutopilotHashService_MessageReceived;

            try
            {
                // default Action is IMPORT
                var action = "IMPORT";

                // --erase device deletion request found
                if (_deleteManagedDeviceOnly)
                {
                    _logger.WriteInfo("Identified as device [DELETE] request");
                    action = "DELETE";
                }
                else
                {
                    _logger.WriteInfo("Identified as device [IMPORT] request");

                    // --skip endpoint validation found
                    if (!_skipEndpointsValidation)
                    {
                        var endpointVerificationError = false;
                        if (_endpointsValidationResult = await _backendClient.VerifyEnrollmentEndpoints())
                        {
                            _logger.WriteInfo("All enrollment URLs reachable");
                        }
                        else
                        {
                            if (!_logger.DebugMode)
                            {
                                _logger.WriteInfo("Not all enrollment URLs reachable or system time is wrong!");
                                _logger.WriteInfo($"Use '{Assembly.GetExecutingAssembly().GetName().Name} --connect --verbose' for connectivity test details.");
                                endpointVerificationError = true;
                            }
                            else
                            {
                                _logger.WriteInfo("=> Not all enrollment URLs reachable or system time is wrong!");
                                endpointVerificationError = true;
                            }
                        }
                        // --connect endpoint validation only found
                        if (_endpointsValidationOnly)
                        {
                            if (endpointVerificationError)
                            {
                                Environment.Exit(1);
                            }
                            return;
                        }
                    }

                    // --ignore Autopilot assignment found
                    if (!_ignoreAutopilotAssignment)
                    {
                        try
                        {
                            // is this already a Autopilot device?
                            var rkbase = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
                            using (RegistryKey key = rkbase.OpenSubKey(@"SOFTWARE\Microsoft\Provisioning\Diagnostics\AutoPilot"))
                            {
                                if (key != null)
                                {
                                    var tenantId = key.GetValue("CloudAssignedTenantId").ToString();
                                    if (!string.IsNullOrEmpty(tenantId))
                                    {
                                        _logger.WriteInfo("Device is already Autopilot provisioned");
                                        _logger.WriteDebug($"CloudAssignedTenantId: {tenantId}");

                                        var tenantDomain = key.GetValue("CloudAssignedTenantDomain").ToString();
                                        if (!string.IsNullOrEmpty(tenantDomain))
                                        {
                                            if (_logger.DebugMode)
                                            {
                                                _logger.WriteDebug($"CloudAssignedTenantDomain: {tenantDomain}");
                                            }
                                            else
                                            {
                                                _logger.WriteInfo($"Assigned tenant domain: {tenantDomain}");
                                            }
                                        }
                                        return;
                                    }
                                }
                            }
                        }
                        catch (Exception)
                        {
                            _logger.WriteInfo("Couldn't check existing Autopilot assignment, proceeding.");
                        }
                    }
                }

                // main work start now
                _logger.WriteInfo("Fetching system information");
                _systemInformation        = _windowsAutopilotHashService.FetchData();
                _systemInformation.Action = action;

                if (_systemInformation == null)
                {
                    _logger.WriteInfo("No Windows 10/11 operating system, can't fetch data");
                    return;
                }

                if (string.IsNullOrEmpty(_systemInformation.HardwareHash))
                {
                    _logger.WriteInfo("Information couldn't be fetched");
                }
                else
                {
                    _logger.WriteInfo("Information successfully fetched");
                    if (_fetchHardwareDataOnly)
                    {
                        return;
                    }

                    // check if our backend server is reachable
                    if (!_backendClient.IsValidUrl(_backendUrl, out _backendUrl, "HEAD"))
                    {
                        _logger.WriteInfo("Backend URL not reachable");
                        return;
                    }
                    else
                    {
                        _logger.WriteDebug($"Resolved backend URL is {_backendUrl}");
                    }

                    // Safe the info to app service AutopilotManager... result here may be 'not allowed', catched in ResultReceived handler
                    await _backendClient.SaveDataAsync(_systemInformation, _backendUrl);

                    var url         = new Url($"{_backendUrl}/autopilot/{_systemInformation.Id}/save-information");
                    var qrCodeImage = QrCodeUtil.GenerateQrCode(url.ToString());
                    //var qrCodeImage = QrCodeUtil.GenerateQrCode(url.ToString(), 8);

                    var urlNoSelfService         = new Url($"{_backendUrl}/Home/NoSelfService");
                    var qrCodeNoSelfServiceImage = QrCodeUtil.GenerateQrCode(urlNoSelfService.ToString());

                    var form = new QrCodeForm(qrCodeImage, qrCodeNoSelfServiceImage, _systemInformation, _backendClient, _backendUrl, _preCheckErrorMessage, _endpointsValidationResult);
                    form.DisplayData();

                    if (form.DialogResult == DialogResult.Retry)
                    {
                        _logger.WriteDebug("Retry received");

                        string[] arguments = Environment.GetCommandLineArgs();

                        var path            = Assembly.GetExecutingAssembly().Location;
                        var fullPathcommand = $"{path} {string.Join(" ", arguments.Skip(1).ToArray())}";

                        _logger.WriteDebug($"Using command: {fullPathcommand}");
                        _logger.WriteDebug("Starting as external process, debug output will not be received in this command prompt.");
                        _logger.WriteDebug("Start manually (not using Retry button) from this command prompt to receive the debug output again.");

                        using (var p = new Process())
                        {
                            p.StartInfo.UseShellExecute = false;
                            p.StartInfo.FileName        = path;
                            p.StartInfo.Arguments       = string.Join(" ", arguments.Skip(1).ToArray());
                            p.StartInfo.CreateNoWindow  = true;
                            p.Start();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.WriteInfo(ex.ToString());
                _logger.WriteInfo("**************** Closing application, fix and porting try again ****************");
                return;
            }
        }