<commit_before>﻿using System;
using Microsoft.Extensions.Logging;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using NBitcoin;
using System.IO;
using System.Net;
using NBXplorer.Logging;
using NBitcoin.Protocol;
using NBitcoin.DataEncoders;
using NBitcoin.RPC;

namespace NBXplorer.Configuration
{
	public class ExplorerConfiguration
	{
		public string ConfigurationFile
		{
			get;
			set;
		}
		public string DataDir
		{
			get;
			set;
		}

		public Network Network
		{
			get; set;
		}
		public List<IPEndPoint> Listen
		{
			get;
			set;
		} = new List<IPEndPoint>();
		public RPCArgs RPC
		{
			get;
			set;
		}

		public bool Rescan
		{
			get; set;
		}

		public void LoadArgs(string[] args)
		{
			LoadArgs(new TextFileConfiguration(args));
		}

		public ExplorerConfiguration LoadArgs(TextFileConfiguration consoleConfig)
		{
			ConfigurationFile = consoleConfig.GetOrDefault<string>("conf", null);
			DataDir = consoleConfig.GetOrDefault<string>("datadir", null);
			if(DataDir != null && ConfigurationFile != null)
			{
				var isRelativePath = Path.GetFullPath(ConfigurationFile).Length > ConfigurationFile.Length;
				if(isRelativePath)
				{
					ConfigurationFile = Path.Combine(DataDir, ConfigurationFile);
				}
			}

			Network = consoleConfig.GetOrDefault<bool>("testnet", false) ? Network.TestNet :
				consoleConfig.GetOrDefault<bool>("regtest", false) ? Network.RegTest :
				Network.Main;

			if(ConfigurationFile != null)
			{
				AssetConfigFileExists();
				var configTemp = TextFileConfiguration.Parse(File.ReadAllText(ConfigurationFile));
				Network = configTemp.GetOrDefault<bool>("testnet", false) ? Network.TestNet :
						  configTemp.GetOrDefault<bool>("regtest", false) ? Network.RegTest :
						  Network.Main;
			}
			if(DataDir == null)
			{
				DataDir = DefaultDataDirectory.GetDefaultDirectory("NBXplorer", Network);
			}

			if(ConfigurationFile == null)
			{
				ConfigurationFile = GetDefaultConfigurationFile();
			}

			if(!Directory.Exists(DataDir))
				throw new ConfigurationException("Data directory does not exists");

			var config = TextFileConfiguration.Parse(File.ReadAllText(ConfigurationFile));
			consoleConfig.MergeInto(config, true);

			Logs.Configuration.LogInformation("Network: " + Network);
			Logs.Configuration.LogInformation("Data directory set to " + DataDir);
			Logs.Configuration.LogInformation("Configuration file set to " + ConfigurationFile);<commit_msg>Fix proper initialization of network through command line<commit_after>﻿using System;
using Microsoft.Extensions.Logging;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using NBitcoin;
using System.IO;
using System.Net;
using NBXplorer.Logging;
using NBitcoin.Protocol;
using NBitcoin.DataEncoders;
using NBitcoin.RPC;

namespace NBXplorer.Configuration
{
	public class ExplorerConfiguration
	{
		public string ConfigurationFile
		{
			get;
			set;
		}
		public string DataDir
		{
			get;
			set;
		}

		public Network Network
		{
			get; set;
		}
		public List<IPEndPoint> Listen
		{
			get;
			set;
		} = new List<IPEndPoint>();
		public RPCArgs RPC
		{
			get;
			set;
		}

		public bool Rescan
		{
			get; set;
		}

		public void LoadArgs(string[] args)
		{
			LoadArgs(new TextFileConfiguration(args));
		}

		public ExplorerConfiguration LoadArgs(TextFileConfiguration consoleConfig)
		{
			ConfigurationFile = consoleConfig.GetOrDefault<string>("conf", null);
			DataDir = consoleConfig.GetOrDefault<string>("datadir", null);
			if(DataDir != null && ConfigurationFile != null)
			{
				var isRelativePath = Path.GetFullPath(ConfigurationFile).Length > ConfigurationFile.Length;
				if(isRelativePath)
				{
					ConfigurationFile = Path.Combine(DataDir, ConfigurationFile);
				}
			}

			Network = consoleConfig.GetOrDefault<bool>("testnet", false) ? Network.TestNet :
				consoleConfig.GetOrDefault<bool>("regtest", false) ? Network.RegTest :
				null;

			if(ConfigurationFile != null)
			{
				AssetConfigFileExists();
				var configTemp = TextFileConfiguration.Parse(File.ReadAllText(ConfigurationFile));
				Network = Network ?? (configTemp.GetOrDefault<bool>("testnet", false) ? Network.TestNet :
						  configTemp.GetOrDefault<bool>("regtest", false) ? Network.RegTest :
						  null);
			}

			Network = Network ?? Network.Main;
			if(DataDir == null)
			{
				DataDir = DefaultDataDirectory.GetDefaultDirectory("NBXplorer", Network);
			}

			if(ConfigurationFile == null)
			{
				ConfigurationFile = GetDefaultConfigurationFile();
			}

			if(!Directory.Exists(DataDir))
				throw new ConfigurationException("Data directory does not exists");

			var config = TextFileConfiguration.Parse(File.ReadAllText(ConfigurationFile));
			consoleConfig.MergeInto(config, true);

			Logs.Configuration.LogInformation("Network: " + Network);
			Logs.Configuration.LogInformation("Data directory set to " + DataDir);
			Logs.Configuration.LogInformation("Configuration file set to " + ConfigurationFile);
{'fraction_non_alphanumeric': 0.10752903484181017, 'fraction_numerical': 0.0, 'mean_word_length': 4.22489539748954, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, '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': '818235', 'n_tokens_mistral': 1770, 'n_tokens_neox': 1468, 'n_words': 300}