using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Configuration;
using System.Web.Security;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Nmo;
using Microsoft.SqlServer.NotificationServices;

namespace BootstrapDemo
{
	class Program
	{
		static bool noWait = false;

		static void WaitForUser(string message)
		{
			if (false == noWait)
			{
				WL(message);
				Console.ReadLine();
			}
		}

		static void DisplayException(Exception e)
		{
			const int maxInnerExceptionLevels = 5;

			WL("Exception type: {0}", e.GetType());
			WL("Exception message: {0}", e.Message);
			WL("Exception stack trace: {0}", e.StackTrace);
			WL("");

			Exception innerException = e.InnerException;
			int levels = 1;
			while (null != innerException && levels <= maxInnerExceptionLevels)
			{
				WL("Inner exception type: {0}", innerException.GetType());
				WL("Inner exception message: {0}", innerException.Message);
				WL("Inner exception stack trace: {0}", innerException.StackTrace);
				WL("");

				innerException = innerException.InnerException;
				levels++;
			}
		}
		static void W(string format, params object[] arg)
		{
			Console.Write(format, arg);
		}

		static void WL(string format, params object[] arg)
		{
			Console.WriteLine(format, arg);
		}

		static void Main(string[] args)
		{
			if (args.Length > 0 && String.Compare(args[0], "/nowait", true) == 0)
			{
				noWait = true;
			}
			WL("Start...");
			try
			{
				string databaseServer = Environment.MachineName;
				string nsServer = Environment.MachineName;
				string administratorsRoleName = "Administrators";
				string adminUserName = "admin";
				string adminPassword = "a!Account123";

				W("Checking if Administrators role exists...");
				if (Roles.RoleExists(administratorsRoleName))
				{
					WL("YES.");
				}									
				else
				{
					WL("NO.");

					W("Creating Administrators Role...");
					Roles.CreateRole(administratorsRoleName);
					WL("DONE.");
				}

				W("Checking if admin user exists...");
				MembershipUser adminUser = Membership.GetUser(adminUserName);

				if (null != adminUser)
				{
					WL("YES.");
				}
				else
				{
					WL("NO.");
					W("Creating admin user...");
					MembershipCreateStatus createStatus;
					adminUser = Membership.CreateUser(
						adminUserName,
						adminPassword,
						"admin@admin.com",
						"What is your favorite color?",
						"Blue",
						true,
						out createStatus);

					if (null != adminUser)
					{
						WL("DONE.");
					}
					else
					{
						WL("Create user failed.");
						throw new Exception(createStatus.ToString());						
					}					
				}

				Debug.Assert(null != adminUser);

				W("Checking if admin user is in Administrators role...");
				if (Roles.IsUserInRole(adminUserName, administratorsRoleName))
				{
					WL("YES.");
				}
				else
				{
					WL("NO.");
					W("Adding admin user to Administrators role...");
					string[] userNames = new string[1];
					userNames[0] = adminUserName;

					Roles.AddUsersToRole(userNames, administratorsRoleName);
					WL("DONE.");
				}								
			}
			catch (Exception e)
			{
				WL("Caught an exception:");
				DisplayException(e);

				goto Cleanup;
			}
			finally
			{
				WL("DONE.");				
			}

		Cleanup:
			try
			{

			}
			catch (Exception e)
			{
				WL("Caught an exception:");
				DisplayException(e);
			}
			finally
			{
				WaitForUser("Press Enter to end program");				
			} 
		}
	}
}
