private StepOneModel PrepareStepOneModel(StepOneModel model = null)
        {
            if (model == null)
            {
                model = new StepOneModel();
            }

            model.AvailableAuthenticationMethods.Add(new SelectListItem {Text = "SQL Server Authentication", Value = "10"});
            model.AvailableAuthenticationMethods.Add(new SelectListItem {Text = "Windows Authentication", Value = "20"});

            return model;
        }
        public ActionResult StepOne(StepOneModel model)
        {
            var manager = new DataSettingsManager();
            model = PrepareStepOneModel(model);

            // Make sure if SQL Server authenticaton is selected, we have been given a username/password
            if (model.DatabaseAuthenticationMethod == 10)
            {
                if (string.IsNullOrEmpty(model.DatabaseUsername))
                    ModelState.AddModelError("DatabaseUsername", "Please enter your SQL username.");

                if (string.IsNullOrEmpty(model.DatabasePassword))
                    ModelState.AddModelError("DatabasePassword", "Please enter your SQL password.");
            }

            // Make sure we have the required folder permissions
            string root = Server.MapPath("~/");
            var directories = new List<string>
            {
                Path.Combine(root, "robots.txt"),
                Path.Combine(root, "App_Data"),
                Path.Combine(root, "Uploads"),
                Path.Combine(root, "Uploads\\Media"),
                Path.Combine(root, "Uploads\\Profile")
            };

            // Check each directory, flagging a modelstate error if the permissions aren't correct
            foreach (var directory in directories.Where(directory => !CheckPermissions(directory, false, true, true, true)))
                ModelState.AddModelError("", string.Format("The '{0}' account is not granted with Modify permission on folder '{1}'. Please configure these permissions.", WindowsIdentity.GetCurrent().Name, directory));

            // Ensure the rest of the form is valid
            if (ModelState.IsValid)
            {
                try
                {
                    // Build the connection string
                    string connectionString = GenerateConnectionString(model.DatabaseAuthenticationMethod == 20, model.DatabaseServerName, model.DatabaseName, model.DatabaseUsername, model.DatabasePassword);

                    // If the database doesn't exist, create it
                    if (!DatabaseExists(connectionString))
                    {
                        string databaseCreationError = CreateDatabase(connectionString);
                        if (!string.IsNullOrEmpty(databaseCreationError))
                            throw new Exception(databaseCreationError);
                        Thread.Sleep(3000);
                    }

                    // Create settings and save
                    var settings = new DataSettings {ConnectionString = connectionString};
                    manager.SaveSettings(settings);

                    // Initialise the database
                    var dataProviderInstance = new EfDataProvider();
                    dataProviderInstance.InitDatabase();

                    // Install the core data
                    var installService = EngineContext.Current.Resolve<IInstallService>();
                    bool coreDataInstalled = installService.InstallCoreData();

                    // Make sure the core data installed correctly before running on ahead
                    if (!coreDataInstalled)
                        ModelState.AddModelError("", "An error occurred during installation, please try again.");
                    else
                        return RedirectToAction("steptwo", "install");
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", "Installation failed: " + ex);
                }
            }

            return View(model);
        }