]> granicus.if.org Git - icinga2/blob - agent/windows-setup-agent/Program.cs
e132bc1b1b98c35ff30c51c5feb5d32823c64c64
[icinga2] / agent / windows-setup-agent / Program.cs
1 using System;
2 using System.IO;
3 using System.Windows.Forms;
4 using Microsoft.Win32;
5 using System.Runtime.InteropServices;
6 using System.Text;
7
8 namespace Icinga
9 {
10         static class Program
11         {
12         [DllImport("msi.dll", SetLastError = true)]
13         static extern int MsiEnumProducts(int iProductIndex, StringBuilder lpProductBuf);
14
15         [DllImport("msi.dll", CharSet = CharSet.Unicode)]
16         static extern Int32 MsiGetProductInfo(string product, string property, [Out] StringBuilder valueBuf, ref Int32 len);
17
18         public static string Icinga2InstallDir
19                 {
20                         get
21                         {
22                 StringBuilder szProduct;
23
24                 for (int index = 0; ; index++) {
25                     szProduct = new StringBuilder(39);
26                     if (MsiEnumProducts(index, szProduct) != 0)
27                         break;
28
29                     int cbName = 128;
30                     StringBuilder szName = new StringBuilder(cbName);
31
32                     if (MsiGetProductInfo(szProduct.ToString(), "ProductName", szName, ref cbName) != 0)
33                         continue;
34
35                     if (szName.ToString() != "Icinga 2")
36                         continue;
37
38                     int cbLocation = 1024;
39                     StringBuilder szLocation = new StringBuilder(cbLocation);
40                     if (MsiGetProductInfo(szProduct.ToString(), "InstallLocation", szLocation, ref cbLocation) == 0)
41                         return szLocation.ToString();
42                 }
43
44                 return "";
45                         }
46                 }
47
48         public static string Icinga2DataDir
49         {
50             get
51             {
52                 return Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\icinga2";
53             }
54         }
55
56
57         public static void FatalError(Form owner, string message)
58                 {
59                         MessageBox.Show(owner, message, "Icinga 2 Setup Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
60                         Application.Exit();
61                 }
62
63                 /// <summary>
64                 /// The main entry point for the application.
65                 /// </summary>
66                 [STAThread]
67                 static void Main()
68                 {
69                         Application.EnableVisualStyles();
70                         Application.SetCompatibleTextRenderingDefault(false);
71
72                         string installDir = Program.Icinga2InstallDir;
73
74             if (installDir == "") {
75                 FatalError(null, "Icinga 2 does not seem to be installed properly.");
76                 return;
77             }
78
79                         Form form;
80
81                         if (File.Exists(Program.Icinga2DataDir + "\\etc\\icinga2\\features-enabled\\api.conf"))
82                                 form = new ServiceStatus();
83                         else
84                                 form = new SetupWizard();
85
86                         Application.Run(form);
87                 }
88         }
89 }