<commit_before>﻿using System;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.ServiceProcess;
using System.Threading;

namespace AppMetrics.AgentService
{
	static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		static void Main(string[] args)
		{
			if (args.Length > 0)
			{
				var arg = args[0];

				try
				{
					if (arg == "-debug")
					{
						var service = new AgentServiceClass();
						service.Start();
						Thread.Sleep(Timeout.Infinite);
					}
					else if (arg == "-install")
					{
						InitWorkingArea();
						try
						{
							ManagedInstallerClass.InstallHelper(new[] { ExePath, "/LogFile=" });
							SetRecoveryOptions(Const.AppName);
						}
						catch (InvalidOperationException exc)
						{
						}
					}
					else if (arg == "-uninstall")
					{
						try
						{
							ManagedInstallerClass.InstallHelper(new[] { "/u", ExePath, "/LogFile=" });
						}
						catch (InvalidOperationException exc)
						{
						}
					}
					else if (arg == "-start")
					{
						StartService();
					}
				}
				catch (Exception exc)
				{
					var message = exc.ToString();
					ShowMessage(message);
				}
			}
			else
			{
				var servicesToRun = new ServiceBase[]
					{
						new AgentServiceClass()
					};
				ServiceBase.Run(servicesToRun);
			}
		}

		private static void StartService()
		{
			using (var controller = new ServiceController(Const.AppName))
			{
				if (controller.Status != ServiceControllerStatus.Running)
				{
					controller.Start();
				}
			}
		}

		static void InitWorkingArea()
		{
			EnsureFolderExists(Const.WorkingAreaPath);

			var accessRights = Directory.GetAccessControl(Const.WorkingAreaPath);
			var accessRule = new FileSystemAccessRule("NETWORK SERVICE", FileSystemRights.FullControl,
				InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None,
				AccessControlType.Allow);
			accessRights.AddAccessRule(accessRule);
			Directory.SetAccessControl(Const.WorkingAreaPath, accessRights);

			EnsureFolderExists(Const.WorkingAreaBinPath);
			EnsureFolderExists(Const.WorkingAreaTempPath);
		}

		private static void EnsureFolderExists(string path)
		{
			if (!Directory.Exists(path))
				Directory.CreateDirectory(path);<commit_msg>bugfix<commit_after>﻿using System;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.ServiceProcess;
using System.Threading;

namespace AppMetrics.AgentService
{
	static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		static void Main(string[] args)
		{
			if (args.Length > 0)
			{
				var arg = args[0];

				try
				{
					if (arg == "-debug")
					{
						var service = new AgentServiceClass();
						service.Start();
						Thread.Sleep(Timeout.Infinite);
					}
					else if (arg == "-install")
					{
						InitWorkingArea();

						if (GetServiceStatus() == null)
						{
							ManagedInstallerClass.InstallHelper(new[] {ExePath, "/LogFile="});
							SetRecoveryOptions(Const.AppName);
						}
					}
					else if (arg == "-uninstall")
					{
						if (GetServiceStatus() != null)
						{
							ManagedInstallerClass.InstallHelper(new[] { "/u", ExePath, "/LogFile=" });
						}
					}
					else if (arg == "-start")
					{
						StartService();
					}
				}
				catch (Exception exc)
				{
					var message = exc.ToString();
					ShowMessage(message);
				}
			}
			else
			{
				var servicesToRun = new ServiceBase[]
					{
						new AgentServiceClass()
					};
				ServiceBase.Run(servicesToRun);
			}
		}

		private static void StartService()
		{
			using (var controller = new ServiceController(Const.AppName))
			{
				if (controller.Status != ServiceControllerStatus.Running)
				{
					controller.Start();
				}
			}
		}

		private static ServiceControllerStatus? GetServiceStatus()
		{
			try
			{
				var sc = new ServiceController { ServiceName = Const.AppName };
				return sc.Status;
			}
			catch (InvalidOperationException)
			{
				return null;
			}
		}

		static void InitWorkingArea()
		{
			EnsureFolderExists(Const.WorkingAreaPath);

			var accessRights = Directory.GetAccessControl(Const.WorkingAreaPath);
			var accessRule = new FileSystemAccessRule("NETWORK SERVICE", FileSystemRights.FullControl,
				InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None,
				AccessControlType.Allow);
			accessRights.AddAccessRule(accessRule);
			Directory.SetAccessControl(Const.WorkingAreaPath, accessRights);

			EnsureFolderExists(Const.WorkingAreaBinPath);
			EnsureFolderExists(Const.WorkingAreaTempPath);
		}

		private static void EnsureFolderExists(string path)
		{
			if (!Directory.Exists(path))
				Directory.CreateDirectory(path);
{'fraction_non_alphanumeric': 0.11296988781735878, 'fraction_numerical': 0.000787246604999016, 'mean_word_length': 2.9952830188679247, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7521301', 'n_tokens_mistral': 2054, 'n_tokens_neox': 1498, 'n_words': 298}