using System.Collections.Generic;
namespace StreamsOverUnder
{
public struct LapStreamEqualityComparer : IEqualityComparer<Element>
{
public bool Equals(Element x, Element y)
{
if (Object.ReferenceEquals(x, y))
return true;
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
return x.LapStream.Equals(y.LapStream);
}
public int GetHashCode(Element elem)
{
return elem.LapStream.GetHashCode();
}
}
class Program
{
static int Main(string[] args)
{
bool ret = false;
Task<bool> init = initStatAsync();
if (init.Result)
{
LapStreamEqualityComparer comparer = new LapStreamEqualityComparer();
foreach (Element e in Stat.Elements.Distinct(comparer).OrderBy(n => n.LapStream))
Console.WriteLine(e.ToString("L"));
ret = true;
}
return (ret) ? 0 : 1;
}
private static async Task<bool> initStatAsync()
{
AcDepots depots = new AcDepots(dynamicOnly: true);
if (!(await depots.initAsync())) return false;
List<Task<bool>> tasks = new List<Task<bool>>();
foreach (AcStream
stream in depots.SelectMany(d => d.Streams).Where(s => s.HasDefaultGroup))
tasks.Add(runStatCommandAsync($@"stat -s ""{stream}"" -o -B -fx"));
bool[] arr = await Task.WhenAll(tasks);
return (arr != null && arr.All(n => n == true));
}
private static async Task<bool> runStatCommandAsync(string cmd)
{
bool ret = false;
AcResult result = null;
try
{
result = await AcCommand.runAsync(cmd);
ret = (result != null && result.RetVal == 0);
}
catch (AcUtilsException exc)
{
Console.WriteLine($"AcUtilsException caught in runStatCommandAsync{Environment.NewLine}{exc.Message}");
}
return (ret && Stat.init(result.CmdResult));
}
}
}