using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace Cleanup;

internal class Program
{
	private const string DEFAULT_PATH = "C:\\Qualcomm\\QSPR\\TestLog";

	private const int DEFAULT_DAYS_OLD = 15;

	private static void Main(string[] args)
	{
		string text = "C:\\Qualcomm\\QSPR\\TestLog";
		int num = 15;
		string searchPattern = "";
		bool recursiveSearch = false;
		if (args.Length > 0)
		{
			foreach (string text2 in args)
			{
				try
				{
					string[] array = text2.Split('=');
					switch (array[0].ToLower())
					{
					case "-path":
					case "-p":
						text = array[1];
						break;
					case "-daysold":
					case "-d":
						num = int.Parse(array[1]);
						break;
					case "-searchpattern":
					case "-s":
						searchPattern = array[1];
						break;
					case "-recursive":
					case "-r":
						recursiveSearch = true;
						break;
					case "-help":
					case "-h":
					case "-?":
					case "/help":
					case "/h":
					case "/?":
						ShowUsage();
						return;
					default:
						throw new Exception("Error processing arg");
					}
				}
				catch
				{
					Console.WriteLine("Error processing argument: " + text2);
					ShowUsage();
					return;
				}
			}
		}
		Console.WriteLine("Cleaning all files {0} days or older from {1} ...", num, text);
		DeleteFiles(text, recursiveSearch, searchPattern, num);
	}

	private static void DeleteFiles(string folderToClean, bool recursiveSearch, string searchPattern, int daysOld)
	{
		try
		{
			DirectoryInfo directoryInfo = new DirectoryInfo(folderToClean);
			FileInfo[] array;
			if (string.IsNullOrWhiteSpace(searchPattern))
			{
				array = directoryInfo.GetFiles();
			}
			else
			{
				string[] array2 = searchPattern.Split(',', ';', '|');
				List<FileInfo> list = new List<FileInfo>();
				string[] array3 = array2;
				foreach (string searchPattern2 in array3)
				{
					list.AddRange(directoryInfo.GetFiles(searchPattern2));
				}
				array = list.ToArray();
			}
			FileInfo[] array4 = array;
			foreach (FileInfo fileInfo in array4)
			{
				if (DateTime.Now - fileInfo.LastWriteTime >= TimeSpan.FromDays(daysOld))
				{
					try
					{
						File.Delete(fileInfo.FullName);
					}
					catch (Exception ex)
					{
						Console.WriteLine("Error deleting file: {0}{1}{2}", fileInfo.FullName, Environment.NewLine, ex.ToString());
					}
				}
			}
			if (recursiveSearch)
			{
				DirectoryInfo[] directories = directoryInfo.GetDirectories();
				DirectoryInfo[] array5 = directories;
				foreach (DirectoryInfo directoryInfo2 in array5)
				{
					DeleteFiles(directoryInfo2.FullName, recursiveSearch, searchPattern, daysOld);
				}
			}
		}
		catch (Exception ex2)
		{
			Console.WriteLine("Error cleaning folder: {0}{1}{2}", folderToClean, Environment.NewLine, ex2.ToString());
		}
	}

	private static void ShowUsage()
	{
		string value = Assembly.GetExecutingAssembly().ManifestModule.Name + " [-p=<Folder To Clean>] [-d=<Number Of Days Old>] [-s=<FileName pattern>] [-r]" + Environment.NewLine + "  -p (or -path): The folder path to clean (Default path is C:\\Qualcomm\\QSPR\\TestLog)" + Environment.NewLine + "  -d (or -daysOld): The number of days old a file must be in order to be deleted (Default age is " + 15 + " days)" + Environment.NewLine + "  -s (or -searchPattern): The search pattern used to filter list of files to delete (the pattern \"*.txt\" would match all txt files)" + Environment.NewLine + "  -r (or -recursive): If specified all subdirectories will be cleaned as well" + Environment.NewLine;
		Console.WriteLine(value);
	}
}
