/// <summary>
		/// All installed provisioning profiles, sorted by newest first.
		/// </returns>
		public static IList<MobileProvision> GetAllInstalledProvisions (bool includeExpired)
		{
			
			if (!Directory.Exists (ProfileDirectory))
				return new MobileProvision[0];
			
			DateTime now = DateTime.Now;
			var list = new List<MobileProvision> ();
			var uuids = new HashSet<string> ();
			
			foreach (FilePath file in Directory.GetFiles (ProfileDirectory, "*.mobileprovision")) {
				var m = new MobileProvision ();
				try {
					m.Load (file);
					if (includeExpired || m.ExpirationDate > now)
						if (uuids.Add (m.Uuid))
							list.Add (m);
				} catch (Exception ex) {
					LoggingService.LogWarning ("Error reading iPhone mobile provision file '" + file +"'", ex);
				}
			}
			
			//newest first
			list.Sort ((x,y) => y.CreationDate.CompareTo (x.CreationDate));
			
			return list;
		}
		public static MobileProvision LoadFromFile (string fileName)
		{
			var m = new MobileProvision ();
			m.Load (fileName);
			return m;
		}
		static string GetProfileBundleID (MobileProvision provision)
		{
			if (!provision.Entitlements.ContainsKey ("application-identifier"))
				return null;
			
			var id = ((PlistString)provision.Entitlements ["application-identifier"]).Value;
			int i = id.IndexOf ('.') + 1;
			if (i > 0 && i < id.Length)
				return id.Substring (i);
			return null;
		}
		static string ConstructValidAppId (MobileProvision provision, string bundleId, out bool exact)
		{
			exact = false;
			
			string appid = provision.ApplicationIdentifierPrefix[0] + "." + bundleId;
			
			if (!provision.Entitlements.ContainsKey ("application-identifier"))
				return null;
			
			var allowed = ((PlistString)provision.Entitlements ["application-identifier"]).Value;
			int max = Math.Max (allowed.Length, appid.Length);
			for (int i = 0; i < max; i++) {
				if (i < allowed.Length && allowed[i] == '*')
					break;
				if (i >= appid.Length || allowed[i] != appid[i])
					return null;
			}
			exact = (allowed.Length == appid.Length) && allowed[allowed.Length -1] != '*';
			return appid;
		}
		static BuildResult EmbedProvisioningProfile (IProgressMonitor monitor, IPhoneProjectConfiguration conf, MobileProvision profile)
		{
			monitor.BeginTask (GettextCatalog.GetString ("Embedding provisioning profile"), 0);
			
			try {
				File.Copy (profile.FileName, conf.AppDirectory.Combine ("embedded.mobileprovision"), true);
			} catch (IOException ex) {
				var result = new BuildResult ();
				result.AddError ("Embedding the provisioning profile failed: " + ex.Message);
				return result;
			}
			
			monitor.EndTask ();
			return null;
		}